My First Plugin

October 16th, 2007
by psykoprogrammer

Yes, ladies and gentlemen, I have started learning how to write WordPress plugins. My first plugin is one that removes the word “Private: ” from the beginning of blog and page titles (when they are saved with a status of private).

The way this works is by creating a function that takes one argument. I then used a regular expression to remove the offending words. The regex I used was /^(private:\s*)/i and then I simply replaced any match with a blank string, removing it from the source text. And in WordPress there are two types of plugins: Action hooks, and Filter hooks.

In this case I used a filter hook, since filter hooks alter text coming or going from the WordPress database. So to make my filter work you have to use the add_filter function to register my function to handle the filter hook “the_title“. This hook alters the title of posts and pages before they go out to the browser. You can find my plugin at http://blog.adampresley.com/wordpress-mods/hide-private-in-title/. Now, here’s the code.

  1.    /*
  2.       Plugin Name: Hide "Private" In Title
  3.       Plugin URI: http://blog.adampresley.com/?page_id=124
  4.       Description: Removes the word "private:" from the beginning of blog entry
  5.                    and page titles.
  6.       Version: 1.0
  7.       Author: Adam Presley
  8.       Author URI: http://www.adampresley.com
  9.  
  10.       Copyright 2007  Adam Presley  (email : psykoprogrammer@yahoo.com)
  11.  
  12.       This program is free software; you can redistribute it and/or modify
  13.       it under the terms of the GNU General Public License as published by
  14.       the Free Software Foundation; either version 2 of the License, or
  15.       (at your option) any later version.
  16.  
  17.       This program is distributed in the hope that it will be useful,
  18.       but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.       GNU General Public License for more details.
  21.  
  22.       You should have received a copy of the GNU General Public License
  23.       along with this program; if not, write to the Free Software
  24.       Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  25.    */
  26.  
  27.    //————————————————————————–
  28.    // Name: filter_hidePrivateInTitle
  29.    // Auth: Adam Presley
  30.    // Desc: Removes the word "private:" from blog and page titles.
  31.    //————————————————————————–
  32.    function filter_hidePrivateInTitle($content)
  33.    {
  34.       return preg_replace(‘/^(private:\s*)/i’, "", $content);
  35.    }
  36.  
  37.    //—————————
  38.    // Add the hook to WordPress.
  39.    //—————————
  40.    add_filter(‘the_title’, ‘filter_hidePrivateInTitle’);

Tags: , , ,
Posted in Development, PHP | Comments (0)

No comments yet

Leave a Reply

You must be logged in to post a comment.