为什么要使用admin-ajax.php,它如何工作?
-
-
值得注意的是,任何答案都将讨论为什么应将`themes/example/json.php`视为主要的安全漏洞.Of note, any answer would talk about why `themes/example/json.php` should be considered a major security vulnerability
- 2
- 2015-06-10
- Tom J Nowell
-
2 个回答
- 投票数
-
- 2015-06-10
1)为什么使用
admin-ajax.php
而不是在单独的json中编码 像themes/example/json.php
这样的文件,并在那里编码您的数据?使用
admin-ajax.php
意味着WordPress核心已加载并可用.否则,您将需要手动加载所需的文件,这是一个复杂的过程,如果您不太了解Core,则很容易失败.而且,您对Javascript安全性有多好?2)
admin-ajax.php
如何工作?我对此并不了解 文件.它会加载所有功能,以便您准备使用它们吗?- 它会加载WordPress核心,这意味着您可以使用
$wpdb
之类的东西 和$WP_Query
.那是通过第25行. - 它发送一些头文件-行37-41.
- 内容类型标题
- 标头,告诉浏览器不要缓存结果
- 有趣的标头是由
send_nosniff_headers()
发送的标头
- 和
nocache_headers()
.
-
admin_init
钩子触发. - 核心动作是动态定义和注册的-第46-73行.
除非需要,否则不会注册这些文件-即除非
通过
$_GET
或$_POST
请求它们. - 触发"心跳" API钩子-第75行
- 已检查请求用户的"已登录"状态,并且 触发了适当的管理或"无特权"挂钩.
项目#1和#6是使用AJAX API的主要原因.您拥有几乎肯定需要的WordPress Core,并且具有与其余WordPress相同的登录安全系统.
1) Why use
admin-ajax.php
instead of encoding your json in a separate file likethemes/example/json.php
and encode your data there?Using
admin-ajax.php
means that the WordPress Core is loaded and available. WIthout that, you would need to hand load the files you need, which is a complicated process and prone to failure if you don't know the Core very, very well. And, how good are you with Javascript security?2) How does
admin-ajax.php
work? I don't understand much from that file. Does it load all the functions so you are ready to use them?- It loads the WordPress Core, meaning you can use things like
$wpdb
and$WP_Query
. That is through about line 25. - It sends a few headers-- lines 37 - 41.
- A content type header
- A header to tell browsers not to cache the results
- The interesting headers are those sent by
send_nosniff_headers()
- and
nocache_headers()
.
- The
admin_init
hook fires. - Core actions are defined and registered dynamically-- lines 46 - 73.
These won't be registered unless they are needed-- that is, unless
they are requested via
$_GET
or$_POST
. - The "heartbeat" API hook fires-- line 75
- The "logged in" status of the requesting user is checked and the appropriate administrative or "no priviledge" hook is fired.
Items #1 and #6 are the primary reasons to use the AJAX API, in my opinion. You have the WordPress Core, which you almost certainly need, and you have the same login security system as with the rest of WordPress.
-
- 2015-06-10
admin-ajax.php
是WordPress AJAX API 的一部分,是的,它确实可以处理来自后端和前端的请求. 在这里,我想出了您的问题:2)admin-ajax.php如何工作?
对于逻辑,您可以访问这里.
这假定您已经知道如何使JavaScript入队等.
JavaScript片段:
jQuery(document).ready(function($) { // We'll pass this variable to the PHP function example_ajax_request var fruit = 'Banana'; // This does the ajax request $.ajax({ url: ajaxurl, data: { 'action':'example_ajax_request', 'fruit' : fruit }, success:function(data) { // This outputs the result of the ajax request console.log(data); }, error: function(errorThrown){ console.log(errorThrown); } }); });
PHP片段:
function example_ajax_request() { // The $_REQUEST contains all the data sent via ajax if ( isset($_REQUEST) ) { $fruit = $_REQUEST['fruit']; // Let's take the data that was sent and do something with it if ( $fruit == 'Banana' ) { $fruit = 'Apple'; } // Now we'll return it to the javascript function // Anything outputted will be returned in the response echo $fruit; // If you're debugging, it might be useful to see what was sent in the $_REQUEST // print_r($_REQUEST); } // Always die in functions echoing ajax content die(); } add_action( 'wp_ajax_example_ajax_request', 'example_ajax_request' ); // If you wanted to also use the function for non-logged in users (in a theme for example) add_action( 'wp_ajax_nopriv_example_ajax_request', 'example_ajax_request' );
1)为什么使用admin-ajax.php而不是用单独的json编码 像themes/example/json.php这样的文件,并在那里编码您的数据?
admin-ajax.php
is part of the WordPress AJAX API, and yes, it does handle requests from both backend and front. here what i figure-out for your question that is:2) How does admin-ajax.php work?
for the logic you can visit here.
This assumes you already know how to enqueue JavaScript, etc.
JavaScript Piece:
jQuery(document).ready(function($) { // We'll pass this variable to the PHP function example_ajax_request var fruit = 'Banana'; // This does the ajax request $.ajax({ url: ajaxurl, data: { 'action':'example_ajax_request', 'fruit' : fruit }, success:function(data) { // This outputs the result of the ajax request console.log(data); }, error: function(errorThrown){ console.log(errorThrown); } }); });
PHP Piece:
function example_ajax_request() { // The $_REQUEST contains all the data sent via ajax if ( isset($_REQUEST) ) { $fruit = $_REQUEST['fruit']; // Let's take the data that was sent and do something with it if ( $fruit == 'Banana' ) { $fruit = 'Apple'; } // Now we'll return it to the javascript function // Anything outputted will be returned in the response echo $fruit; // If you're debugging, it might be useful to see what was sent in the $_REQUEST // print_r($_REQUEST); } // Always die in functions echoing ajax content die(); } add_action( 'wp_ajax_example_ajax_request', 'example_ajax_request' ); // If you wanted to also use the function for non-logged in users (in a theme for example) add_action( 'wp_ajax_nopriv_example_ajax_request', 'example_ajax_request' );
1) Why use admin-ajax.php instead of encoding your json in a separate file like themes/example/json.php and encode your data there?
may be this helpful.admin-ajax.php vs Custom Page Template for Ajax Requests
-
嘿,您能解释一下这些动作挂钩'wp_ajax_example_ajax_request'和'wp_ajax_nopriv_example_ajax_request'我在任何地方都找不到解释.另外,ajaxurl可以解决什么问题?谢谢Hey, can you explain these action hooks 'wp_ajax_example_ajax_request' and 'wp_ajax_nopriv_example_ajax_request' I find no explanation anywhere. Also what does ajaxurl resolve to? Thanks
- 0
- 2020-04-13
- David Okwii
我对json数据的ajax调用像这样正常工作 functions.php:
javascript:
我有2个问题.
1)为什么使用admin-ajax.php而不是将json编码在单独的文件中,例如
themes/example/json.php
并在那里编码数据?2)admin-ajax.php如何工作?我对那个文件不太了解.它会加载所有功能,以便您准备使用它们吗?
谢谢!