Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I need a random redirect button just like 9gag.com I know that i need a add a rewrite rule but i dont know how to do this

I tried to use plugins and other codes, but none worked.

share|improve this question
1  
what have you tried? –  Milo Jan 23 '13 at 16:52
add comment

closed as not a real question by s_ha_dum, kaiser, toscho Jan 23 '13 at 19:12

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

up vote 0 down vote accepted

you can use 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;
    }
 }

ANd then use this for random redirect

 <a href="http://.....com/?random=1/" >Random Post</a>
share|improve this answer
    
it looks like half of that code is not relevant to the answer –  Milo Jan 23 '13 at 17:13
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.