0

I'm doing a plugin using Shortcodes. I have some classes which extend an abstract class calls Shortcode.

abstract class Shortcode {

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

    public function __construct($tag) {
        $this->attrs = array();
        $this->tag = $tag;
        $this->function = static::className().'::getCallBack';

        add_shortcode( $this->tag, $this->function );

        $this->init();
    }

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

    abstract public function check_page();

    abstract public static function className();

    abstract public static function getCallBack( $attrs );
}

My Shortcode class

class MyShortcode extends Shortcode {

    public function check_page(){
         error_log( "test 2" );
         global $post;
         $pattern = '/(\['.$this->tag.'\])/';
         if( !empty( $post->post_content ) && preg_match(  $pattern, $post->post_content ) ){
            add_action('wp_enqueue_scripts', array( $this , 'set_styles'));
        }
    }

    public static function getCallBack( $attrs = null ){
       ...
    }

    //CSS stylesheets
    public function set_styles() {
        //wp_enqueue_style( 'wgsstyle', PLUGIN_DIR_URL . 'front-end/views/css/wgs-front-end.css' );
        //wp_enqueue_style( 'wgscardsstyle', PLUGIN_DIR_URL . 'front-end/views/css/wgs-cards.css' );
    }

    public static function className(){
        return __CLASS__;
    }

}

Something happens is weird... In debug.log, it's written "test 1" but not "test 2" and css stylesheets are still loaded.

Is there a cache for Shortcodes ? How can I reinitialize this code ?

3
  • How you call MyShortcode? Nov 2, 2017 at 16:59
  • in a class named FrontEnd.php which called in the main class. I have just moved some files, that works but it seems to be fixed with the old code. But when I comment //New MyShortcode(), the page show correctly the shortcode as text. So weird !
    – J.BizMai
    Nov 2, 2017 at 17:25
  • This is probably an issue with Class inheritance and class overrides. Nov 2, 2017 at 17:28

2 Answers 2

0

I'm pretty sure it's this line:

$this->function = static::className().'::getCallBack';

I believe this is going to be the name of the abstract class. You may want to try get_called_class() or something similar.

Try logging the $this->function to see what the name of the class is.

1
  • Everything is called correctly, even getCallBack()
    – J.BizMai
    Nov 3, 2017 at 9:54
0

The problem was when you moved up some files in your plugin and you upload changes in your wordpress, that does not remove the files with the old location/path. The solution was to remove the entire plugin and install it again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.