Embed RSS feed into your WordPress page content

RSS feeds are common in sidebars. Fuelled by widgets and looking much the same from site to site. Some designers create widgetized areas for their clients so they can put RSS feeds in the main content column. There is a simpler way, using a handy shortcode.

RSS Feed shortcode

function tcb_rssfeed_shortcode($atts){
  include_once(ABSPATH.WPINC.'/rss.php');
  $default = array(
    'feed'    => 'http://www.tcbarrett.com/feed',
    'entries' => 3,
  );
  extract( shortcode_atts($default, $atts) );
  if( function_exists('wp_rss') )
     return wp_rss($feed, $entries);
  return '';
}
add_shortcode('rssfeed', 'tcb_rssfeed_shortcode');

You can now easily insert your own feed:

[[rssfeed]]

Here is an example of my own site’s rss feed:

[rssfeed]

How to extend the idea

There are various ways one could expand on this. For example, adding optional pre and post text would allow a wrapping div and title to be added.

Adding time of day published to posts column

Web sites that wish to publish articles regularly would be lost without the ability to schedule posts. An important and necessary tool for the busy blogger. I use it myself for this little site. However sites with such a high volume may find that they are publishing more than once per day. At this point, knowing just the date when a post was published or is scheduled to be published is not enough. They need to know the time of publication too.

Over at the WP Tavern they mention how it is possible to see this by hovering over the date. I’m quite lazy though, and would find that a little cumbersome. Luckily WP Beginner have pinged the article with a link to a plugin that solves this problem by replacing the date column with something much more powerful. Now, I’m quite inquisitive, so I like to know how to do it myself. So here is my 14 line snippet that you can paste into your functions.php file to add a time column to your posts. It’s visually better than having to hover over every post, but not as featured as the plugin.

Add a time column to your posts list overview admin page

add_filter('manage_posts_columns', 'tcb_add_time_column_to_posts');
function tcb_add_time_column_to_posts($columns) {
  $columns['time'] = __('Time');
  return $columns;
}

add_action('manage_posts_custom_column',  'tcb_handle_time_column');
function tcb_handle_time_column($col){
  switch($col) :
    case 'time':
      echo get_the_time();
      break;
  endswitch;
}

Hope you enjoyed that.

Using the WordPress excerpt correctly

There are a number of ways in which post excerpts can be manipulated in WordPress. The hooks and filters available have improved greatly over the years.

The complicated version 2.8 way
Back in 2009 you would have to roll out your own function. Grabbing the content and doing some rudimentary word counting yourself.

function tcb_the_content($limit) {
  $content = explode(' ', get_the_content(), $limit);
  if (count($content)>=$limit) {
    array_pop($content);
    $content = implode(' ',$content).'...';
  }
  else {
    $content = implode(' ',$content);
  }
  $content = preg_replace('/\[.+\]/','',  $content);
  $content = apply_filters('the_content', $content);
  $content = str_replace(']]>', ']]>', $content);
  return $content;
}

That’s 14 lines of code. Not too much, but the main issue is that it is a hack. It replaces core code. And when you replace core code rather than work with it, you lose out when core gets updated. Not good.

The easy version 2.9 way
Since version 2.9 it has been a trivial exercise in the use of filters.

function tcb_new_excerpt_length($length) { return 20; }
add_filter('excerpt_length', 'tcb_new_excerpt_length');

Doing without excerpts.
Often simple things like this disappear, because they simply are not needed. And quite possibly, with the use of more tags, there is no need to fuss around with the excerpt size any more. A simple and small comment in the main post body can be used to mark the excerpt.

<!--more-->

Do you think the excerpt is needed any more?

Embed screen shots of websites in your content using a shortcode

Shortcodes are a very powerful tool to get to grips with. They enable the website user to place content where they want without needing a great deal of technical knowledge. If you wanted to put a screen shot of a website in your post you might think that you needed to:

  1. Visit the site
  2. Take a screen grab
  3. Open photo editing software to remove unwanted screen items (desktop, tabs)
  4. Upload the file to the Media Library
  5. Insert into to post.

That is a lot of work. You might be forgiven if you thought that automating this would be a technical nightmare, but it isn’t. With some simple use of a shortcode hook and availing yourself of a WordPress snapshot resource, you will be good to go in no time.

Website screen shots using a shortcode

function tcb_screenshot_shortcode($atts, $content=null) {
  $default = array(
    'url'   => 'http://www.tcbarrett.com',
    'alt'   => 'TCBarrett - Open Source Architect',
    'title' => 'Screenshot of www.tcbarrett.com',
    'class' => 'screenshot',
    'w'     => '400',
    'h'     => '150'
  );
  extract( shortcode_atts($default, $atts) );

  $mshots = 'http://s.wordpress.com/mshots/v1/';
  $url    = urlencode($url);
  $img    = "<img src='{$mshots}{$url}?w=$w&h=$h' alt='$alt' class='$class'/>";
  return $img;
}

add_shortcode('screenshot', 'tcb_screenshot_shortcode');

You simply add the screenshot like so:

[[screenshot]]

Here is one of my site:
[screenshot]

And one of the Miramedia website:
[screenshot url=”http://www.miramedia.co.uk”]

[[screenshot url="http://www.miramedia.co.uk"]]

The life and times of WordPress plugins

Alternative title: Three great plugins I no longer use

Part of the WordPress strategy is to use popular plugins as an indicator for future core features. Sometimes this happens, and sometimes it doesn’t. WordPress has been around for long enough now that it is possible to look back at how things have evolved. How once-great plugins have succumbed to the passages of time…

Gone without replacement – Register Plus

At first, all my WordPress installations came with this plugin. I couldn’t imagine building without it. The feature I wanted it for most was the ability to force email verification on user registration. The author stopped developing the plugin. WordPress core updates meant it broke. There were some attempts to fork it and continue, but none gained any traction. The plugin is now completely missing from wordpress.org.

Partially absorbed into core and abandoned – Scissors

Everyone I knew used this plugin! It had two main features. The first allowed images to be edited and this was pulled into WordPress core. The second let you watermark your images (automatically). It was common knowledge that the core team wanted to use the plugin code and the author assisted them and then publicly abandoned his now (partly) defunct master piece. The water marking was a great feature, I miss it a little and I hope someone has created a plugin to replace it.

Thought I couldn’t live without it and losing momentum – Capsman

This little beauty is a powerful plugin with a simple UI that allows you to view and manage your roles and capabilities. I used it a lot before I learnt about the add_role() and add_cap() functions. I prefer to set thing up programmatically rather than configure a plugin. But I have still been using it. It has been a great way to check and debug my code. However, it has not been updated over a year and will surely soon fall foul of the ever-moving WordPress core. I think this is an ideal candidate for the core team to pull in. With the fast growth in use of custom post types with custom roles (mapping the post capabilities onto role-specific capabilities) , I think there are many freelancers out there who would benefit from this.