Add Snippet To Project
Ever thought about setting up your WordPress site to redirect users from the home page directly to a random blog post? This can serve as a fun and engaging way to draw visitors deeper into your site and have them engage with content they might not otherwise find. In this article, we’ll be guiding you through the steps of how to implement this feature smoothly and effectively, keeping your readers intrigued every time they visit your site.
function wpturbo_redirect_home_to_random_post() {
if (is_home() || is_front_page()) {
$args = array(
'posts_per_page' => 1,
'orderby' => 'rand',
);
$random_post = get_posts($args);
foreach ($random_post as $post) {
wp_redirect(get_permalink($post->ID), 301);
exit();
}
}
}
add_action('template_redirect', 'wpturbo_redirect_home_to_random_post');
This code snippet introduces a new function: wpturbo_redirect_home_to_random_post()
. The purpose of this function is to detect when a user lands on the home page or front page of a WordPress website and reroute them to a random blog post.
function wpturbo_redirect_home_to_random_post() {
if (is_home() || is_front_page()) {
The function starts with an if condition that checks if the current page is the home or the front page of the website. This condition relies on two built-in WordPress functions: is_home()
and is_front_page()
. Both of these functions return a boolean (true or false) depending on whether the current page is the home page or front page.
$args = array(
'posts_per_page' => 1,
'orderby' => 'rand',
);
$random_post = get_posts($args);
If the condition is satisfied, this snippet proceeds by setting up an arguments array for the get_posts
function. It sets ‘posts_per_page’ to 1
and ‘orderby’ to 'rand'
, which indicates that we want to retrieve a single random post.
The get_posts
function is a built-in WordPress function that retrieves an array of posts matching the supplied parameters. By supplying our argument array to this function, we are returned an array containing a single random post.
foreach ($random_post as $post) {
wp_redirect(get_permalink($post->ID), 301);
exit();
}
Next, a foreach
loop iterates over the random_post
array. Inside the loop, the wp_redirect()
function takes the permalink of the randomly selected post and issues a 301 redirect to it.
The get_permalink()
function fetches the URL of the supplied post ID. The exit();
function then terminates script execution once the redirection has been set up.
The final piece of this code snippet is where the function wpturbo_redirect_home_to_random_post()
is hooked into the ‘template_redirect’ action.
add_action('template_redirect', 'wpturbo_redirect_home_to_random_post');
The add_action()
function is a WordPress hook function that ‘attaches’ our function to a specific action. In this case, template_redirect
is an action that run just before WordPress determines which template file to load. This is the perfect timing to redirect the user without loading unnecessary templates or making irrelevant database queries.