My plugin class doesn’t work!

Long story short; I’m writing a plugin in OOP. The constructor is run, but the callback of my add_action does not run. I just can’t find out why.

I have added echo’s on different spots to see what works and what not. The constructor is run, though the pepare() method is not.

Read More

WP_DEBUG is enabled, though I’m not getting any warnings or errors.

<?php
/*
Plugin Name: Simplist slider
Description: My description
Version: 1.0
Author: Tim Severien
*/

class SimplistSlider {
    public function __construct() {
        // something is wrong here
        add_action('wp_enqueue_scripts', array($this, 'prepare'));
    }

    public function prepare() {
        // registers
        wp_register_style('simplist-slider', plugins_url('/css/simplist-slider.css', __FILE__), array(), '1');
        wp_register_script('simplist-slider', plugins_url('/js/simplist-slider.js', __FILE__), array('jquery'), '1');

        // enqueues
        wp_enqueue_style('simplist-slider');
        wp_enqueue_script('simplist-slider');

        add_shortcode('slider', array($this, 'shortcode'));
    }

    public function shortcode($attr, $content = NULL) {
        // format content, irrelevant
    }
}

$simplist_slider = new SimplistSlider();
?>

I’m running a fresh WordPress 3.4.2 installation.

Thanks in advance.


edit

After modifying the code (you guys suggested), I have manage to get the shortcode working! Thanks!

Problem is, that my style and script isn’t loaded. Have a look at my updated code:

<?php
/*
Plugin Name: Simplist slider
Description: My description
Version: 1.0
Author: Tim Severien
*/

class SimplistSlider {
    public function __construct() {
        // adding shortcode here
        add_shortcode('slider', array($this, 'shortcode'));

        add_action('wp_enqueue_scripts', array($this, 'registerScripts'));
    }

    public function registerScripts() {
        // registers
        wp_register_style('simplist-slider', plugins_url('/css/simplist-slider.css', __FILE__), array(), '1');
        wp_register_script('simplist-slider', plugins_url('/js/simplist-slider.js', __FILE__), array('jquery'), '1');

        // enqueues
        wp_enqueue_style('simplist-slider');
        wp_enqueue_script('simplist-slider');
    }

    public function shortcode($attr, $content = NULL) {
        // format content, irrelevant
    }
}

function simplist_slider_init() {
    $simplist_slider = new SimplistSlider();
}

add_action('plugins_loaded', 'simplist_slider_init');

?>

edit #2

  • Please drop whatever your holding in your primary hand
  • Look at the palm of your hand
  • Continue moving your hand towards your head until they make contact

facepalm();

I screwed up the theme and removed the wp_head(); function. My sincere apologies! Everything works as expected now.

Related posts

Leave a Reply

2 comments

  1. Your problem is that you are using the wrong hook to add your shortcode. wp_enqueue_scripts is executed way too late and should only be used to add javascript includes.

    You should instead consider using the hook called init for adding your shortcodes. This hook is called after the current theme’s functions.php has been included which means that you don’t risk getting your shortcode overridden (unless that is a desired feature) by the theme if there is a name collision.

    Here is a list of available actions: Plugin API – Action Reference.

    If you’re going with the init hook for the shortcodes but still want the plugin to be hooked to plugins_loaded you could do something like these changes:

    class SimplistSlider {
        public function __construct() {
            // Hook shortcodes
            add_action('init', array($this, 'registerShortcodes'));
            // Hook javascripts
            add_action('wp_enqueue_scripts', array($this, 'registerScripts'));
        }
    
        // ...
    
        public function registerShortcodes() {
            // adding shortcodes here
            add_shortcode('slider', array($this, 'shortcode'));
        }
    
        // ...
    }
    
  2. The recommended way for including plugins is using the plugins_loaded hook.

    add_action( 'plugins_loaded', 'simplist_slider_init' );
    function simplist_slider_init() {
        $simplist_slider = new SimplistSlider();
    }
    

    Apart from that, if you’re correctly enabling the plugin in the admin interface, you should be able to echo things in the function above and so on.