Using wp_redirect to Redirect Users to a New Post When Published in WordPress

Home » Snippets » Using wp_redirect to Redirect Users to a New Post When Published in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Do you want to automatically redirect users to a new post once it’s published on your WordPress site? This could be a very useful tool in increasing engagement and ensuring the new content is promptly viewed by your target audience. The wp_redirect function in WordPress allows you to achieve exactly that. In this article, we’re going to guide you through the process of using wp_redirect to automatically navigate users to a newly published post.

					function wpturbo_redirect_to_new_post($post_id){
  $post = get_post($post_id);
  if($post->post_status == 'publish'){
    $url = get_permalink($post_id);
    wp_redirect($url);
    exit();
  }
}
add_action('save_post', 'wpturbo_redirect_to_new_post');
				

The function wpturbo_redirect_to_new_post($post_id) in the provided code snippet is executed when a post is being saved. The function uses the unique $post_id parameter that is passed to it as part of o the ‘save_post’ action hook.

Here’s a breakdown of what each line of code does:

$post = get_post($post_id); This line retrieves the post object for the post that is currently being saved using the WordPress built-in function get_post(). This function returns an object that contains all information about the post, such as the post title, content, status, and more. The parameter $post_id is passed to the get_post() function to specify which post’s data we want to fetch.

if($post->post_status == 'publish') We use an if statement to check whether the status of the current post is ‘publish’. The status of the post can be retrieved from the post object that we got in the previous step using the post object’s attribute ‘post_status’. If the post’s status is ‘publish’, this means that the post has been saved in the WordPress database and is now ready to be displayed to users. If the post’s status is not ‘publish’, the function will terminate at this point, and no redirection will happen.

$url = get_permalink($post_id); This line gets the URL of the post that is currently being saved. This is done using the WordPress function get_permalink(), which returns the permanent URL for a specified post. We pass the $post_id parameter to this function in order to get the correct URL for the current post.

wp_redirect($url); exit(); Then we use the WordPress function wp_redirect() to send the user to the URL of the new post. This function performs a redirect to the specified URL. We pass the $url parameter, which contains the URL of the current post, to this function. It should be noted that exit(); is called after wp_redirect(). This is a standard PHP function which halts the execution of the script.

Finally, we hook our function wpturbo_redirect_to_new_post() to the ‘save_post’ action, which triggers every time a post is being saved. This ensures our function will be executed each time a post is published.

add_action('save_post', 'wpturbo_redirect_to_new_post'); By using the WordPress action hook ‘save_post’ during the saving process, our function wpturbo_redirect_to_new_post() can be initiated effectively.

Register an account to save your snippets or go Pro to get more features.