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.
initinstead ofwp_enqueue_scripts. That should work, though it might not be the best hook. – hampusn Sep 20 '12 at 11:10add_shortcodefunction didn't work at thewp_enqueue_scriptshook. He only mentioned the hook for calling my class. – Tim S. Sep 20 '12 at 12:13