One Click Featuring in WordPress Sites

In consulting You Show participants on their site organization, I am trying to help them see there are more opportunities for the front page than the long river of reverse chronological ordered posts.

Many themes (like the Virtue Theme on the You Show) give you an option to display posts from a category, rather than just the newest. I helped Franzi today flip hers using the stick Twenty Fourteen Theme; there is an option in the Customizer to put posts tagged “featured” in the front magazine style layout.

For themes that do not provide this, I have gone the route of customizing the WordPress Loop query used on the front page. A non hacking route is the Category Excluder from Theme Customizer plugin which lets you choose which categories not to display on the front page.

This capability becomes very useful on Feed WordPress type aggregation hubs because the inflow of posts is so high (hopefully) that a river of new posts is not useful.

My strategy is to recommend that people who are admins on the site do some curating-by editing aggregated posts to add a category (or tag), they can create a set of “featured” posts (viewable via the Category Archive view). I was never able to get the Connected Courses folks to grasp this idea; maybe no one wanted that responsibility.

I did try using a rating plugin on the ETMOOC hub that enabled a “highest rated” view but not too many people voted.

As a site admin, it helps to do this via a category process because it forces you to regularly review incoming stuff. I set it up on the You Show site, we have a category called “Highlight Reel” we can add to posts:

highlight-reel

But the process for getting to the posts to add the category was.. tedious. In an archive view, there is no way to edit that post, and because the links in the syndication hub go out to the site, it is not an option to view the post, and then click the “Edit” button in the admin bar.

Some themes add an “edit” link in the templates so you can do this, but Virtue does not. It’s a pretty easy thing to add, if you can find the write template file (this varies from theme to theme, in Virtue, it was in /templates/content). The thing you want to add is the wordpress function edit_post_link. There are options you can use on that function to add spans and or css classes, but at it’s simplest, somewhere in the loop where it is spitting out content for each post, add something like:


What this does is add an “edit” link next to each post that can only be seen by users that are logged in with post editing privileges (Editor, Admin, etc). Visitors to the site will not see them.

These helps some, because if I see a post on the flow I’d like to feature, I just:

  1. Click the Edit Post link
  2. In the editor, click the category to add
  3. Update the post
    1. It’s not bad, but still seems 3 steps when all we need to do is to add a category. So I hacked something in.

      I will need a way to send from the clicked link a variable that identifies the post ID number I am updating. In my (child) theme functions.php, I do need a function that allows WordPress to understand extra URL parameters. For my site, I am using ones already created for the signup form:

      [sourcecode lang=”php”]
      /* —– add allowable url parameter for urls */
      add_filter(‘query_vars’, ‘youshow_parameter_queryvars’ );

      function youshow_parameter_queryvars( $qvars )

      // allow parameters to be passed in wordpress query strings
      {
      $qvars[] = ‘pid’; // when we need to pass a post id
      return $qvars;
      }
      [/sourcecode]

      This means I can send to a WordPress URL something like:

      http://www.mysite.org/feature-this/?pid=23

      And my custom template for this page named Feature This has some logic it can act on.

      Next, I made a function that can put the link to mark a post as featured, and make it so only people logged in with editing privileges can see (and click) on it.

      [sourcecode lang=”php”]
      function get_highlight_post_link ( $post_id, $toolslug = ‘feature-this’ ) {

      // only for editors and above
      if ( current_user_can(‘publish_pages’) ) {
      return (‘ [Feature This]’);
      }
      }
      [/sourcecode]

      I send the function get_highlight_post_link() a value for the post I want to affect. By default, the link will be to a page on my site with a url / slug name of feature-this. And it opens in a new window so I don’t lose my place

      This is solely so I can add this to my templates. By making it a function, I can use it more than one template. In my theme, I find the part of the code that echoes the meta data below a post, and add in:

      [sourcecode lang=”php”]
      … (meta data theme stuff like date, comments, categories)

      ID )?>
      [/sourcecode]

      Now… (if anyone is still with me). I create a page that has the slug name (url) of feature-this which means if I create and upload a new theme template named page-feature-this I can put in stuff that only is used on the one page.

      The way to do this is to copy the basic page.php template for your theme, and rename it page-feature-this. Put this code at the top:

      [sourcecode lang=”php”]
      query_vars[‘pid’])) ? $wp_query->query_vars[‘pid’] : -1;

      // set up array to update post
      $w_information = array(
      ‘ID’ => $post_id,
      ‘post_category’ => array( $highlight_category_id )
      );

      // update the post, value is 0 if it borked
      $da_post = wp_update_post( $w_information );
      ?>
      [/sourcecode]

      This all happens before the page loads. Farther done in the template, after where it spits out the contents for the page (which can be anything, its only for admins) add right after <code?

      [sourcecode lang=”php”]

      Processing…

      <?php
      if ( $post_id == -1) {
      echo "

      Uh oh. No post id found. Bad link?

      “;
      } elseif (!$da_post) {
      echo “

      Egads. Something bad happened. Unable to update post.

      “;
      } else {
      echo ‘

      Eureka! We added ‘ . get_the_title( $post_id ) . ‘ to the ‘ . $def_category_name . ‘ Category. Yipppie!’;
      }
      ?>

      [/sourcecode]

      With this in place, I can add a post from any archive view listing to our “Highlight Reel” category, by clicking the “Highlight This” link (my names are different from the example code above)

      Admin's only have access to these tool links
      Admin’s only have access to these tool links

      It not only makes it simple for me, I can give anyone I want to push things into this curating role an Editor role, and then can log in and mark posts to be featured.


      Top feature image credits : cc licensed (BY-NC-SA) flickr photo by Neetesh Gupta (neeteshg): http://flickr.com/photos/neetesh/6766075885

About cogdog

I bark about and play with technology and web stuff. Harmless. I have my shots.

Leave a Reply

Your email address will not be published. Required fields are marked *