How to Write a
WordPress Plugin
Baltimore WordPress Meetup • September 19,
                   2011
What is a plugin?
Written in PHP
        Adds functionality
      without affecting core
               code
               Plugin API
Hooks: Actions &
    Filters
Actions
Hey, WP! I’d like to do this
   when you do that...

Calls a function that performs an
 action, possibly with some data
           given by WP
Actions
add_action( $hook_name, $function );

     add_action(‘wp_head’, ‘my_custom_js’);



add_action(‘wp_head’, array(
    $some_object, ‘my_custom_js_method’)
));
Actions
init

  wp_head

       save_post
Filters

Hey, WP! I’d like to change a
value that you’re returning/
           using...

Calls a function that returns a final
Filters
add_filter( $hook_name, $function );

add_filter(‘the_content’, ‘my_content_filter’);



add_filter(‘the_content’, array(
    $some_object, ‘my_content_method)
));
Filters
the_title

  the_content

     wp_email_from
Getting Started
PHP file or folder
containing PHP file

    Javascript, CSS, etc.

          /wp-content/
Plugin Information
<?php
/*
Plugin Name: My Simple Plugin
Plugin URI: http://theandystratton.com/my-plugin
Description: Does something awesome.
Version: 1.0
Author: theandystratton
Author URI: http://theandystratton.com
License: GPL2
*/
Info Shows in Admin
         UI
Don’t Just Drop
        Code
WordPress includes plugins
    while initializing

Coding without hooking into
 actions/filters = explosion
Starting to Code

What do you want to do?

I want to add my personal
   signature to all posts.
Starting to Code
What does my plugin do?

 Filter the content of all
  posts and append my
         signature.
Some Code

   Hey WP! When you’re
processing standard content,
     call this function!
Take the value WP provides us, add
    my signature, give it back.
It’s as easy as that.
But we can
complicate things...
Archives, Listings,
        etc.
Oops. I mean, I only want it
to add my signature to the
 single post, not listings/
         archives.
Archives, Listings,
         etc.
 What do you want to do
         now?

Filter the content of all posts
  and append my signature
IF we’re on a single post page,
    THEN add my signature.
But we can
complicate things...
Post Type Content

Oops. I have a custom post
type called “Products,” and
it’s adding my signature to
         those, too.
IF we’re on a single blog post
page, THEN add my signature.
Getting More
 Advanced
Using Objects
      Avoid tons of
    prefixed functions

Use a prefixed class to hold
  all your actions/filters
Using Objects
Do All Kinds of Fun
       Stuff
Plugins Do Way
     More
  Custom Post Types

      Shortcodes

Metaboxes and metadata
     management
Plugins Do Way
         More
        Settings Pages

        Settings Fields

Incorporate Internationalization
Plugins Do Way
     More
   Redirect requests

 Modify Rewrite Rules

Emulate CRON jobs (wp-
       cron.php)
Going Public
Get in the
        Repository
  Submit your plugin code
http://wordpress.org/extend/plugins/
               about/
http://wordpress.org/extend/plugins/
             about/faq/
Get in the
        Repository
          Readme.txt
http://wordpress.org/extend/plugins/
          about/readme.txt
Get in the
       Repository
    Subversion access &
       management

       Support URL?

Update within 2 years or hide
More Resources
 “Writing a Plugin”
http://codex.wordpress.org/
      Writing_a_Plugin

“WordPress Coding
   Standards”
  http://codex.wordpress.org/
  WordPress_Coding_Standards
Google Works Great
Keep It Simple Stupid.
Code is poetry.
Thanks.

How To Write a WordPress Plugin