Using several add_action with parameters inside a class

I’m creating a theme framework and using a class to initiate all basic function of the theme (adding theme support, menus, etc). I’ve got stuck when I tried to enqueue the scripts and styles though:

I’m using add_action( 'init', array(&$this, 'EnqueueScrits'), 10, 1); and do_action( 'init', $enqueued ); to call method with the $enqueued param. That’s working fine, however when I try to use the same technique in order to add wp_enqueue_style the $param are getting mixed up (the $param for scripts are also used for styles).

Read More

My solution then was to create a chain reaction to the first add_action like so:

but then nothing happens and the action is not called at all (it seems like that anyway). Does anyone knows a better solution for this or what am I missing?

EDIT: Here’s the full code. My class:

class Nomade
{

    public function __construct($menus, $features, $enqueued, $styled)
    {

        // Define base constants
        define('THEME_FRAMEWORK', 'nomade');
        define('NOMADE_VERSION', '1.0');

        // Define directories
        define('NOMADE_DIR', get_template_directory());
        define('NOMADE_LIB_DIR', get_template_directory() . '/library');

        $this->AddThemeSupport($features);
        $this->MenuBuilder($menus);

        // Add support for WordPress features
        add_action('after_setup_theme', array(__CLASS__, 'MenuBuilder'));
        add_action( 'after_setup_theme', array(__CLASS__, 'AddThemeSupport'));
        add_action( 'init', array(&$this, 'EnqueueScrits'), 10, 1);

        do_action( 'init', $enqueued );

    }

    public function AddThemeSupport($features)
    {

        if(is_array($features))
        {

            foreach ($features as $feature) {

                add_theme_support($feature);

            }

        }

    }

    public function MenuBuilder($menus)
    {

        if (is_array($menus)) {

            foreach ($menus as $location => $name) {

                register_nav_menus(array(

                    $location => $name

                ));

            }

        }

    }

    public function EnqueueScrits($enqueued)
    {

        if (is_array($enqueued)) {

            foreach ($enqueued as $value) {

                wp_enqueue_script( $value["name"], $value["location"], array("jquery"), $value["version"], true );

            }

        }

    }

}

And here how I instantiate the object (in a separate file):

<?php

// Depedencies: menu registrer and builder
include('application.class.php');

// Set menus
$menus = array( 'main_menu' => 'Main Menu', 'footer_menu' => 'Footer Menu', 'side_menu' => 'Sidebar Menu' );

// Set theme features
$features = array( 'menus', 'custom-background', 'custom-header', 'post-thumbnail' );

// Register scripts array
$register = array( 'scripts' => 'NOMADE_DIR' . '/js/scripts.js' );

// Enqueue scripts array
$enqueued = array(  
    array("name" => "general", "location" => get_template_directory_uri() . '/js/general.js', "version" => 1)
);

$styled = array(
    array("name" => "main", "location" => "lac1", "version" => 2, "media" => "all")
);

// Instantiate Nomade (main application)
$Nomade = new Nomade($menus, $features, $enqueued, $styled);

Thanks for the help in advance

Related posts

Leave a Reply

1 comment

  1. The init action…

    is one of many WordPress core action hooks. Hence it does not require, i.e. should not be accompanied by, a manual do_action call.

    You only need to manually run do_action for custom, previously non-existent, action hooks.

    Further, init is not the action which you want to hook the script enqueuing with. There is an action specifically for this purpose.

    Your situation

    if ( ! class_exists( 'WPSE_Your_Class' ) ) :
    class WPSE_Your_Class
    {
    
        private $enqueued = array();
    
        public function enqueue_all_scripts()
        {
            if ( is_array( $this->enqueued ) && ! empty( $this->enqueued ) ) {
                foreach ( $this->enqueued as $script ) {
                    wp_register_script(
                        $script['handle'],
                        $script['src'],
                        array( 'jquery' ),
                        $script['version'],
                        true
                    );
                    wp_enqueue_script( $script['handle'] );
                }
            }
        }
    
        public function __construct( $enqueued = array() )
        {
            $this->enqueued = $enqueued;
            add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_all_scripts' ) );
        }
    
    }
    endif;
    

    Pass the $enqueued array to the object instance via the constructor of your class.
    Assign that value to a class property and thereafter hook the enqueue_all_scripts method to wp_enqueue_scripts.
    Make said method dependent on the previously populated class property.

    Create a new instance like so:

    $enqueued = array(
          array(
               'handle' => 'some-handle',
               'src' => 'http://some-url.com/path/to/script/scripts.js',
               'version' => '1.0'
          ),
          array(
               'handle' => 'another-handle',
               'src' => 'http://some-url.com/path/to/script/another_scripts.js',
               'version' => '1.1'
          )
          // possibly more
    );
    $your_class_instance = new WPSE_Your_Class( $enqueued );
    

    Sidenote

    As an aside, &$this is PHP4, and, as I recently learned, it is useless to attempt to support PHP4, since the core won’t run on it anyway.

    Edit – your full code rewritten

    The above is the way I’d do it. That’s certainly not the only option and most likely not the slickest possible, but it does work. Given your edit, I’ve adjusted your code while trying to preserve some of your own naming conventions and coding style:

    class Nomade
    {
    
        /* class properties */
        private $enqueued = array();
        private $features = array();
        private $menus = array();
        private $styled = array();
    
        /* constructor */
        public function __construct( $menus, $features, $enqueued, $styled )
        {
            // Assign values to class properties
            $this->enqueued = $enqueued;
            $this->features = $features;
            $this->menus = $menus;
            $this->styled = $styled;
    
            // Define base constants
            define('THEME_FRAMEWORK', 'nomade');
            define('NOMADE_VERSION', '1.0');
    
            // Define directories
            define('NOMADE_DIR', get_template_directory());
            define('NOMADE_LIB_DIR', get_template_directory() . '/library');
    
            $this->AddThemeSupport();
            $this->MenuBuilder();
    
            // Add support for WordPress features
            add_action( 'after_setup_theme', array( $this, 'MenuBuilder' ) );
            add_action( 'after_setup_theme', array( $this, 'AddThemeSupport' ) );
            add_action( 'wp_enqueue_scripts', array( $this, 'EnqueueScripts' ) );
        }
    
        public function AddThemeSupport()
        {
            if( is_array( $this->features ) && ! empty( $this->features ) )
            {
                foreach ( $this->features as $feature ) {
                    add_theme_support( $feature );
                }
            }
        }
    
        public function MenuBuilder()
        {
            if( is_array( $this->menus ) && ! empty( $this->menus ) )
            {
                register_nav_menus( $this->menus );
            }
        }
    
        public function EnqueueScripts()
        {
            if( is_array( $this->enqueued ) && ! empty( $this->enqueued ) )
            {
                foreach ( $this->enqueued as $value ) {
                    wp_enqueue_script(
                        $value["name"],
                        $value["location"],
                        array("jquery"),
                        $value["version"],
                        true
                    );
                }
            }
        }
    
    }