Notice: wp_enqueue_script was called incorrectly
-
01-10-2020 - |
Вопрос
This is an error I'm receiving when my plugin is activated:
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 login_enqueue_scripts hooks. Please see Debugging in WordPress for more information. (This message was added in version 3.3.) in /directory/ on line 3547
This is line 3547:
trigger_error( sprintf( __( '%1$s was called <strong>incorrectly</strong>. %2$s %3$s' ), $function, $message, $version ) );
Here is the rest of the if statement for the WP_DEBUG output:
if ( WP_DEBUG && apply_filters( 'doing_it_wrong_trigger_error', true ) ) {
if ( function_exists( '__' ) ) {
$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 ) );
} else {
$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 ) );
}
}
This is the only instance of wp_enqueue script I have in my plugin:
wp_enqueue_script('csv3', plugins_url( '/js/demo.js' , __FILE__ ) , array( 'jquery' ));
It's pretty much verbatim from an example I found that performed the task I need
Here are the three functions that pass variables to the demo.js file:
function check_db(){
global $table;
global $quanid;
$hf_userid = get_current_user_id();
global $wpdb;
$wpdb->get_results( $wpdb->prepare("SELECT count( 1 ) FROM $table WHERE ItemID = '$quanid' AND user = '$hf_userid'", ARRAY_A));
}
add_action('wp_ajax_check_db', 'check_db');
function update_entry(){
global $quanid;
$price = isset($_POST[$quanid]);
$hf_userid = get_current_user_id();
global $table;
global $wpdb;
$wpdb->update( $wpdb->prepare( '$table',
array(
'ItemID' => '$quanid',
'Price' => $price,
'user' => $hf_userid)));
}
add_action('wp_ajax_update_entry', 'update_entry');
function post_entry(){
global $quanid;
$price = isset($_POST[$quanid]);
$hf_userid = get_current_user_id();
global $wpdb;
$wpdb->insert( $wpdb->prepare(
'$table',
array(
'ItemID' => '$quanid',
'Price' => $price,
'user' => $hf_userid
),
array(
'%d', '%d', '%d'
)
));
die();
return true;
}
add_action('wp_ajax_post_entry', 'post_entry');
Решение
You do not show how you hook your wp_enqueue_script()
call.
But the error message you get is pretty clear, use one of the following hooks:
wp_enqueue_scripts
admin_enqueue_scripts
login_enqueue_scripts
depending on the use case - see the notes at the wp_enqueue_script()
codex page for additional information.
For example do for correctly enqueuing on the frontend:
function csv_three_script() {
wp_enqueue_script(
'csv3',
plugins_url(
'/js/demo.js',
__FILE__
),
array(
'jquery'
)
);
}
add_action( 'wp_enqueue_scripts', 'csv_three_script' );