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
do_action
call - it is not necessary. In the line above that (add_action( 'init', array(&$this, 'EnqueueScrits'), 10, 1);
), change the action frominit
towp_enqueue_scripts
, i.e. make thatadd_action( 'wp_enqueue_scripts', array(&$this, 'EnqueueScrits'));
. And finally, make the$enqueued
array accessible by yourEnqueueScrits
method by assigning it to a class property (as I have laid out below) from within the constructor and access it via$this->enqueued
from within yourEnqueueScrits
method. Do the same for the other 3 variables.