这一定很简单,但我无法弄清楚。
主题具有更新用户配置文件的 ajax 操作功能。这是代码:
add_action( 'wp_ajax_nopriv_update_profile', 'update_profile' );
add_action( 'wp_ajax_update_profile', 'update_profile' );
if( !function_exists('update_profile') ):
function update_profile(){
//update profile discipline
......
echo json_encode( array( 'success' => true) );
die();
}
endif;
我正在开发一个插件,它将主题的功能扩展到rest api。现在我想在我的插件中使用上述操作,并在它返回后做一些事情。但由于它已经die()
结束,我无法在行动后继续执行。这是我在插件中调用它的方式:
add_action( 'rest_api_init', function () {
register_rest_route( 'mobile-api/v1', '/update-profile', array(
'methods' => 'POST',
'callback' => 'editProfile',
));
});
function editProfile() {
do_action("wp_ajax_update_profile");
$response = array(); //<---- this and next doesn't get executed
$response["works"] = "Voila!";
wp_send_json($response, 200);
}
我已经尝试过ob_get_contents()
,但它不会超过 die 方法。
编辑主题不是一种选择。
如何避免die()
from 主题操作并返回我自己的响应?
或者在 PHP 代码中模拟 ajax 请求的最佳方法是什么。
原因:原动作有BUG。我想通过撤消一件事来纠正,那是在行动中完成的。