我正在创建一个主题框架,并使用一个类来启动主题的所有基本功能(添加主题支持、菜单等)。我在尝试将脚本和样式排队时遇到了问题:
我正在使用add_action( \'init\', array(&$this, \'EnqueueScrits\'), 10, 1);
和do_action( \'init\', $enqueued );
使用$enqueued参数调用方法。但是,当我尝试使用相同的技术来添加wp_enqueue_style
$param正在混淆(脚本的$param也用于样式)。
然后,我的解决方案是对第一个add\\u操作创建连锁反应,如下所示:
但是什么都没有发生,动作也没有被调用(不管怎么说,似乎是这样)。有没有人知道更好的解决方案,或者我遗漏了什么?
编辑:这是完整的代码。我的班级:
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 );
}
}
}
}
下面是我如何实例化对象(在单独的文件中):
<?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);
提前谢谢你的帮助
最合适的回答,由SO网友:Johannes Pille 整理而成
Theinit
操作
是众多
WordPress core action hooks. 因此,它不需要(即不应随附)手册
do_action
呼叫
您只需手动运行do_action
对于以前不存在的自定义操作挂钩。
进一步的init
不是要挂接脚本排队的操作。有一个action specifically for this purpose.
你的情况
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;
通过
$enqueued
通过
constructor 你的班级
将该值分配给
class property 然后钩住
enqueue_all_scripts
方法到
wp_enqueue_scripts
.
使所述方法依赖于先前填充的类属性。
创建一个新实例,如下所示:
$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 );
旁注作为旁白,
&$this
为PHP4,且,
as I recently learned, 尝试支持PHP4是无用的,因为无论如何核心都不会在其上运行。
编辑-您的完整代码已重写,以上就是我的做法。这当然不是唯一的选择,也很可能不是最巧妙的选择,但它确实有效。根据您的编辑,我调整了您的代码,同时试图保留您自己的一些命名约定和编码风格:
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
);
}
}
}
}