0

I don´t why for some reasons, my callback function is not called for this url :

https://my-site/?post_type=foo-login&p=11

In the database is like this :

+----+---------------------------------------------------------------------------+------------+-------------+-----------+-----------+
| ID |                               post_content                                | post_title | post_status | post_name | post_type |
+----+---------------------------------------------------------------------------+------------+-------------+-----------+-----------+
| 19 | <!-- wp:paragraph --><p>[foo-plans][/foo-plans]</p><!-- /wp:paragraph --> | Plans      | published   | plans     | page      |
| 11 | [foo-login][/foo-login]                                                   | Login      | published   | sign-in   | foo-login |
+----+---------------------------------------------------------------------------+------------+-------------+-----------+-----------+

Php classes

Shortcode abstract class

abstract class Shortcode{

    public $tag;
    public $attrs;
    public $function;

    public function __construct( $tag ) {
        $this->attrs = array();
        $this->tag = $tag;
        $this->function = get_called_class().'::getCallBack';
        add_shortcode( $this->tag, $this->function );

        $this->init();
    }

    protected function init(){
        if( !is_admin() ){
            //Front-end
            add_action('wp',  array( $this , 'check_page' ) );
        }
    }

    abstract public function check_page();

    public static function page_template( $template = "" ) {
        if( empty( $template ) ){
            $template = locate_template( array( 'page.php' ) );
        }
        return $template;
    }

}

Login Shortcode Class

class LoginShortcode extends Shortcode {

    private $post_type;

    public function __construct( $tag ) {
        parent::__construct( $tag );
        $this->post_type = $tag;
    }

    public function check_page(){
        $login_post = get_unique_post_by_content( "[foo-login][/foo-login]", ["foo-login"] );
        if( get_post_type() === $this->post_type || ( isset($_GET['p']) && (int) $_GET['p'] === $login_post->ID ) ){
            add_filter( 'template_include', array( get_called_class(), 'page_template' ), 99 );
            add_action( "template_redirect", array( $this, "is_redirection") );
            add_action( "wp_enqueue_scripts", array( $this, "set_styles" ) );
            add_action( "wp_enqueue_scripts", array( $this, "set_scripts" ) );
        }
    }

    /**
     * @throws \Exception
     */
    public static function is_redirection() {
         CALLED
         ...
         return;
    }

    public static function getCallBack( $attrs = null ){
            NEVER CALLED !!!    
    }

    public function set_styles() {
        ...
    }

    public function set_scripts() {
        ...
    }
}

For the shortcode [foo-plans][/foo-plans] that works with the right url but for [foo-login][/foo-login] the callback function is never called.

When I display the list of shortcodes, foo-login is in the list.

global $shortcode_tags;
var_dump( $shortcode_tags );

/*output = array (
  'wp_caption' => 'img_caption_shortcode',
  'caption' => 'img_caption_shortcode',
  'gallery' => 'gallery_shortcode',
  'playlist' => 'wp_playlist_shortcode',
  'audio' => 'wp_audio_shortcode',
  'video' => 'wp_video_shortcode',
  'embed' => '__return_false',
  'foo-plans' => 'Shortcodes\\PlansListShortcode::getCallBack',
  'foo-login' => 'Shortcodes\\LoginShortcode::getCallBack'
)*/

One differnce between foo-plans abd foo-login is the post type but I don´t know if it´s the key to find a solution.

  • I don't see any function with "add_shortcode". That is the regular way of adding shortcodes. It may work, but to be honest, I've never seen somebody adding shortcode that complicated (over the top OOP style..). – user3135691 May 2 at 21:41
  • the "add_shortcode" function is in the abstract class Shortcode inside the constructor. This structure is complex because it´s a little part of a plugin core code. I reuse this core for several plugins. I´m using ini and xml files to simply generate the admin menu, pages, shortcodes etc... thanks to this code. – J.BizMai May 3 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.