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 Blog to First Post

Today Tricks is How To Redirect Blog to First Post. This code original writen by Ashton Sanders. You need to put this code on your index.php or home.php to make it work. It just uses the WordPress Redirect function to … Read More

How To Extend User Contact Info

WordPress 2.9 built with new ability to Extend User Contact Info, this tricks can be done by added some code on functions.php in your current theme. We can added many contact info, depend on your needed. To do extend user … Read More

Tutorial Enable HTML markup in user profiles

User profiles in WordPress is the perfect ways to display our information, such as website, Yahoo Messenger and many more. You can add some description about yourself on WordPress Profiles too. The problem is, default WordPress Profiles descriptio

The simple way to show guest author name in your post

When your blog has many content writer, you might consider put their information into their own posts. You need to put some code in your index.php or else (like loop.php, home.php) as the main page and inside your single page. … Read More

How To Add WP Login Link to Your Navigation Menu Automatically

I will show you easier tutorial add WP login Link to Your Navigation Menu Automatically. We only needed to some function to our functions.php and WP Login link will be automatically added into our WordPress Navigation Menu. Description About wp_

How To Limit Post Title Words on WordPress

WordPress and SEO is the perfect match so far, easy to develop, easy to use and give high impact on SEO. Like we all know, many search engine only limited 160 character to be included the indexing. Now we will … Read More

The Best Daily WordPress Tricks & Tips

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 read more

Source: http://wptricks.net/

How to Redirect Users to a Random Post in WordPress

Short snippet that redirect users to a random post in When a user clicks on this button, they are taken to a random post on a site. Few bugs we ran into: If you read more

Source: http://www.wpbeginner.com/wp-tutorials/how-to-redirect-users-to-a-random-post-in-wordpress/

WP Tricks Authors / WordPress Tricks & Tips

How To Redirect User into Random Post; Test New Post to Fix Bugs; How To Create Fancy Highlight search-result; WordPress 3.2.1 has been Released; Download bbPress 2.0 Beta 2 read more

Source: http://wptricks.net/author/admin/

Debugging: Random Redirect and WP Super Cache — Ikiru Design

Random Redirect function relies on. So the user random() ?>">a random post This solution works, but it’s got a problem. If I were to click the redirect version into read more

Source: http://www.ikirudesign.com/2008/09/13/debugging-random-redirect-and-wp-super-cache/

LWP::UserAgent subclass to make it follow redirects after POST

Create a new user; The Monastery Gates request (ie. it will follow POST redirects but it will turn them into GETs ua->requests_redirectable }, 'POST'; $ua->redirect_ok read more

Source: http://www.perlmonks.org/?node_id=147608


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>