WP 排队脚本顺序和本地化错误,需要挂钩吗?
WP enqueue script order and localize errors , hook needed?
以下代码产生错误:
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. This notice was triggered by the waitlist_update_call handle. Please see Debugging in WordPress for more information. (This message was added in version 3.3.0.) in /home/food/domains/xyz.com/public_html/wp-includes/functions.php on line 5225"
它还在控制台中显示错误:
POST https://theste.com/wp-admin/admin-ajax.php 400 (Bad Request)
PHP 函数文件中的代码
wp_enqueue_script( 'update_call',
get_theme_file_uri( '/assets/js/update_call.js' ),
array('jquery'), null, true );
wp_localize_script('update_call', 'my_ajax',
array('ajax_url' => admin_url('admin-ajax.php')));
//calls Waitinglist data and creates table
add_action('wp_ajax_function_1', 'update_function'); // logged in user can make a call
add_action('wp_ajax_nopriv_function_1', 'update'); // non logged in user can make a call
function update_function() {
global $wpdb;
$results = $wpdb->query( $wpdb->prepare("UPDATE 'my_table_name' SET `currentstatus` =
'myupdate1' WHERE ID = '1'"));
die($results);
}
编辑 1:
我试图直接调用它。请原谅我的 newness.Ok 下面的拳头解决了入队问题,但 POST 400 错误仍然存在。错误是
POST https://x.com/wp-admin/admin-ajax.php 400 (Bad Request)
当点击我应该触发的按钮时,我得到 -
Uncaught ReferenceError: update_functionis not defined
at HTMLButtonElement.onclick
我已将函数文件中的 PHP 更改为:
function my_scripts() {
wp_enqueue_script( 'update_call', get_theme_file_uri( '/assets/js/update_call.js' ), array('jquery'), null, true );
wp_localize_script('update_call', 'my_ajax', array('ajax_url' => admin_url('admin-ajax.php')));
//calls Waitinglist data and creates table
}
add_action('wp_enqueue_scripts', 'my_scripts');
add_action('wp_ajax_function_1', 'waitlist_update'); // logged in user can make a call
function waitlist_update() {
global $wpdb;
$results = $wpdb->query( $wpdb->prepare("UPDATE 'wp_wpdatatable_4' SET `currentstatus` =
'myupdate1' WHERE wdt_ID = '1'"));
die($results);
}
单独的 JS 文件是:
// JavaScript Document
jQuery.ajax({
type: 'post',
url: my_ajax.ajax_url,
action: 'waitlist_update',
success: function(data){
// callback function
}
});
和HTML是:
<button class="seat-btn" ID="update" onclick="update_function()">Go!</button>
WordPress 提供挂钩以正确地排队脚本。
您可能直接调用了入队方法。它应该在 wp_enqueue_scripts
钩子的动作回调中。
function my_scripts() {
wp_enqueue_script('update-call', get_template_directory_uri() . '/assets/js/update_call.js', array('jquery'), false, true);
wp_localize_script('update-call', 'my_ajax', array(
'ajax_url' => admin_url('admin-ajax.php')
));
}
add_action('wp_enqueue_scripts', 'my_scripts');
此外,
//calls Waitinglist data and creates table
add_action('wp_ajax_function_1', 'update_function'); // logged in user can make a call
add_action('wp_ajax_nopriv_function_1', 'update_function'); // non logged in user can make a call
如果你在旧问题中查看我之前的。 function_1
是从 AJAX 调用中请求的操作,update_function
是触发操作时我们应该调用的函数名称。触发操作后未登录的用户将根据您上面的代码调用函数 update
,这是不正确的。
如果您只想允许登录用户发出此请求,您可以删除为非登录用户附加的第二个操作。
add_action('wp_ajax_nopriv_function_1', 'update_function'); // notice the nopriv?
注:
ajax 动作名称应与动作挂钩匹配。
jQuery.ajax({
//...
data: {
action: 'action_name'
}
});
// The action name should be same wp_ajax_{action_name} from whatever defined in jQuery action
add_action('wp_ajax_action_name', 'update_function')
以下代码产生错误:
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. This notice was triggered by the waitlist_update_call handle. Please see Debugging in WordPress for more information. (This message was added in version 3.3.0.) in /home/food/domains/xyz.com/public_html/wp-includes/functions.php on line 5225"
它还在控制台中显示错误:
POST https://theste.com/wp-admin/admin-ajax.php 400 (Bad Request)
PHP 函数文件中的代码
wp_enqueue_script( 'update_call',
get_theme_file_uri( '/assets/js/update_call.js' ),
array('jquery'), null, true );
wp_localize_script('update_call', 'my_ajax',
array('ajax_url' => admin_url('admin-ajax.php')));
//calls Waitinglist data and creates table
add_action('wp_ajax_function_1', 'update_function'); // logged in user can make a call
add_action('wp_ajax_nopriv_function_1', 'update'); // non logged in user can make a call
function update_function() {
global $wpdb;
$results = $wpdb->query( $wpdb->prepare("UPDATE 'my_table_name' SET `currentstatus` =
'myupdate1' WHERE ID = '1'"));
die($results);
}
编辑 1:
我试图直接调用它。请原谅我的 newness.Ok 下面的拳头解决了入队问题,但 POST 400 错误仍然存在。错误是
POST https://x.com/wp-admin/admin-ajax.php 400 (Bad Request)
当点击我应该触发的按钮时,我得到 -
Uncaught ReferenceError: update_functionis not defined
at HTMLButtonElement.onclick
我已将函数文件中的 PHP 更改为:
function my_scripts() {
wp_enqueue_script( 'update_call', get_theme_file_uri( '/assets/js/update_call.js' ), array('jquery'), null, true );
wp_localize_script('update_call', 'my_ajax', array('ajax_url' => admin_url('admin-ajax.php')));
//calls Waitinglist data and creates table
}
add_action('wp_enqueue_scripts', 'my_scripts');
add_action('wp_ajax_function_1', 'waitlist_update'); // logged in user can make a call
function waitlist_update() {
global $wpdb;
$results = $wpdb->query( $wpdb->prepare("UPDATE 'wp_wpdatatable_4' SET `currentstatus` =
'myupdate1' WHERE wdt_ID = '1'"));
die($results);
}
单独的 JS 文件是:
// JavaScript Document
jQuery.ajax({
type: 'post',
url: my_ajax.ajax_url,
action: 'waitlist_update',
success: function(data){
// callback function
}
});
和HTML是:
<button class="seat-btn" ID="update" onclick="update_function()">Go!</button>
WordPress 提供挂钩以正确地排队脚本。
您可能直接调用了入队方法。它应该在 wp_enqueue_scripts
钩子的动作回调中。
function my_scripts() {
wp_enqueue_script('update-call', get_template_directory_uri() . '/assets/js/update_call.js', array('jquery'), false, true);
wp_localize_script('update-call', 'my_ajax', array(
'ajax_url' => admin_url('admin-ajax.php')
));
}
add_action('wp_enqueue_scripts', 'my_scripts');
此外,
//calls Waitinglist data and creates table
add_action('wp_ajax_function_1', 'update_function'); // logged in user can make a call
add_action('wp_ajax_nopriv_function_1', 'update_function'); // non logged in user can make a call
如果你在旧问题中查看我之前的function_1
是从 AJAX 调用中请求的操作,update_function
是触发操作时我们应该调用的函数名称。触发操作后未登录的用户将根据您上面的代码调用函数 update
,这是不正确的。
如果您只想允许登录用户发出此请求,您可以删除为非登录用户附加的第二个操作。
add_action('wp_ajax_nopriv_function_1', 'update_function'); // notice the nopriv?
注: ajax 动作名称应与动作挂钩匹配。
jQuery.ajax({
//...
data: {
action: 'action_name'
}
});
// The action name should be same wp_ajax_{action_name} from whatever defined in jQuery action
add_action('wp_ajax_action_name', 'update_function')