How To Redirect User into Random Post


Sponsored Links




Like we all know, WordPress is powerful publishing platform. Now everywhere is WordPress, blogs, portal, e-commerce and many more running on WordPress. WordPress is amazing and multi user. Now we will show you how to redirect user into random post in WordPress.

Raikage and Tsunade - WP Tips

Raikage and Tsunade - WP Tips

We only needed tiny script that we need to put on functions.php. Mostly on newest wordpress theme already have functions.php on the theme folder. So open that file and added this code

add_action('init','random_add_rewrite');
function random_add_rewrite() {
       global $wp;
       $wp->add_query_var('random');
       add_rewrite_rule('random/?$', 'index.php?random=1', 'top');
}

add_action('template_redirect','random_template');
function random_template() {
       if (get_query_var('random') == 1) {
               $posts = get_posts('post_type=post&orderby=rand&numberposts=1');
               foreach($posts as $post) {
                       $link = get_permalink($post);
               }
               wp_redirect($link,307);
               exit;
       }
}


That’s it. Now you have done with most important step on this tutorial. Now if you want enable this is super easy. Create new link that link to http://yourdomain.com/random and when this link click user will get random content from your blog post. That easy right? Thanks for WP Beginner to share this tricks.

Please notest if you are using W3 Total Cache you need to add this code on the exclude list.

/random/
/index.php?random=1

Subscribe to Full RSS Feed

RSS FeedIf you thing this article useful, please consider subscribing to our RSS Feed or e-mail updates and stay updated with us. You can also follow @WPTricksNet on twitter for latest updates.

About WP Tricks

We are WordPress Family, we collected and writed useful tips and tricks for you WordPress. We share it for you...

Related Tips and Trick from Local Data and on The Net

How to redirect users to a random post | Fresh News - Blog Design

loop foreach ( $my_random_post as $post ) { // redirect the user to the random post wp_redirect A Foot On The Bottom Rung: First Forays Into Responsive Web Read More

Redirect after POST behavior in LWP::UserAgent differs from

Create a new user; The Monastery Gates; Super Search Netscape, thus, transforms the POST request into a GET Comment on Redirect after POST behavior in LWP::UserAgent Read More

URL redirection - Wikipedia, the free encyclopedia

For example, a redirect might take a user to a site that would attempt to trick them into scripting language to redirect the user's Random article; Donate to Read More

Redirecting Users to Another Page

ways for you to build redirection into your default, buttons in an ASP.NET Web page post page using the capabilities of the user's browser. In a browser redirect, the Read More

Google Redirect and Random Audio Problems - MajorGeeks Support Forums

Google Redirect and Random Audio Problems Malware Then reboot and see if you can log into the problem user Last Post: Google Redirect: lifelovedat: Malware Removal Read More


Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>