Using WordPress ‘redirect_post()’ PHP function

The redirect_post() WordPress PHP function redirects users to the previous page.

Usage

redirect_post( $post_id );

Parameters

  • $post_id (int, optional) – The Post ID to redirect to. Default: ''.

More information

See WordPress Developer Resources: redirect_post()

Examples

Redirect to the previous page after a form submission

After a user submits a form, you can use the redirect_post() function to redirect them back to the previous page.

// Form processing logic here...

// Redirect the user back to the previous page
redirect_post( $post_id );

Redirect to a specific post

To redirect users to a specific post after an action is completed, you can pass the post ID to the redirect_post() function.

// Action completion logic here...

// Redirect the user to a specific post with ID 42
$post_id = 42;
redirect_post( $post_id );

Redirect after a custom post type submission

You can use the redirect_post() function to redirect users back to the previous page after submitting a custom post type form.

// Custom post type form processing logic here...

// Redirect the user back to the previous page
redirect_post( $post_id );

Redirect after comment submission

Redirect users back to the previous page after submitting a comment using the redirect_post() function.

// Comment form processing logic here...

// Redirect the user back to the previous page
redirect_post( $post_id );

Redirect after user registration

After a user successfully registers on your site, you can use the redirect_post() function to redirect them to a specific post.

// User registration logic here...

// Redirect the user to a specific post with ID 55
$post_id = 55;
redirect_post( $post_id );