<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Frumph.NET &#187; widget</title>
	<atom:link href="http://frumph.net/tag/widget/feed/" rel="self" type="application/rss+xml" />
	<link>http://frumph.net</link>
	<description>Just another WordPress site</description>
	<lastBuildDate>Sun, 20 May 2012 22:46:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Random Post Widget</title>
		<link>http://frumph.net/2009/09/21/random-post-widget/</link>
		<comments>http://frumph.net/2009/09/21/random-post-widget/#comments</comments>
		<pubDate>Mon, 21 Sep 2009 08:08:07 +0000</pubDate>
		<dc:creator>Philip M. Hofer (Frumph)</dc:creator>
				<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[code snippet]]></category>
		<category><![CDATA[theme]]></category>
		<category><![CDATA[widget]]></category>

		<guid isPermaLink="false">http://frumph.net/?p=222</guid>
		<description><![CDATA[Navigation is a big issue with *alot* of people. People want to find your content or find a way to &#8216;get&#8217; to your content. One of ways you can do that is to actually have something that makes it so [&#8230;] <a class="more-link" href="http://frumph.net/2009/09/21/random-post-widget/">&#8595; Read the rest of this entry...</a>]]></description>
			<content:encoded><![CDATA[<p>Navigation is a big issue with *alot* of people.  People want to find your content or find a way to &#8216;get&#8217; to your content.  One of ways you can do that is to actually have something that makes it so that users can go to a post of yours that&#8217;s completely random.</p>
<p>In ComicPress, Tyler Martin created a Random Comic widget which is very useful to have people go to any number of comics in the archive.   I&#8217;ve adapted the widget to be random posts, a pick of all of them and converted it to a WordPress 2.8 + widget.</p>
<p><span id="more-222"></span></p>
<p>Let&#8217;s start off by writing the line of code that looks for a word in the URI line:</p>
<pre class="brush: php; title: ; notranslate">
if ( isset( $_GET['randompost'] ) )
	add_action( 'template_redirect', 'random_post' );
</pre>
<p>What this line does it looks for /?randompost at the end of a URI line and adds an action to the <em>template_redirect</em> function that executes the <em>function random_post</em>.  Because of the questionmark before randompost the uri line will think that it&#8217;s a uri variable container and wants a variable for it, but we dont need one, just setting it is enough.</p>
<p>Now let&#8217;s write the function random_post that gets run when it finds randompost in the uri line.</p>
<pre class="brush: php; title: ; notranslate">
//Generate a random post page - to use simply create a URL link to &amp;quot;/?randompost&amp;quot;
function random_post() {
	$randomPostQuery = new WP_Query(); $randomPostQuery-&amp;gt;query('showposts=1&amp;amp;orderby=rand');
	while ($randomPostQuery-&amp;gt;have_posts()) : $randomPostQuery-&amp;gt;the_post();
		$random_post_id = get_the_ID();
	endwhile;
	wp_redirect( get_permalink( $random_post_id ) );
	exit;
}
</pre>
<p>This function random_post creates a new query since $wp_query and $post are not being read as a global will totally avoid it.   In the query line it asks for 1 post and orders it by rand which means randomize the entire posts archive but only show one post.   Then what it does it does a while loop of all the posts it finds and sets $random_post_id to the ID of the post.  Then redirect by getting the permalink that $random_post_id points to.</p>
<p>Okay so far we&#8217;ve got this, we&#8217;ve got the ability to read <em>randompost</em> from the URI line with a $_GET and then we have it execute the function to actually redirect users to a random post on your system.</p>
<p>But how do we embed it on your site?   Well you could just make a static link that goes   <a href="/?randompost">Random Post</a>  that would work just fine, because all your doing is wanting to set the URI line.   You can do that with a text widget very easily.   &#8230; I wrote a WordPress 2.8/2.8.4+ widget that will do it for you however so you can just adjust it&#8217;s position in your sidebars how you see fit.</p>
<pre class="brush: php; title: ; notranslate">
class widget_random_post extends WP_Widget {

	function widget_random_post() {
		$widget_ops = array('classname' =&amp;gt; 'widget_comicpress_random_post', 'description' =&amp;gt; 'Displays a link to click to trigger a random blog post.' );
		$this-&amp;gt;WP_Widget('random_post', 'Random Post', $widget_ops);
	}

	function widget($args, $instance) {
		global $post;
		extract($args, EXTR_SKIP);

		echo $before_widget;
		$title = empty($instance['title']) ? '' : apply_filters('widget_title', $instance['title']);
		if ( !empty( $title ) ) { echo $before_title . $title . $after_title; }; ?&amp;gt;
			&amp;lt;h2&amp;gt;&amp;lt;a href=&amp;quot;?randompost&amp;quot;&amp;gt;&amp;lt;span class=&amp;quot;random-post-icon&amp;quot;&amp;gt;?&amp;lt;/span&amp;gt; Random Post&amp;lt;/a&amp;gt;&amp;lt;/h2&amp;gt;
		&amp;lt;?php
		echo $after_widget;
	}

	function update($new_instance, $old_instance) {
		$instance = $old_instance;
		$instance['title'] = strip_tags($new_instance['title']);
		return $instance;
	}

	function form($instance) {
		$instance = wp_parse_args( (array) $instance, array( 'title' =&amp;gt; '' ) );
		$title = strip_tags($instance['title']);
		?&amp;gt;
		&amp;lt;p&amp;gt;&amp;lt;label for=&amp;quot;&amp;lt;?php echo $this-&amp;gt;get_field_id('title'); ?&amp;gt;&amp;quot;&amp;gt;Title: &amp;lt;input class=&amp;quot;widefat&amp;quot; id=&amp;quot;&amp;lt;?php echo $this-&amp;gt;get_field_id('title'); ?&amp;gt;&amp;quot; name=&amp;quot;&amp;lt;?php echo $this-&amp;gt;get_field_name('title'); ?&amp;gt;&amp;quot; type=&amp;quot;text&amp;quot; value=&amp;quot;&amp;lt;?php echo attribute_escape($title); ?&amp;gt;&amp;quot; /&amp;gt;&amp;lt;/label&amp;gt;&amp;lt;/p&amp;gt;
		&amp;lt;?php
	}
}
register_widget('widget_random_post');

function widget_random_post_init() {
	new widget_random_post();
}

add_action('widgets_init', 'widget_random_post_init');
</pre>
<p>This is a standard 2.8 widget.   You can then go to Appearance -&gt; Widgets and add it to any of your sidebars.</p>
<p>Just like Tyler did with ComicPress&#8217;s Random Comic I set a class inside the widget so you can place an icon image inside there if you wanted to, or in this case just make the ? mark look nicer.</p>
<pre class="brush: php; title: ; notranslate">
&amp;lt;span class=&amp;quot;random-post-icon&amp;quot;&amp;gt;?&amp;lt;/span&amp;gt;
</pre>
<pre class="brush: css; title: ; notranslate">
.random-post-icon {
	padding: 0 5px;
	color: #fff;
	background: #000;
}
</pre>
<p>You of course can change the CSS to look how you want to for an icon.</p>
<p>Have fun!</p>
<p>- Phil (Frumph)</p>
 <img src="http://frumph.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=222" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://frumph.net/2009/09/21/random-post-widget/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

