Confused?
Don’t know where to put a comment on a #ds106 student blog as part of the Magnificent Seven Blog Comment Challenge?
I have the link for you.
With some rubbing together of magic dust and a dab of elbow grease, I have a new feature for the various “sections” of ds106- these are classes we know of who’s student blogs we are syndicating into ds106; all of these posts that come in carry an extra tag we can use to slice, dice, and make fries out of.
After a few variations of setting up a random picker in the Assignments Collection, I saw it would be easy to do something for the posts we bring in. The exception here is that we want recent stuff, so these scripts only look for posts published to ds106 n the last 3 weeks.
Without further ado, give thse a spin:
- Random post for UMW Section 1 (Martha’s class)
- Random post for UMW Section 2 (My class)
- Random post for UMW Section 1 (Martha’s class)
- Random post for York College (Michael Branson Smith’s class)
- Random post for Kennesaw State University (Ryan Rish’s class)
- Random post for all Open Online participants
And the links are available on the specific section pages on the main ds106 site.
This is achieved with a little php magic. The first thing I had to do is to add a few bits to by functions.php template to add some key functionality.
First we have to set up our code so we can pass an extra parameter in our URLs, the one that identifies that “tag” we use internally to identify each section.
1 2 3 4 5 6 7 8 9 |
// add allowable url parameters to be passed add_filter('query_vars', 'ds106_parameter_queryvars' ); function ds106_parameter_queryvars( $qvars ) // allow parameter 'tag' to be passed in query string { $qvars[] = 'tag'; return $qvars; } |
Next, and this took a little digging on the codex , when we do the work of finding our random posts, to keep it to ones in the last 21 days, we have to add another block of code to functions.php. This essentially adds a bit of mySQL logic needed to do a date offset (note he example in the codex is 30 days, I shrunk it a week)
1 2 3 4 5 6 |
// Create filtering function that will add our where clause to a wp_query function filter_where( $where = '' ) { // posts in the last 21 days $where .= " AND post_date > '" . date('Y-m-d', strtotime('-21 days')) . "'"; return $where; } |
Now what we have to do is to create a dummy wordpress page. The content it has can be BLAH BOOOGER BOP since it will not be used, the page is a holder we will populate dynamically via its template Heck it can have typos.. But what I want its to make sure it’s ‘slug” is named ‘random’;
Now we create a new template named page-random.php – it is invoked only if a wordpress page n my site has a slug named… random!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
// check for the passed parameter value, set to blank if not fo $tag = (isset($wp_query->query_vars['tag'])) ? $wp_query->query_vars['tag'] : ''; // 21 day filter function (in functions.php) add_filter( 'posts_where', 'filter_where' ); // set arguments for WP_Query() $args = array( 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => 1, 'orderby' => 'rand' ); if ($tag != '') { // add parameter to search for tag $args['tag'] = $tag; } // get a random post from the database $my_random_post = new WP_Query ( $args ); // clean up after ourselves remove_filter( 'posts_where', 'filter_where' ); // process the database request through WP_Query while ( $my_random_post->have_posts () ) { $my_random_post->the_post (); // redirect the user to the random post wp_redirect ( get_permalink () ); exit; } |
What we are doing is first finding out if the URL has a parameter like “tag=purpledinosaur”. We set up our new function so we can filter the query results we will do by the date function set up (published in the last 21 days). Following this, we make an array for some parameters to pass to a wordpress function that looks stuff up in the database. We want it to looks for things that are published posts (no pages, no drafts); we only want one, and the ‘orderby” is the magic paramater, it says to brings us a random result.
If we did get a parameter passed to us, we will add one more thing to look for, published posts tagged “purpledinosaur”
We runt he query, do some cleanup, and that grab the one result- from that post data we can get its URL (get_permalink()) and just send the viewer there via the wp_redirect() function.
It all works pretty damned smoothly. The one thing I am stuck on is I thought I could make a case for when no parameters are passed, that I could just pick a random one from all syndicated posts. I tried some extra query parameters based on the post meta data, but all I ever get is the most recent post.
I love going random!
And thus, you have no excuse not to have fun giving a blog comment to our students.
C’mom.
click (and comment).
How fun is THAT?
UPDATE For a simple random post from a blog in general, it’s much easier- just append ?random
to the main blog URL, like try http://cogdogblog.com/?random
Featured Image:
I left a couple of comments and it was easy! I loved the idea of random. Sometimes when I’m asked to comment on a class, there are so many students and I feel like I have to comment on them all or hurt someone’s feelings. That pressure sometimes turns me away from leaving any comments. This was quick and easy! Thanks!