我已经安装了好几个wordpress,现在我正在尝试创建自己的小框架,以便更有效地创建和维护我的主题。我想做的是为wordpress提供的挂钩提供一个包装。
我想在我的应用程序中调用这样的挂钩
$_addThumb = function(){
add_theme_support(\'post-thumbnails\');
};
$core->registerAction(array(\'tag\' => \'after_theme_setup\',
\'callback\' => $_redirect
));
在我的类中,我像这样处理逻辑(这是一个片段,整个代码如下)
if (!empty($this -> _actions)) {
foreach ($this->_actions as $action) {
add_action($action[0], array($this, call_user_func($action[1])));
}
}
问题是,如果我像这样将$this关键字添加到add\\u操作调用中…
add_action($tag, array($this, $callback))
…我收到以下警告:
警告:call\\u user\\u func\\u array()要求参数1是有效回调,第二个数组成员不是Z:\\Website\\wp includes\\plugin中的有效方法。php
如果我不这样添加它…
add_action($tag, $callback)
然后我收到通知:
未定义的偏移量:Z:\\Website\\wp includes\\plugin中的0。php
不过,在这两种情况下,一切都是可行的。我只是想了解为什么会发生这种情况,至少要消除警告。
以下是我的完整代码:
class Core {
/**
* theme features
*/
protected $_features = array();
private $_actions = array();
/**
* register extra functions so you can add actions to the theme
*@param array mixed
*/
public function registerAction($args) {
if (!is_array($args)) {
$args = func_get_args();
}
if (count($args) < 2) {
throw new InvalidArgumentException(sprintf(\'Not enough arguments(registerCallback needs at least two arguments): %s.\', print_r($args, true)));
}
$arg_list = array_values($args);
/**
* sets the default priority
*/
if (!isset($arg_list[2])) {
$arg_list[2] = 10;
}
/**
* sets default args
*/
if (!isset($arg_list[3])) {
$arg_list[3] = 0;
}
if (!is_callable($arg_list[1])) {
throw new Exception("callback not callable : Core:13");
}
$this -> _actions[] = $arg_list;
}
/**
* TODO: development only
*/
public function get_actions() {
echo "<pre>";
print_r($this -> _actions);
echo "</pre>";
}
/**
* set theme features
* \'post-formats\'
* \'post-thumbnails\'
* \'custom-background\'
* \'custom-header\'
* \'automatic-feed-links\'
* \'menus\'
* @param string (required) name of the feature to be added
* @param array (optional) optional arguments
*/
public function set_feature($feature, $options = array()) {
if (is_array($options) && !empty($options)) {
$this -> _features[][\'args\'] = array($feature, $options);
} else {
$this -> _features[][\'name\'] = $feature;
}
}
public function get_features() {
return $this->_features;
}
function __construct() {
}
public function init($tag = \'\', $function = \'\') {
add_action(\'after_setup_theme\', array($this, \'initial_setup\'));
if (!empty($this -> _actions)) {
foreach ($this->_actions as $action) {
add_action($action[0], array($this, call_user_func($action[1])), $action[2], $action[3]);
}
}
}
public function initial_setup() {
// check if we have ay features so we can add them to our theme
if (!empty($this -> _features)) {
foreach ($this->_features as $feature) {
if (isset($feature[\'args\'])) {
add_theme_support($feature[\'args\'][0], $feature[\'args\'][1]);
} else {
add_theme_support($feature[\'name\']);
}
}
}
}
}
我的职能。php
$core = new Core();
$core->set_feature(\'automatic-feed-links\');
$core->set_feature(\'post-formats\', array(\'aside\',
\'image\',
\'link\',
\'quote\',
\'status\',
\'gallery\',
\'video\' ));
$_addThumb = function(){
add_theme_support(\'post-thumbnails\');
};
$_sidebars = function() {
register_sidebar( array(
\'name\' => __( \'Main Widget Area\', \'twentythirteen\' ),
\'id\' => \'sidebar-1\',
\'description\' => __( \'Appears in the footer section of the site.\', \'twentythirteen\' ),
\'before_widget\' => \'<aside id="%1$s" class="widget %2$s">\',
\'after_widget\' => \'</aside>\',
\'before_title\' => \'<h3 class="widget-title">\',
\'after_title\' => \'</h3>\',
) );
register_sidebar( array(
\'name\' => __( \'Secondary Widget Area\', \'twentythirteen\' ),
\'id\' => \'sidebar-2\',
\'description\' => __( \'Appears on posts and pages in the sidebar.\', \'twentythirteen\' ),
\'before_widget\' => \'<aside id="%1$s" class="widget %2$s">\',
\'after_widget\' => \'</aside>\',
\'before_title\' => \'<h3 class="widget-title">\',
\'after_title\' => \'</h3>\',
) );
};
$_adminHead = function(){
echo \'<style>[for="wp_welcome_panel-hide"] {display: none !important; } #normal-sortables{display:none !important;}</style>\';
};
/** i don\'t get a notiche with this **/
$core->registerAction(array(\'tag\' => \'after_theme_setup\', \'callback\' => $_addThumb));
/** it works but i get this warning
* arning: call_user_func_array() expects parameter 1 to be a valid callback, second array member is not a valid method in Z:\\Armen Website\\wp-includes\\plugin.php on line
*/
$core->registerAction(array(\'tag\' =>\'admin_head\', \'callback\' => $_adminHead));
$core->registerAction(array(\'tag\' => \'widgets_init\', \'callback\' => $_sidebars));
$core->init();