【问题标题】:WP_debug errors with wp_enqueuewp_enqueue 出现 WP_debug 错误
【发布时间】:2012-07-05 00:17:31
【问题描述】:

我遇到了两种我不知道如何解决的错误。

您可以在此处查看错误:http://www.brainbuzzmedia.com/themes/vertex/

第一种出现两次,如下所示:

Notice: wp_enqueue_script was called incorrectly. Scripts and styles should not be registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts, or init hooks. Please see Debugging in WordPress for more information. (This message was added in version 3.3.) in /home/admin/buzz/themes/vertex/wp-includes/functions.php on line 3587

我在functions.php中有一个调用:

function my_init() {
if (!is_admin()) {
    wp_enqueue_script('jquery');
}
}
add_action('init', 'my_init');

第二种错误类型是这样的:

Notice: Undefined property: stdClass::$slider_height in /vertex/wp-content/themes/vertex/slider_settings.php on line 32

无论我在哪里(if 语句内部或外部或两者)定义这些变量,它们仍然给我这个错误。

* 更新

我在主题文件的子文件夹中加入了一些其他脚本,主要用于管理区域。

$path = get_template_directory_uri() 。 '/includes/metabox/smart_meta_box/smart_meta_fields/layout-editor/';

wp_enqueue_script('mcolorpicker', $path . 'js/mColorPicker.js', array('jquery'));

wp_enqueue_style('chosen', $path . 'css/chosen.css');

wp_enqueue_style('content-layout', $path .'css/content-layout.css');

wp_enqueue_script('jquery-json', $path . 'js/jquery.json.js', array('jquery'));

wp_enqueue_script('chosen-jquery', $path . 'js/chosen.jquery.min.js', array('jquery'));

wp_enqueue_script('content-layout-js', $path . 'js/content-layout.js', array('jquery', 'jquery-ui-sortable'));

我认为前端显示也可能需要它们。我如何以正确的方式将这些加入队列?

  • 更新 2

这是发生两个未定义属性错误的代码:

link to txt

【问题讨论】:

  • 关于第二个问题,能否附上你的主题文件中的相关行?
  • 在 txt 中添加了指向 php 文件的链接

标签: php jquery wordpress debugging


【解决方案1】:

使用 wp_enqueue_scripts 动作来调用这个函数,或者 admin_enqueue_scripts 在管理员端调用它。

要将其加载到前端,请使用

add_action('wp_enqueue_scripts', 'my_scripts_method');
function my_scripts_method() {
    wp_enqueue_script('jquery');
}

要为管理员加载它,请使用

add_action('admin_enqueue_scripts', 'my_admin_scripts_method');
function my_admin_scripts_method() {
    wp_enqueue_script('jquery');
}

参考:Codex

第二个错误是因为jQuery没有加载。

更新:

如果您在您的functions.php 或您的任何plugin file 中直接调用任何wp_register_script(xxx)wp_enqueue_style(xxx),则在wp_enqueue_script 处理程序中使用它们,如下所示

add_action('wp_enqueue_scripts', 'my_scripts_method');
function my_scripts_method() {
    wp_register_script('xxx'); // replace the xxx with valid script name
    wp_enqueue_script('jquery');
    wp_enqueue_style(xxx) // if you have any
}

【讨论】:

  • 我已经用您的前端和管理操作替换了我的“my_init”代码,但我仍然遇到同样的错误。为什么说错误在“/wp-includes/functions.php”中?我没有触及 wp-includes 文件夹中的任何内容。另外,jquery的第二个错误如何没有被加载?它通过该脚本加载到标题中。
  • 我的 wp-includes/functions.php 与您可以从 wordpress.org 下载的相同。我不知道第 3587 行实际上是什么,因为有注释区域,但与入队无关。如果我在文件中搜索入队,甚至什么都没有显示。我在主题文件夹中的 functions.php 甚至没有那么长。
  • 你的主题的functions.php中有wp_register_script调用吗?
  • 啊,我在子文件夹中有一些脚本,我将更新原始帖子以便显示代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-19
  • 1970-01-01
  • 2014-03-13
  • 1970-01-01
  • 2016-08-31
  • 2011-12-24
  • 2018-08-11
相关资源
最近更新 更多