Archive for September, 2011

Page 1 of 1

WordPress Site Funtionality: Plugin or Functions.php?

If you’re developing websites using WordPress as your CMS, you’ve probably had to decide whether or not to create a plugin for that site’s functionality or to just dump it into your theme’s functions.php file. My solution used to be the latter, but lately I’ve found the former to be superior for one simple reason: themes are less permanent and more likely to be changed after you’ve passed it off to the client than a plugin. Hell, even I change my theme frequently enough for it to be an issue.

That news is likely old hat to you, but it took me a while to really see things that way. I’m not much of a serious developer, and so I don’t have extensive training or knowledge of programming models and logic, but I’ve always had an interest in it. Unfortunately, using object oriented programming wasn’t something that I understood how to do. Don’t get me wrong, I get the model, and how useful it is, I just never knew how to implement it logically, efficiently, and usefully.

I found, though, that as WordPress progressed, it was going to become quite useful to understand OOP on at least a basic level.

As it stands, I’m becoming heavily invested in developing for WordPress, and when I create a site now, I am generally going to be modifying the user experience in such a way as to require the creation of a plugin. This is to make sure clients aren’t doing something that breaks the site, or to shut down some unneeded functionality mostly, but occasionally I’m adding some handy widgets and custom post types.

Bottom line: if you need some Theme dependent functionality, the obvious choice is to keep it in the functions.php file, otherwise create a plugin to preserve that functionality across themes.

Creating the Recent Custom Posts WordPress Widget

I recently found myself needing a way to display the most recent posts from multiple custom post types in multiple widgetized areas of a site I was working on.

Because I needed to do this quickly, and I assumed this need was common, I was on the lookout for an already well written and developed plugin that would do the job. I ran across a widget plugin from New2WP1 that allowed you to display the most recent posts from a single custom post type. Unfortunately, it wasn’t a multi-widget and you could only select one post type, so the plugin wasn’t suitable.

My first thought was that I could easily turn the plugin into a multi-widget capable plugin, but when I started to work on it, I realized that any modifications I made could easily be overwritten on the next update. Additionally, when I began work on turning it into a multi-widget plugin, it became exceedingly obvious that I would probably need to start from scratch.

That’s when I decided to develop a plugin of my own, based on the structure of what I saw in the plugin from New2WP, that would fit my needs, and hopefully the needs of others.

Where to Begin

Visit the Widgets API reference at the WordPress codex for a better overview of this information.

Once I figured out that I needed to turn the widget into one that could be used in multiple widgetized areas, I started to research like mad to find out what it would take to accomplish this. One trip over to the WordPress codex was enough to send me in the right direction.

It was basically as simple as this default usage code implies:

class My_Widget extends WP_Widget {
	function My_Widget() {
		// widget actual processes

	function form($instance) {
		// outputs the options form on admin

	function update($new_instance, $old_instance) {
		// processes widget options to be saved

	function widget($args, $instance) {
		// outputs the content of the widget


However, converting the existing plugin to this wasn’t as simple as the above code implies. The old plugin was built as a function, and its structure made it quite a bit more difficult to tie into the WordPress Widgets API.

Based on what I learned from New2WP’s plugin, and the WordPress Codex, I proceeded to build out my own plugin.

The Recent Custom Posts Plugin

I started by creating the directory /mm-recent-cpt/, the file mm-recent-cpt.php, and readme.txt file for future upload to the WordPress plugin directory.

In the php file I added the plugin header:

 * Plugin Name: Recent Custom Posts
 * Plugin URI:
 * Description: Multi-widget for displaying recent posts of custom post types.
 * Version: 1.0
 * Author: Mike Mattner
 * Author URI:
 * Tags: custom post types, post types, latest posts, sidebar widget, plugin
 * License: GPL

This allows WordPress to gather information about your plugin. If you just put that file into the plugins directory right now, you’ll have a plugin that does nothing, but it will show up in the list.

The structure of this widget is fairly simple because of the Widgets API. You’ll create a class that extends the WP_Widget class, essentially making it a breeze to create this plugin. I named my class mm_recent_posts. In order to get things moving, you need to initialize the plugin. In most cases you’ll use add_action to get your class or function working.

The function I’m calling with add_action returns register_widget, which takes the $widget_class argument. According to the Codex, $widget_class is the name of a class that extends WP_Widget.

So, this is my basic structure:

add_action( 'widgets_init', 'init_mm_recent_posts' );
function init_mm_recent_posts() { return register_widget('mm_recent_posts'); }

class mm_recent_posts extends WP_Widget {
    /** constructor */
    function mm_recent_posts() {
       parent::WP_Widget( 'widget_recent_cpt', $name = 'Recent Custom Posts' );

    function widget($args, $instance) {
       // outputs the content of the widget

    function update($new_instance, $old_instance) {
       // processes widget options to be saved
    function form($instance) {
       // outputs the options form on admin


The above is fairly self-explanatory as far as what each function’s purpose is. The constructor function mm_recent_posts() is designed to give a little information about the widget. You could use something along these lines to get it up and running: parent::WP_Widget('my_widget_base', 'My Widget title', array('description' => 'Very simple widget example', 'class' => 'my-widget-class'));.

Pretty easy, right?

The Plugin

The plugin I created is fairly straightforward. It is essentially just an extension of the original Recent Posts widget that comes with WordPress by default–except that this doesn’t overwrite that.

If this looks like something you could use, feel free to download, use, and modify till you’re blue in the face.

Download From WordPress Plugin Directory

I’ve now uploaded the plugin to the WordPress plugin directory. I hope you’ll find it to be useful.

I’m working on getting this added to the WordPress plugin repository The plugin can also be downloaded from the WordPress plugin repository. If you have any questions, comments, or modifications, let me know in the comments. I can’t guarantee that this will work with all plugins, particularly the Custom Post Type UI plugin, but I will say that I’ve used it, and it works for me. I apologize if my code isn’t up to snuff.

Also, welcome Stumblers! Thanks for stopping by. Hopefully you’ll find my little plugin to be useful.

  1. This is a fine plugin, it just needs to be multi-widget capable to be more useful.