1

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();

1 Answer 1

1

Some errors and strange things:

  • echo add_action() does not exist. It's simply add_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 enqueuing get_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:

class WPStylesheetLoader
{
    public $styles = array();
    public $plugin_url;

    public function __construct()
    {
        $this->plugin_url = plugins_url( '/', __FILE__ );
    }

    public function add( $identifier, $url )
    {
        $this->styles[] = array(
            'identifier' => $identifier, 
            'url'        => $this->plugin_url . $url 
        );
    }

    public function enqueueAll()
    {
        foreach( $this->styles as $style )
            wp_enqueue_style( $style['identifier'], $style['url'] );
    }

    public function activate() 
    {
        add_action( 'wp_enqueue_scripts', array( $this, 'enqueueAll' ) );
    }
}

$stylesheet = new WPStylesheetLoader;
$stylesheet->add( 'normalize', 'css/normalize.css' );
$stylesheet->add( 'media-queries', 'css/media-queries.css' );
$stylesheet->activate();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.