Tell me more ×
WordPress Answers 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 at 16:52

closed as not a real question by s_ha_dum, kaiser, toscho Jan 23 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, see the FAQ.

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;
    }
 }

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

add_action('template_redirect','vote_template');
function vote_template() {
   if (get_query_var('vote') == 1) {
           $posts = get_posts('post_type=post&orderby=post_date&numberposts=10');
           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 at 17:13

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