A fine WordPress.com site

Menu

In my post ‘Changing the WordPress Search Form to HTML5‘ I mentioned it could be a made into a plugin. Today we will have a quick look at writing a plugin. We will be building a useless plugin which does basic word filtering, which will change ‘the’ to ‘da’, though not in any smart way. The plugin will be like the ‘egg’ to ‘duck’ word filter from 4Chan’s Moot which resulted in the DuckRoll MeMe.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
[/cc]

Plugin Name is the name WordPress will show as the name

Plugin URI is where the Plugin can be downloaded from

Description is a the description used for the plugin which can include some basic HTML

Version allows to inform people of the version for upgrades

Author and URI creates a link to you

License is a URI and the license agreement for the plugin which in this case GPL is being used

Vital, use classes for protection

A lot of plugin tutorials skip over the vital step of wrapping your plugin in a class. By using a class you reduce the chance of code conflict with other plugins. For instance I have seen the function ‘my_admin_init’ used in multiple plugins which will cause WordPress to crash. First we will check that there is no other class with the same name, if there isn’t we will create our class.
[cc lang=”PHP”]if (!class_exists(“the_to_da”))//check that there is no other classes like this
{
class the_to_da //it takes class to use classes
{

}
}//end the check class exists[/cc]

Next we will add our code to create the object using our class.

[cc lang=”PHP”]if (!class_exists(“the_to_da”))//check that there is no other classes like this
{
class the_to_da //it takes class to use classes
{

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

if (!class_exists(“the_to_da”))//check that there is no other classes like this
{
class the_to_da //it takes class to use classes
{
function the_to_da()//construct
{
add_filter(‘the_title’, array(&$this, ‘word_replace’));//add a filter
add_filter(‘the_excerpt’, array(&$this, ‘word_replace’));//add a filter
add_filter(‘the_content’, array(&$this, ‘word_replace’));//add a filter
}