【问题标题】:Notice: wp_enqueue_script was called incorrectly注意:wp_enqueue_script 被错误调用
【发布时间】:2014-07-17 13:13:20
【问题描述】:

所以我正忙于 Wordpress。我有一个网站和所有的主题。一切正常。然后我想激活新主题。 BENG BENG,白屏死机。我调试了,这是错误:

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 C:\xampp\htdocs\wp-includes\functions.php on line 2944

第 2944 行只是引发错误的行。我已经检查过了。

有人遇到过并解决过这个问题吗?

编辑:

参考线:

    function _doing_it_wrong( $function, $message, $version ) {

    do_action( 'doing_it_wrong_run', $function, $message, $version );

    // Allow plugin to filter the output error trigger
    if ( WP_DEBUG && apply_filters( 'doing_it_wrong_trigger_error', true ) ) {
        $version = is_null( $version ) ? '' : sprintf( __( '(This message was added in version %s.)' ), $version );
        $message .= ' ' . __( 'Please see <a href="http://codex.wordpress.org/Debugging_in_WordPress">Debugging in WordPress</a> for more information.' );
        trigger_error( sprintf( __( '%1$s was called <strong>incorrectly</strong>. %2$s %3$s' ), $function, $message, $version ) );
    }
}

【问题讨论】:

  • 你能告诉我们网站引用的行吗?
  • 错误地使用wp_enqueue_script() 不会触发致命错误。最重要的是,仅当您将 WP_DEBUG 设置为 true 时才会显示错误。主题肯定有其他问题。

标签: wordpress


【解决方案1】:

该错误与您添加的代码无关。这不是与 Wordpress 核心相关的问题,而是您的主题或插件的问题

错误的意思是某些脚本过早地入队,即wp_enqueue_script() 挂接到了在wp_enqueue_scriptsadmin_enqueue_scriptsinit 之前运行的错误钩子。

不幸的是,Wordpress 中的这个错误有点过时,因为它没有告诉你问题出在哪里,只是错误地调用了 wp_enqueue_script

您需要在您的主题和插件中查找wp_enqueue_script 的所有实例,并检查它是否正确挂钩

编辑

从您的 cmets 中,您找到了三个 wp_enqueue_script 实例。你现在需要看看它是如何被钩住的。它应该看起来像这样

function some_function_name(){

   wp_enqueue_script(ALL THE SCRIPT DETAILS IN HERE);
}

add_action( 'THIS IS THE HOOK THAT IS IMPORTANT', 'some_function_name'); 

THIS IS THE WRONG HOOK USED 是你必须检查的,因为这是错误的钩子。这必须是 wp_enqueue_scriptsadmin_enqueue_scripts,具体取决于脚本是用于前端还是后端

【讨论】:

  • 我有三个文件,其中wp_enqueue_script 即将出现。我如何知道我是否拥有正确的文件以及要更改的内容?
  • 现在我得到一个白屏?
【解决方案2】:

在您的某些插件或主题中,可能会包含这样的脚本:

wp_enqueue_script( string $handle, string $src = '', array $deps = array(), string|bool|null $ver = false, bool $in_footer = false )

尝试使用此代码更改它:

<?php

function wpb_adding_scripts() {
wp_register_script('my_amazing_script', plugins_url('amazing_script.js', __FILE__), array('jquery'),'1.1', true);
wp_enqueue_script('my_amazing_script');
}

add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts' );  
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-06
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 2023-02-22
    • 1970-01-01
    • 2012-12-24
    • 1970-01-01
    相关资源
    最近更新 更多