[英]How do I enqueue multiple Wordpress AJAX scripts in my functions.php file?
I am currently trying to enqueue 2 Wordpress AJAX scripts in my functions.php file, but I seem to have hit an error.我目前正在尝试在我的 functions.php 文件中加入 2 Wordpress AJAX 脚本,但我似乎遇到了错误。 The first AJAX script works fine, but for the second I keep getting a 500 Internal Server Error.
第一个 AJAX 脚本工作正常,但第二个脚本我一直收到 500 内部服务器错误。
What I have so far in my AJAX scripts is this:到目前为止,我的 AJAX 脚本中的内容是:
This first block is for my first AJAX script located in my-ajax-script.js
.第一个块用于我位于
my-ajax-script.js
中的第一个 AJAX 脚本。
function lesson_watched_call(lesson_id_value) {
console.log('Lesson watched fn called.');
console.log(lesson_id_value);
$.post(my_ajax_obj.ajax_url, {
_ajax_nonce: my_ajax_obj.nonce, // nonce
action: "lesson_watched", // action call
lesson_value: lesson_id_value // data
});
}
This second block is for my second AJAX script located in fav-ajax-script.js
.第二个块用于我位于
fav-ajax-script.js
中的第二个 AJAX 脚本。
function toggleFavTut(tutorial_id_value) {
console.log('Tutorial toggle fn called.');
console.log(tutorial_id_value);
$.post(my_ajax_obj.ajax_url, {
_ajax_nonce: my_ajax_obj.nonce, // nonce
action: "tutorial_fav_toggle", // action call
tutorial_value: tutorial_id_value // data
});
}
In my functions.php
file, this is what I have:在我的
functions.php
文件中,这是我所拥有的:
/*---------------------------------------------------------------------------------*/
/* Enqeue AJAX Script and Define Lesson Watched Function */
/*---------------------------------------------------------------------------------*/
add_action( 'wp_enqueue_scripts', 'enqueue_ajax_scripts' );
function enqueue_ajax_scripts() {
wp_enqueue_script( 'ajax-script', get_template_directory_uri() . '/js/my-ajax-script.js', array('jquery') );
wp_localize_script( 'ajax-script', 'my_ajax_obj',
array( 'ajax_url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'lesson_nonce_value' ),
)
);
wp_enqueue_script( 'ajax-script2', get_template_directory_uri() . '/js/fav-ajax-script.js', array('jquery') );
wp_localize_script( 'ajax-script2', 'my_ajax_obj',
array( 'ajax_url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'tutorial_nonce_value' ),
)
);
}
function lesson_watched() {
$lastValue_php = wp_unslash( $_POST['lesson_value'] );
$current_user_id = get_current_user_id();
$found = get_user_meta( $current_user_id, 'lessons_watched_ids', true );
if ( !in_array( $lastValue_php, $found ) ) :
if( empty( $found ) ) :
update_user_meta( $current_user_id, 'lessons_watched_ids', array( $lastValue_php ) );
else :
$found[] = $lastValue_php;
update_user_meta( $current_user_id, 'lessons_watched_ids', $found );
endif;
endif;
wp_die(); // Ends all AJAX handlers
}
add_action('wp_ajax_nopriv_lesson_watched', 'lesson_watched');
add_action('wp_ajax_lesson_watched', 'lesson_watched');
/*---------------------------------------------------------------------------------*/
/* Enqeue AJAX Script and Define Tutorial Toggle Function */
/*---------------------------------------------------------------------------------*/
function tutorial_fav_toggle() {
$tutorial_value = wp_unslash( $_POST['tutorial_value'] );
$current_user_id = get_current_user_id();
$found = get_user_meta( $current_user_id, 'tutorial_favorited_ids', true );
if ( !in_array( $tutorial_value, $found ) ) :
$found[] = $tutorial_value;
update_user_meta( $current_user_id, 'tutorial_favorited_ids', $found );
else:
$key = array_search( $tutorial_value, $found );
unset( $found[$key] );
update_user_meta( $current_user_id, 'tutorial_favorited_ids', $found );
endif;
wp_die(); // Ends all AJAX handlers
}
add_action('wp_ajax_nopriv_tutorial_fav_toggle', 'tutorial_fav_toggle');
add_action('wp_ajax_tutorial_fav_toggle', 'tutorial_fav_toggle');
I first start out by enqueueing both scripts and then localizing each script.我首先将两个脚本加入队列,然后对每个脚本进行本地化。 I'm wondering if maybe I have to use a different variable for some of the localize variables, but I am not certain about that.
我想知道我是否必须为某些本地化变量使用不同的变量,但我不确定。 Would you have an tips on what to try to debug?
你有关于尝试调试什么的提示吗?
Per the docs , the second parameter to wp_localize_script
should be unique.根据文档,
wp_localize_script
的第二个参数应该是唯一的。
$object_name is the name of the variable which will contain the data.
$object_name 是将包含数据的变量的名称。 Note that this should be unique to both the script and to the plugin or theme.
请注意,这对于脚本和插件或主题都应该是唯一的。 Thus, the value here should be properly prefixed with the slug or another unique value, to prevent conflicts.
因此,此处的值应该以 slug 或其他唯一值作为前缀,以防止冲突。
So, following your pattern the second function could use my_ajax_obj2
for the variable.因此,按照您的模式,第二个 function 可以使用
my_ajax_obj2
作为变量。
wp_enqueue_script( 'ajax-script', get_template_directory_uri() . '/js/my-ajax-script.js', array('jquery') );
wp_localize_script( 'ajax-script', 'my_ajax_obj',
array( 'ajax_url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'lesson_nonce_value' ),
)
);
wp_enqueue_script( 'ajax-script2', get_template_directory_uri() . '/js/fav-ajax-script.js', array('jquery') );
wp_localize_script( 'ajax-script2', 'my_ajax_obj2',
array( 'ajax_url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'tutorial_nonce_value' ),
)
);
Do take care to make your variable specific to your application, too.也请注意使您的变量特定于您的应用程序。 These variables all effectively are just properties on the global
window
object and everything can read/write them, so make sure they are unique.这些变量实际上都是全局
window
object 上的属性,任何东西都可以读/写它们,所以要确保它们是唯一的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.