SimplyExclude Plugin for WordPress

April 27th, 2008 @ 8pm : 216 comments : Socialize This
Filed Under: Tags: , , , , ,

Have you ever been working on a client WordPress project, or maybe your own blog and needed to exclude a Post category from the Front page? Or needed to exclude a Page from being included in a Search? Keep reading.

Over the past year I’ve worked on twenty different client projects. Each one a little similar yet each one a little different. On most the client wanted only a certain category listed on the front page. After many checks on the different versions of WordPress, I’ve not found a ’standard’ way of doing this via the default WordPress admin interface. Sure on the Options -> Reading page you can select a Page or Latest Posts to display. But if you need to limit the display on the Front page to say just the News category you are out of luck.

One solution that works very well is to hook into the WP query object to intercept the ‘action’ in WordPress. All it takes is a simple function and filter added to your theme’s function.php file. Something like the following.

1
2
3
4
5
6
7
8
9
10
// Used to limit the categories displayed on the home page. Simple
function myHomePostsFilter($query)
{
   if ($query->is_home)
   {
      $query->set('cat','1');
   }
   return $query;
}
add_filter('pre_get_posts','myHomePostsFilter');

If you are not familiar with WordPress filters let me provide some overview. The function takes a single parameter, This is the reference to the WP Query object (see /includes/query.php from the WordPress engine code). Don’t worry about the details as you will not be calling this function directly. This function will be called when WordPress is preparing a query for some display of information via another function call. Inside the function you will see the ‘if ($query->is_home)’. The ‘is_home’ is a built-in WordPress action. for Categories there are 5 total. They are ‘is_home’, ‘is_list’, ‘is_search’, ‘is_feed’ and ‘is_archive’. In our example above I’m only concerned with the ‘is_home’. So if the query is being built for the Home/Front page the if is true so the next line is executed. The next line ‘$query->set(‘cat’,'1′);’ sets the query category to the one cat_id I’m interested in. For the example this is cat_id=1. If needed I could have added more than one category as in ‘$query->set(‘cat’,'1,5,15,36,285′);’. I could also exclude categories from the category list simply by preceding the cat_id with a minus as in ‘$query->set(‘cat’,'-1, -16, -32′);’. The next line returns the query back to the calling function. Then end of function. So far this is just a standalone function. Now we have to tell WordPress we want it to hit our function. This is the last line. A filter in WordPress can be thought of as a ’subscribe’ action. The first parameter of the filter. Tells WordPress you want to subscribe to a certain action. In our case this is ‘pre_get_posts’. See this Codex page on Custom Queries. Scroll down to the section header ‘Category Exclusion’. Our filter basically tells WordPress ‘before you run the query hit our function first’. This give our function to massage the parameters to adjust the query. Done. This works for most versions of WordPress 2.x up to the latest and greatest (2.3.1 as of this entry). Pretty simple.

But sadly this cat_id is hard-coded into the functions.php file. So if this is your blog and you have control of the admin keys to the kingdom you can stop reading. I mean it takes all of two seconds to adjust the value of the cat_ids you wish to include/exclude. But what if you are knocking out a project for a client. A client you don’t really want to hit you up every time they need to exclude/include a category from some WordPress action like ‘keep the category X from showing on the Home page’. You have a choice. You can document for the user how to make edits to the functions.php file and hope they don’t somehow screw the code up.

Or you can look for some plugin that will do this or you. I can tell you I’ve looked through various plugins. Only really found one that came close to making this somewhat automated. And that plugin stopped working at WordPress 2.1. Something about the very complicate queries it was building. After reading through 300 line of it’s code just for the query logic I dropped it. I mean the 8 lines of code I provided above are really all you need for the logic portion to include/exclude cat_ids. Why go through the twisted query logic that didn’t run until after the page was loaded. This in effect caused two queries to be executed.

So I started writing my own. Something simple. I just wanted a list of the categories and the available options or WordPress ‘actions’. Let the user select the action for the category and go. I’ve produced something functional. It’s in the very early stages of work. I’ve also included a section for Pages. for Pages I’ve only worked through the logic for the ‘is_search’ action. For both Categories and Pages the user has the option of including or excluding for the actions.

Here is a screenshot of the SimplyExclude Category admin page. It’s the same for the Pages but just for search action.

View of SimplyExclude plugin admin page for categories

So download the SimplyExclude Plugin for WordPress v0.1. It’s version 0.1 0.2 but I plan adding more bling soon. I’ve tested this on the latest 2.3.1 engine only. If you are running this and have trouble please feel free to contact me for help.

Update

After entering this post and sending a similar response to someone on the wp-hackers list it occurred to me that for Pages there should be an option on the Page editor that allows the exclusion of the page from the Search actions. So took some time this evening to update the plugin to version 0.2 (W00t!). So not only will the Pages be listed under the plugin’s options. But along the right sidebar on the Page editor look for the ‘Exclude from Search’ dropdown option. Both options update the same information.
See the screenshot below for what it appears like.
SimplyExclude Plugin Page Options

Download SimplyExclude Plugin for WordPress v0.2

Update 2008-04-27

After some comment I took another look at the plugin code. Seems I provided the version for WordPress before 2.3. The new version now works all the way up to WordPress 2.5.1. Also, I’ve relabeled ‘List’ to ‘Archive’. I’ve also added logic for tags inclusion/exclusion. This works the same way as Categories. The tags admin menu only works under WordPress 2.3 and higher.

Download SimplyExclude Plugin for WordPress

Also upcoming is a better Category and Tags display listing. I’ve received some comments that users want to see the display nested similar to other parts of the WordPress admin interface.

Update 2008-07-16

Seems some of the WordPress 2.6 changes do in fact effect the way the plugin stores it’s options. I have a fixed version but due to technical (or accounting) I’m having trouble updating the plugin into the main repository. You can download version 1.7.1 here. I’ll update the plugin into the repository when WordPress gets things worked out.

simply-exclude-v171

Versioning your Blog content

April 18th, 2008 @ 4pm : 15 comments : Socialize This
Filed Under: Tags: , , ,

Have you ever made an edit to some Post, saved the results then sometime later wished you had an undo option? Or worse have you setup a site for a client and they ‘accidentally’ wack one of the Pages? Then they call you to ‘fix it’. Well I have some good news for you…

Introducing a new plugin, Revisions

Design

The Revisions plugin is based on a very old plugin, Bliki (Blog + Wiki), written by the fine folks at Automattic. There are a few other good versioning plugins out there. But in my opinion they all offer too many admin bells and whistles like Version Diff-ing, Funky rollback schemes, Complicated admin interfaces, etc.

Sticking with my ‘Simple’ philosophy, I’ve opted to cut the functionality to the necessities. The Revisions plugin provides the user with three specific types of integrated functionality.

  1. The ability to create a backup of the edited Post/Page. This is built-in once the plugin is activated.
  2. The ability to view a Revision just as you would preview a Post in the Theme.
  3. The ability to reset or rollback a version into the admin editor

Below is a screen capture of the Revision section that is added to the Admin editor screens. This is a screen of 2.5 but looks just the same under pre-2.5 versions of WordPress.

Revisions plugin admin interface

From the screen image you can see there are 3 Revisions to this one Post. Each Revision notes the version number, the time of the Revision and by what author. Below each Revision items is a link ‘View Revision’. This will display a themed version of Revision. There is also a second link that will load the Revision content into the Post/Page editor. Once reloaded the author can Save or make changes to the content. This reload and save will generate a new Revision. In other words the original Revision will remain intact.

Installation

Download the Revisions plugin. Unzip it. Upload the ‘Revisions’ folder to the plugins folder of your WordPress site. Once uploaded go to Manage -> Plugins and activate the plugin. That is it. Now when you make a change to any Post or Page the version is saved. A suggestion is to perform a Save on an existing Post or Page first before starting to make changes. I’ve tested this plugin on WordPress 2.5 down to 2.0.3.

Future Plans

As I mentioned this plugin is basic on purpose. One feature I might be adding in the near future is the ability to delete individual Revisions. Sometime while you are in the Edit & Save cycle you can build up a few unnecessary Revisions. Another possible option will be to define a threshold for comparing the new version to the next previous Revision. If the amount change is below the threshold the Revision is not saved. This would prevent some very minor Revisions.

Enjoy.