plugins – wp_post not working, keeps redirecting to posts page

Question

I’ve been on this for more than two weeks, I’ve looked up a lot resources, I keep getting the same results; my site redirecting to posts page without executing anything in the defined function.
I’m trying to get the value of $_POST data with admin_post hook. Below is the hook and the function

add_action( 'admin_post_nopriv_add_foobar', 'prefix_admin_add_foobar' );
function prefix_admin_add_foobar() {
    status_header(200);
    $mydata = $_POST['data'];
    echo '<script>
        console.log('.$mydata.');
    </script>';
    //request handlers should exit() when they complete their task
    wp_redirect('somewhereelse.php');
    exit();
}

and below is the form

<form action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" method="post">
   <input type="hidden" name="action" value="add_foobar">
   <input type="hidden" name="data" value="foobarid">
   <input type="submit" value="Submit">
</form>

I’m testing this on wamp localhost, I don’t know if it might be a factor.
I’m also not sure if I should add the hook and its function in function.php or inside the page template file.

0
Stephynoe 2 weeks 2021-09-02T00:01:42-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse