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
Add a comment  | 

1 Answer 1

Reset to default 3

I 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