尝试使用wp_Register_script/style并将它们从数组中排队-出现错误

时间:2013-10-28 作者:Neta Meta

以下是我目前得到的信息:

  // my array
$this->headersArray = array(
    array(\'handle\' => \'buy_mobile_funcs\', \'src\' => \'jsFunctions\', \'extention\' => \'js\', \'deps\' => \'\', \'media\' => \'\'),

    array(\'handle\' => \'css_main_style\', \'src\' => \'main_style\', \'extention\' => \'css\', \'deps\' => \'\', \'media\' => \'screen\')
);

// registering 
foreach($this->headersArray as $singleHeader){
    if($singleHeader[\'extention\'] == \'js\'){
        wp_register_script(
            $singleHeader[\'handle\'], plugins_url(\'scripts/\'.$singleHeader[\'src\'].\'.\'.$singleHeader[\'extention\'],__FILE__), 
            (!empty($singleHeader[\'deps\']))? $singleHeader[\'deps\']  : \'\'
        );
    }else{
        wp_register_style(
            $singleHeader[\'handle\'], plugins_url(\'css/\'.$singleHeader[\'src\'].\'.\'.$singleHeader[\'extention\'],__FILE__), 
            (!empty($singleHeader[\'deps\']))? $singleHeader[\'deps\']  : \'\'
        );
    }
}

// enqueuing 
foreach($this->headersArray as $singleHeader){
    if($singleHeader[\'extention\'] == \'js\'){
        wp_enqueue_script($singleHeader[\'handle\']);
    }else{
        wp_enqueue_style( $singleHeader[\'handle\'], \'\', \'\', \'\', $singleHeader[\'media\'] );
    }
}
我知道我可以把两个foreach组合起来。然而,我有理由保持现状,这与问题无关。

错误:

Notice: wp_register_script was called incorrectly. Scripts and styles should not be registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts, or login_enqueue_scripts hooks. Please see Debugging in WordPress for more information. (This message was added in version 3.3.) in /home1/stodeckc/public_html/orgadsite/wp-includes/functions.php on line 3012
提前感谢。

1 个回复
最合适的回答,由SO网友:kaiser 整理而成

正如“错误消息”(实际上是“做错了”消息)告诉您的,您使用了错误的挂钩:

wp_enqueue_scripts, admin_enqueue_scripts, 或login_enqueue_scripts 挂钩。

只要阅读关于挂钩的内容,将代码包装在回调中,然后在三个挂钩中的一个上正确注册它(提示:适合的那个)。

结束

相关推荐

意外删除了wp_options数据库表的active_plugins部分

好吧,所以我很确定我在这里完蛋了,因为我没有数据库备份,但我想我还是问问以防万一。在PhpMyAdmin中,我的意思是只需转到wp\\u options表并清空该表中的active\\u plugins部分,因为插件错误而无法登录。我点击了删除按钮,而不是清空它。是否有任何方法可以手动添加此表的active\\u插件部分?我尝试了修复数据库的方法,但没有成功。