我发现init hook被调用了两次,然后我尝试跟踪并找出wp-settings.php
也被打了两次电话。我本来想发一个问题来找出原因。在一些代码注入测试之后。我找到了原因,希望在这里与大家分享,看看是否有人有其他见解、方法或解决方法可以避免;双重呼叫;。
以下是找出的测试方法,可能还有其他的可能性和方法。任何在详细性能调整方面有类似想法的人都可以看看。
我发现init hook被调用了两次,然后我尝试跟踪并找出wp-settings.php
也被打了两次电话。我本来想发一个问题来找出原因。在一些代码注入测试之后。我找到了原因,希望在这里与大家分享,看看是否有人有其他见解、方法或解决方法可以避免;双重呼叫;。
以下是找出的测试方法,可能还有其他的可能性和方法。任何在详细性能调整方面有类似想法的人都可以看看。
进行以下测试以找出原因。因为测试将触及核心源代码。只有当你觉得舒服的时候才去做。
注意:如果只是简单地将var\\u dump输出到屏幕上。它可能只显示一条消息,就好像它只运行一次一样(但实际上它被调用了两次)。记录并输出到文件显示更多信息。
// output content to a file which shows clearly how much time is run
$log_location = \'/volumes/ram/test-internal.log\';
// because debug_backtrace() give an object, put the string result to buffer and write to log file
ob_start();
print_r(debug_backtrace()); //
$trace = ob_get_contents();
ob_end_clean();
// output
file_put_contents( $log_location, \'wp-settings\' . "\\n", FILE_APPEND);
file_put_contents( $log_location, $trace . "\\n", FILE_APPEND);
文件需要跟踪结果Array
(
[0] => Array
(
[file] => /dev/wp-config.php
[line] => 108
[function] => require_once
)
[1] => Array
(
[file] => /dev/wp-load.php
[line] => 37
[args] => Array
(
[0] => /dev/wp-config.php
)
[function] => require_once
)
[2] => Array
(
[file] => /dev/wp-blog-header.php
[line] => 13
[args] => Array
(
[0] => /dev/wp-load.php
)
[function] => require_once
)
[3] => Array
(
[file] => /dev/index.php
[line] => 17
[args] => Array
(
[0] => /dev/wp-blog-header.php
)
[function] => require
)
)
根据跟踪日志,它显示wp-settings.php
被呼叫两次,呼叫方wp负载。php->;需要wp设置。php博客标题。php->;需要wp负载。php,因此它复制了wp设置中所有内容的加载。php分别为。自init
挂钩在wp设置内。因此,即使没有ajax调用,init钩子也至少被调用了两次。我认为在将来,如果可能的话,如果对它们进行优化和改造,使其只加载一次,这将是理想的,并且对性能有好处。如果ajax检查不可用,则在每个心跳中都会调用init钩子的init操作。
add_action( \'init\', \'run_something\' );
function run_something() {
// if not desired to be called by ajax heartbeat
// just an illustrative example, further detailed testing is needed on developer\'s own need
if( wp_doing_ajax() ) {
return;
}
// any work
}
我试图通过ajax发送一个文件,但收到一个400错误请求。我的代码data.coverQuestions.medicalMalpractice.file = docFile.prop(\'files\')[0]; $.ajax({ url: \'/wp-admin/admin-ajax.php\', method: \'post\', data: { action: \'insurance_form_data\', data, },