1

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 
        }
    }
}
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:

And naturally the do_action Codex-page has all the glory details:

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.

  • 1
    I changed it to do_action but nothing happens when I trigger the javascript that passes data to this hook. I'm confident it's the right hook for the plugin I'm trying to pass data to. Any guesses why it might not be working? – friendlyfire Sep 23 '16 at 21:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.