AJAX调用期间是否曾经调用过functions.php文件?调试AJAX
2 个回答
- 投票数
-
- 2013-04-20
admin-ajax.php
加载wp-load.php
:/** Load WordPress Bootstrap */ require_once( dirname( dirname( __FILE__ ) ) . '/wp-load.php' );
wp-load.php
会加载wp-config.php
,然后会加载wp-settings.php
.在这里我们找到这个:
// Load the functions for the active theme, for both parent and child theme if applicable. if ( ! defined( 'WP_INSTALLING' ) || 'wp-activate.php' === $pagenow ) { if ( TEMPLATEPATH !== STYLESHEETPATH && file_exists( STYLESHEETPATH . '/functions.php' ) ) include( STYLESHEETPATH . '/functions.php' ); if ( file_exists( TEMPLATEPATH . '/functions.php' ) ) include( TEMPLATEPATH . '/functions.php' ); }
所以,是的,主题的
functions.php
已加载.
wp-settings.php
中有一个例外:// Stop most of WordPress from being loaded if we just want the basics. if ( SHORTINIT ) return false;
如果先前将
SHORTINIT
定义为TRUE
,则不会加载主题.因此请出于某种原因检查
SHORTINIT
是否为TRUE
.
另一个常见错误是
is_admin()
的错误使用.在admin-ajax.php
中始终为TRUE
,因此以下操作将失败:if ( ! is_admin() ) // register or execute AJAX stuff
调试AJAX
一种有效的原始方法是使用HTTP标头调试AJAX.
这是一个简单的辅助函数:
function send_debug_header( $msg ) { static $counter = 1; header( "X-Debug-Ajax-$counter: $msg" ); $counter += 1; }
此插件显示了如何使用它:
<?php # -*- coding: utf-8 -*- /** * Plugin Name: Debug AJAX per HTTP * Description: Look at the HTTP headers in your browser's network console */ // The constant is already defined when plugins are loaded. // Prove we have been called. if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) send_debug_header( 'File "' . __FILE__ . '" was called on an AJAX request.' ); function send_debug_header( $msg ) { static $counter = 1; header( "X-Debug-Ajax-$counter: $msg" ); $counter += 1; } add_action( 'wp_ajax_debug_test', 't5_debug_test' ); add_action( 'wp_ajax_nopriv_debug_test', 't5_debug_test' ); function t5_debug_test() { $in = is_user_logged_in() ? '' : 'not '; send_debug_header( 'Function "' . __FUNCTION__ . '" was called and the user is ' . $in . 'logged in.' ); print_r( debug_backtrace() ); die(1); } add_action( 'wp_enqueue_scripts', 't5_enqueue_jquery' ); function t5_enqueue_jquery() { wp_enqueue_script( 'jquery' ); } add_action( 'wp_footer', 't5_debug_ajax_test_button', 0 ); function t5_debug_ajax_test_button() { ?> <input type="submit" id="t5debugajax" value="Debug AJAX"> <script> jQuery( function($){ var sendFeedBack = function( response ){ console.log( response ); }; $("#t5debugajax").on("click", function(){ $.post( "<?php echo admin_url( 'admin-ajax.php' ); ?>", { action: "debug_test" }, sendFeedBack ); }); }); </script> <?php }
它将在前端添加一个按钮,单击该按钮会触发AJAX请求.打开浏览器的网络控制台,然后查看请求的响应标头:
admin-ajax.php
loadswp-load.php
:/** Load WordPress Bootstrap */ require_once( dirname( dirname( __FILE__ ) ) . '/wp-load.php' );
wp-load.php
loadswp-config.php
, and therewp-settings.php
is loaded.And here we find this:
// Load the functions for the active theme, for both parent and child theme if applicable. if ( ! defined( 'WP_INSTALLING' ) || 'wp-activate.php' === $pagenow ) { if ( TEMPLATEPATH !== STYLESHEETPATH && file_exists( STYLESHEETPATH . '/functions.php' ) ) include( STYLESHEETPATH . '/functions.php' ); if ( file_exists( TEMPLATEPATH . '/functions.php' ) ) include( TEMPLATEPATH . '/functions.php' ); }
So, yes, the theme’s
functions.php
is loaded.
There is one exception in
wp-settings.php
:// Stop most of WordPress from being loaded if we just want the basics. if ( SHORTINIT ) return false;
When
SHORTINIT
is defined asTRUE
earlier, the theme will not be loaded.So check if
SHORTINIT
isTRUE
for some reason.
Another common error is the wrong usage of
is_admin()
. This is alwaysTRUE
inadmin-ajax.php
, so the following will fail:if ( ! is_admin() ) // register or execute AJAX stuff
Debugging AJAX
One method as primitive as efficient is using HTTP header to debug AJAX.
Here is a simple helper function:
function send_debug_header( $msg ) { static $counter = 1; header( "X-Debug-Ajax-$counter: $msg" ); $counter += 1; }
And this plugin shows how to use it:
<?php # -*- coding: utf-8 -*- /** * Plugin Name: Debug AJAX per HTTP * Description: Look at the HTTP headers in your browser's network console */ // The constant is already defined when plugins are loaded. // Prove we have been called. if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) send_debug_header( 'File "' . __FILE__ . '" was called on an AJAX request.' ); function send_debug_header( $msg ) { static $counter = 1; header( "X-Debug-Ajax-$counter: $msg" ); $counter += 1; } add_action( 'wp_ajax_debug_test', 't5_debug_test' ); add_action( 'wp_ajax_nopriv_debug_test', 't5_debug_test' ); function t5_debug_test() { $in = is_user_logged_in() ? '' : 'not '; send_debug_header( 'Function "' . __FUNCTION__ . '" was called and the user is ' . $in . 'logged in.' ); print_r( debug_backtrace() ); die(1); } add_action( 'wp_enqueue_scripts', 't5_enqueue_jquery' ); function t5_enqueue_jquery() { wp_enqueue_script( 'jquery' ); } add_action( 'wp_footer', 't5_debug_ajax_test_button', 0 ); function t5_debug_ajax_test_button() { ?> <input type="submit" id="t5debugajax" value="Debug AJAX"> <script> jQuery( function($){ var sendFeedBack = function( response ){ console.log( response ); }; $("#t5debugajax").on("click", function(){ $.post( "<?php echo admin_url( 'admin-ajax.php' ); ?>", { action: "debug_test" }, sendFeedBack ); }); }); </script> <?php }
It will add a button to the front end that triggers an AJAX request when clicked. The open your browser’s network console and look at the response headers for the request:
-
与以往一样详细,@toscho.当代码在您的端上运行正常但对其他人不行时,尤其难以调试.似乎无法重现问题,但您的回答可能会向我指示正确的方向.As detailed as ever, @toscho. Its especially hard to debug when the code runs fine on your end but not for someone else. Can't seem to reproduce the problem but your answer will probably send me in the right direction.
- 0
- 2013-04-20
- Manny Fleurmond
-
@MannyFleurmond我添加了一个调试助手插件.那应该有助于发现问题.@MannyFleurmond I have added a debug helper plugin. That should help finding the problem.
- 0
- 2013-04-20
- fuxia
-
伙计,你很彻底:)Man, you are thorough :)
- 9
- 2013-04-20
- Manny Fleurmond
-
`TEMPLATEPATH`?;)`TEMPLATEPATH`? ;)
- 0
- 2013-07-14
- kaiser
-
- 2013-05-20
我认为如果您的帐户已登录而AJAX仍在工作,而未处于注销状态,对吗?
WordPress中有一个功能可以为未登录的用户访问基于AJAX的文件:例如,wp_ajax_nopriv
/* works for logged users */ add_action( 'wp_ajax_my_action', 'my_action_callback'); /* works for non logged users */ add_action( 'wp_ajax_nopriv_my_action', 'my_action_callback');
I assume your problem was AJAX was working if your are logged in and it was not working in logged out status, right?
There is a function in WordPress to access AJAX-based files for non logged users:wp_ajax_nopriv
, for example/* works for logged users */ add_action( 'wp_ajax_my_action', 'my_action_callback'); /* works for non logged users */ add_action( 'wp_ajax_nopriv_my_action', 'my_action_callback');
试图找出程序员遇到的一个问题.我想知道当您执行管理端AJAX时是否会完全调用
functions.php
文件?我知道当您进行AJAX调用时,会加载一部分WP以处理该调用并发送回响应.其中是否包含functions.php
文件?我问的原因是因为他正在使用Meta-Box`插件中的类,并将其作为主题的一部分进行加载.该类中有一些AJAX仅返回空响应,我认为是因为处理响应的代码未加载.WP处理AJAX时加载的内容是否有任何文档?