I'm trying to do the folllowing.

I have a button in my template (in templates folder of my plugin). When they press the button I want to call a function and send and id to the function.

In that function I want to create an api call and send the results back to the template. How can I do this?


Based on the limited detail of your question something as per the following is a rough gist of what you could do.

In your theme functions.php file:


function handle_request () {

    if (isset($_GET['custom_id']) && isset($_GET['custom_id_nonce']) ) {

        if ( wp_verify_nonce($_GET['custom_id_nonce'], 'custom_id_action') ) {
            $id = $_GET['custom_id'];

            // FIRST
            // do something with $id

            // THEN
            // possibly redirect somewhere else or back to referrer 
            // e.g. wp_safe_redirect( wp_get_referer() );
            // see https://codex.wordpress.org/Function_Reference/wp_get_referer
            // see https://codex.wordpress.org/Function_Reference/wp_safe_redirect

        } else {
            // handle failure state, nonce value is incorrect...


    // if here, the request was likely not for you


add_action( 'init', 'handle_request' );

In your theme template file:

<a href="<?php echo wp_nonce_url( home_url('?custom-id=123'), 'custom_id_action', 'custom_id_nonce' );?>">CLICK ME</a>

In the above example, where home_url('?custom-id=123') is stated, you may want to change the basis for this URL to be the current URL the user is on.

Important reading:

| improve this answer | |

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.