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).
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
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
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 towp_enqueue_scripts
.Make said method dependent on the previously populated class property.
Create a new instance like so:
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: