I have a simple and single class located in a file in its own directory in plugins.
The class has just one purpose, and that is to remove some unwanted wordpress actions using remove_action
, it works just as a simple wrapper for a number of remove_action()
function calls.
Now, the problem is, when trying to instantiate the class outside its own file. Then all the remove_action()
function calls does not do its job, but still returning TRUE
as in, success. This was discovered while trying to debug the simple class.
If I instantiate the class and call its only function within the same file where class is, everything works like a charm. Weird? Could be only me, but I really want to call the plugin class methods outside its own file, like in theme files.
Did anyone had this problem while developing plugins?
<?php
// removed the plugin stuff...
class WPStylesheetLoader
{
public $styles = array();
public function add($identifier, $url)
{
$this->styles[] = array('identifier' => $identifier, 'url' => $url);
}
public function enqueueAll()
{
foreach($this->styles as $style)
{
wp_enqueue_style($style['identifier'], $style['url']);
}
}
public function activate() {
echo add_action('get_header', array(&$this, 'enqueueAll'));
}
}
$stylesheet = new WPStylesheetLoader;
$stylesheet->add('normalize', get_bloginfo('template_url') . '/css/normalize.css');
$stylesheet->add('style', get_bloginfo('stylesheet_url'));
$stylesheet->add('media-queries', get_bloginfo('template_url') . '/css/media-queries.css');
$stylesheet->activate();
Some errors and strange things:
echo add_action()
does not exist. It’s simplyadd_action()
.The correct hook to print the enqueued styles and scripts is
wp_enqueue_scripts
.You’re doing a plugin but loading styles from a theme?
The theme
style.css
is always loaded, why are you enqueuingget_bloginfo('stylesheet_url')
?&$this
is PHP 4 code, drop the&
.The following works, I’ve added a
__construct
so as to register the plugin’s URL: