Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

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.

share|improve this question

closed as too localized by Brian Fegter, kaiser, Rarst Oct 2 '12 at 20:58

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
My guess is that you are using the wrong action hook when adding the short code. Try with init instead of wp_enqueue_scripts. That should work, though it might not be the best hook. –  hampusn Sep 20 '12 at 11:10
    
@hampusn It did fix my shortcode, but my script and style isn't loading now. Please have a look at my edit. –  Tim S. Sep 20 '12 at 11:28
    
@hampusn Never mind. Your reply fixed the problem. If you post it as an actual answer I can select as answer. Thanks a lot! –  Tim S. Sep 20 '12 at 11:40
    
Thanks Tim but you should mark @Vinicius's answer as the correct one. He bothered to find the correct hook :) –  hampusn Sep 20 '12 at 12:03
    
The add_shortcode function didn't work at the wp_enqueue_scripts hook. He only mentioned the hook for calling my class. –  Tim S. Sep 20 '12 at 12:13
show 1 more comment

2 Answers 2

up vote 1 down vote accepted

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'));
    }

    // ...
}
share|improve this answer
add comment

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.

share|improve this answer
    
I have added some additional information. Please have a look at that. –  Tim S. Sep 20 '12 at 11:28
add comment

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