If like me you use WordPress for your website (most people do these days!) you might have 100’s of pages of great content.
Buffer is an AWESOME new tool that lets you create a social buffer of content that gets posted to your social networks at specific times. This is great for when I add new content to my websites, or want to share photos – but I’ve struggled for awhile at finding the best way to share older content, that’s still relevant, and I want to share.
That’s where I found this awesome hack for wordpress. Simply add the code below to your functions.php file, and now every time I go to https://www.bradydyer.com/random – a, you guessed it, random page is displayed from my portfolio! I can then add this to my buffer easily using their chrome plugin.
Too simply, I wish I’d thought of it sooner!
Make sure you use their Pablo extension too, it’s great to buffer a link to the page, and if you’re a photographer, buffer a photo with a link to the page as well!
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; } }
Depending how your wordpress is setup, you might need to change “post_type” to “portfolio” or you can setup a few different ones. I have random for my portfolio and randompost for my ‘post’ (which is the wordpress word for blog)
Thanks to http://stackoverflow.com/users/1038471/rlesko for sharing this code!