admin管理员组文章数量:1414908
I tried to send data from my js to a php function via wp_ajax. All the data is being sent correctly as I can echo it back.
However, now I need to pass the results to another add_action hook. What is the best way to do this? What i'm doing isn't working.
Here's what I have so far:
My ajax send:
jQuery.ajax({
type: 'post',
url: customizerValues.ajax_url,
data: {
action: 'ar_update_plan',
nonce: wpApiSettings.nonce,
plan_id: planId,
blog_id: site_id,
},
success: function(response) {
console.log(response);
}
});
My php function
add_action( 'wp_ajax_ar_update_plan', 'ar_update_plan' );
function ar_update_plan($blog_id, $new_expire, $level){
if( isset($_REQUEST['blog_id'])) {
$blog_id = $_REQUEST['blog_id'];
if( isset($_REQUEST['plan_id'])) {
$level = $_REQUEST['plan_id'];
// THE HOOK I NEED TO PASS THE RESULTS TO
add_action('psts_extend', $blog_id, $new_expire, $new_plan_id);
// just making sure the ajax data came through
echo $_REQUEST['plan_id'];
wp_die(); // ajax call must die to avoid trailing 0 in your response
}
}
}
I tried to send data from my js to a php function via wp_ajax. All the data is being sent correctly as I can echo it back.
However, now I need to pass the results to another add_action hook. What is the best way to do this? What i'm doing isn't working.
Here's what I have so far:
My ajax send:
jQuery.ajax({
type: 'post',
url: customizerValues.ajax_url,
data: {
action: 'ar_update_plan',
nonce: wpApiSettings.nonce,
plan_id: planId,
blog_id: site_id,
},
success: function(response) {
console.log(response);
}
});
My php function
add_action( 'wp_ajax_ar_update_plan', 'ar_update_plan' );
function ar_update_plan($blog_id, $new_expire, $level){
if( isset($_REQUEST['blog_id'])) {
$blog_id = $_REQUEST['blog_id'];
if( isset($_REQUEST['plan_id'])) {
$level = $_REQUEST['plan_id'];
// THE HOOK I NEED TO PASS THE RESULTS TO
add_action('psts_extend', $blog_id, $new_expire, $new_plan_id);
// just making sure the ajax data came through
echo $_REQUEST['plan_id'];
wp_die(); // ajax call must die to avoid trailing 0 in your response
}
}
}
Share
Improve this question
asked Sep 23, 2016 at 20:31
friendlyfirefriendlyfire
1051 silver badge11 bronze badges
1 Answer
Reset to default 3I guess you have a small misunderstanding here (a slight inconsistency only) add_action
is to register a callback for a hook-name, you actually want to fire that hook (not register it).
You can fire a hook with the do_action
function.
The Codex-page of the add_action
function outlines both:
- https://developer.wordpress/reference/functions/add_action/
And naturally the do_action
Codex-page has all the glory details:
- https://developer.wordpress/reference/functions/do_action/
Take care you don't create an endless loop, that will crash PHP and therefore Wordpress but it also shows that there are no limitations built in.
You can stack as many actions as you want to, even recursive action calls are possible.
本文标签: ajaxCan I fire an addaction hook inside of a function that recieves data via wpajax
版权声明:本文标题:ajax - Can I fire an add_action hook inside of a function that recieves data via wp_ajax? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745167636a2645778.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论