有道链接阿贾克斯网址 [英] Proper way to link Ajax URL
问题描述
在我的插件,我有一些jQuery阿贾克斯code,用于处理表单数据,它只要点击按钮添加到数据库中。由于许多人有不同的路径,以自己的插件文件夹,我想知道是否有反正标准化指向数据处理PHP文件的URL。请参阅下面我举的例子:
$。阿贾克斯({
键入:POST,
网址:url_to_php_file_path.php
数据:数据,
缓存:假的,
成功:函数(){
警报(加)。
}
});
搞清楚目标网址
在Word中preSS,所有的AJAX请求必须作出以下网址:
http://www.example.com/wp-admin/admin-ajax.php
您应该不居住在插件和主题目录中的文件,从AJAX请求直接。
另外,不要硬code上述网址,而不是你应该使用下面的函数来构造的网址:
<脚本>
ajax_url =< PHP的回声ADMIN_URL(管理-ajax.php');?>中;
< / SCRIPT>
或者以上,则可以使用 wp_localize_script ()
的,但是这不是必需的,以上是太细
注意:不要担心admin的一部分,该网址是正确的,以供所有用户使用,包括非登录的(游客)用户
。告诉Word preSS用于您的AJAX请求有什么功能
您需要让字preSS知道哪些功能应该处理您的AJAX请求。
为了这个目的,你将创建一个自定义函数,并使用注册它的 wp_ajax _ *
和 wp_ajax_nopriv _ *
挂钩:
add_action('wp_ajax_mycustomfunc','mycustomfunc'); //已登录用户
add_action('wp_ajax_nopriv_mycustomfunc','mycustomfunc'); // Guest用户
功能mycustomfunc(){
$无论= esc_html($ _ POST ['什么']);
回声它的工作原理:'。$什么;
出口; //这是要正确结束AJAX请求所需。
}
不要忘记在AJAX请求指定mycustomfunc太
最后,这里是你将如何作出正确的AJAX请求:
(功能($){
$(文件)。就绪(函数(){
VAR my_data = {
动作:mycustomfunc',//这是必需的,以便字preSS知道要使用的FUNC
什么:没错,是//发布你想在这里的任何变量
};
jQuery.post(ajax_url,my_data,函数(响应){
警报('从服务器得到了这样的:'+响应);
});
});
})(jQuery的);
联合一切
如果你不得不把它成一个文件,这里是你如何做到这一点:
//注册我的自定义功能的AJAX处理
add_action('wp_ajax_mycustomfunc','mycustomfunc'); //已登录用户
add_action('wp_ajax_nopriv_mycustomfunc','mycustomfunc'); // Guest用户
功能mycustomfunc(){
$无论= esc_html($ _ POST ['什么']);
回声它的工作原理:'。$什么;
出口; //这是要正确结束AJAX请求所需。
}
//内联的JavaScript
add_action('wp_footer','my_inline_js');
功能my_inline_js(){&GT?;
<脚本>
//设置ajax_url变量全球范围内提供
ajax_url =< PHP的回声ADMIN_URL(管理-ajax.php');?>中;
//请在文件准备好你的AJAX请求:
(函数($){
$(文件)。就绪(函数(){
VAR my_data = {
动作:mycustomfunc',//这是必需的,以便字preSS知道要使用的FUNC
什么:没错,是//发布你想在这里的任何变量
};
$。员额(ajax_url,my_data,函数(响应){//这将使在页面加载一个AJAX请求
警报('从服务器得到了这样的:'+响应);
});
});
})(jQuery的);
< / SCRIPT>
< PHP
}
注:对于 ajax_url
部分,你可以使用的 wp_localize_script()
,而不是手动设置它,但它不够灵活,因为它需要指定一个现有排队的脚本,你可能没有。
注:另外,对于手动输出的内联JavaScript到页面时, wp_footer
挂钩是正确的使用。如果使用 wp_localize_script()
那么你会使用 wp_enqueue_scripts
钩来代替。
In my plugin, I have some jQuery-Ajax code that processes form data and adds it to the database as soon as the button is clicked. Since many people have a different path to their plugin folder, I was wondering if there was anyway to standardize the URL that points to the data processing PHP file. See my example below:
$.ajax({
type: "POST",
url: "url_to_php_file_path.php",
data: data,
cache: false,
success: function() {
alert.("added");
}
});
Figuring out the target URL
In WordPress, all AJAX requests must be made to the following URL:
http://www.example.com/wp-admin/admin-ajax.php
You should not make an AJAX request directly to a file residing in the plugin or theme directory.
Also, do not hard-code the above URL, instead you should use the following function to construct the URL:
<script>
ajax_url = "<?php echo admin_url('admin-ajax.php'); ?>";
</script>
Alternatively to the above, you can use wp_localize_script()
, but this is not required, the above is fine too.
Note: Don't worry about the "admin" part, this URL is the correct one to use for all users, including non-logged-in (guest) users.
Tell WordPress what function to use for your AJAX request
You need to let WordPress know which function should process your AJAX request.
For that purpose you'll create a custom function, and register it using the wp_ajax_*
and wp_ajax_nopriv_*
hooks:
add_action('wp_ajax_mycustomfunc', 'mycustomfunc'); // Logged-in users
add_action('wp_ajax_nopriv_mycustomfunc', 'mycustomfunc'); // Guest users
function mycustomfunc() {
$whatever = esc_html($_POST['whatever']);
echo 'It works: '.$whatever;
exit; // This is required to end AJAX requests properly.
}
Don't forget to specify "mycustomfunc" in the AJAX request too
Finally, here is how you would make a proper AJAX request:
(function ($) {
$(document).ready(function () {
var my_data = {
action: 'mycustomfunc', // This is required so WordPress knows which func to use
whatever: "yes it is" // Post any variables you want here
};
jQuery.post(ajax_url, my_data, function(response) {
alert('Got this from the server: ' + response);
});
});
})(jQuery);
Combine it all
If you had to put it all into one file, here's how you'd do it:
// Register my custom function for AJAX processing
add_action('wp_ajax_mycustomfunc', 'mycustomfunc'); // Logged-in users
add_action('wp_ajax_nopriv_mycustomfunc', 'mycustomfunc'); // Guest users
function mycustomfunc() {
$whatever = esc_html($_POST['whatever']);
echo 'It works: '.$whatever;
exit; // This is required to end AJAX requests properly.
}
// Inline JavaScript
add_action('wp_footer', 'my_inline_js');
function my_inline_js() { ?>
<script>
// Set the "ajax_url" variable available globally
ajax_url = "<?php echo admin_url('admin-ajax.php'); ?>";
// Make your AJAX request on document ready:
(function ($) {
$(document).ready(function () {
var my_data = {
action: 'mycustomfunc', // This is required so WordPress knows which func to use
whatever: "yes it is" // Post any variables you want here
};
$.post(ajax_url, my_data, function(response) { // This will make an AJAX request upon page load
alert('Got this from the server: ' + response);
});
});
})(jQuery);
</script>
<?php
}
Note: For the ajax_url
part, you could use wp_localize_script()
instead of setting it manually, but it is less flexible as it requires to specify an existing enqueued script, which you might not have.
Note: Also, for outputting inline JavaScript manually into a page, the wp_footer
hook is the correct one to use. If using wp_localize_script()
then you would use the wp_enqueue_scripts
hook instead.
这篇关于有道链接阿贾克斯网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!