Ali Elkhaiat
Ali Elkhaiat

Reputation: 163

Redirect to same page after form submit in WordPress Plugin

I wrote my own WordPress Plugin and I am trying to just refresh the page after form submit.

My code looks like this:

<form method="post" action="<?php echo get_admin_url();?>admin-post.php">
    <input type="hidden" name="action" value="send_nl">
    <input type="text" name="nl_title" placeholder="Title"> 
    <textarea type="text" name="nl_text" placeholder="Message"></textarea>
    <input type="submit" name="submit_nl">  
</form>

If I submit the form everything works fine. I get an Email with the right data, but I get redirected to a white page: to https://www.myurl.com/wp-admin/admin-post.php! Now I would need a hook or something similar to redirect to my plugin page again.

I searched a lot, but I couldn't find the right solution - god knows why.

Thank you.

Upvotes: 1

Views: 8964

Answers (1)

bueltge
bueltge

Reputation: 2402

After you have send, save your date you should the function wp_safe_redirect() from the WP API to redirect to your page. A source example below.

wp_safe_redirect(
    // Sanitize.
    esc_url(
        // Retrieves the site url for the current site.
        site_url( '/wp-admin/admin-post.php' )
    )
);
exit();

You see this usage here in this repo in a function to import data from a json string in the database in the full context.

Upvotes: 2

Related Questions