WordPress:
plugin basics

A presentation for the Ventura County WordPress Meetup

Available at SplendidSpider.com/article/how-plugins-work

Created by Paul Blakelock
web@SplendidSpider.com

Overview

Using functions.php vs. a plugin

Basic requirements

Simple example: add some shortcodes

Not: admin, uninstall

Plugin vs. functions.php

Lots of helpful examples say to "…add it to your functions.php…". In most cases, don't.

Check an article's "expires on" date.

Ask yourself:

  • Is there a chance you'll reuse it?
  • Does it do something that's needed no matter which theme you use?

Which one?

Add to functions.php

Use this if it is only theme related and you won't use it for any other themes

Make a plugin

Everything else.

Did I just lie?

Yes. It often isn't that clear cut.

Add a favicon: theme or site related?

If it involves data or you'll reuse it, start with a plugin.

Often what the plugin does will require a change to the theme to make it visible (slideshow, add a logo).

Plugin issues

Plugins can be removed or disabled. When something is broken "…disable all of your plugins…". What if your plugin is providing a major part of your content (via shortcode)?

OTOH: it's also gone if it's in your functions.php and you change themes.

Can it be deleted? Am I still using it? Esp. for shortcodes.

What about the data?

When a plugin is deleted (not just disabled), its data is too.

What is a plugin?

Plugin requirements

There are only a few things required:

  • Header comments
  • Some code — because it should do something
  • Directory name — the plugin-slug
  • File name — the plugin-slug.php, usually

But the requirements are fuzzy…

Header details

The header is a PHP comment.


<?php
/*
Plugin Name: SSWD Most Basic plugin
Description: The simplest: no controls and it provides html text.
Version: 0.1.0
Author: Paul Blakelock, Splendid Spider Web Design.
*/

The minimum to make it work is the name line:


Plugin Name: SSWD Most Basic plugin

More headers at http://codex.wordpress.org/Writing_a_Plugin

Directory and filename

  • The directory is optional, but use one to keep your code separated.
  • The plugin will work if the directory name does not match the main PHP file name, but don't.
  • Codex says: "no two Plugins … can have the same PHP file name." Not really, it just needs to be uniquely identifiable. (Different directory and header name was enough.)

Using the plugin

They're all the same… install and enable.

  • Put it in the plugin directory
  • Go to the plugins page in Admin
  • Enable it

Let's make a plugin

We'll cover:

  • The Most Basic Plugin™ (TMBP®)
  • Again, but as on object
  • Make a shortcode (or two)

The Most Basic Plugin #1

<?php
/*
Plugin Name: SSWD Most Basic Plugin 1
Description: Plugin demo: a single function to make text.
Version: 0.1.0
Author: Paul Blakelock, Splendid Spider Web Design.
License: GPLv2
*/
// Echo this function in a template to see the text 'Hello world'.
function sswd_hello () {
    return 'Hello world';
}

Using TMBP™ #1

The plugin can be used by adding some code to a template:

if (function_exists('sswd_hello')) {
    echo sswd_hello();
}

Enabling the plugin is all it takes.

TMBP™ #2

Do the same thing (return some HTML), but using a class.

<?php
/*
Plugin Name: SSWD Most Basic Plugin 2
...
*/

class SSWD_Most_Basic_2
{
    const THE_HTML = '

The most basic plugin.

'; public function get_html() { return self::THE_HTML; } }

Using TMBP™ #2

It's almost no different than using the function.

This code could go in the plugin, or in functions.php or in a template:

function get_sswd_tmbp2() {
    $obj = new SSWD_Most_Basic_2();
    return $obj->get_html();
}

Then do the same as TMBP1:

if (function_exists('get_sswd_tmbp2')) {
    echo get_sswd_tmbp2();
}

How about something useful?

Plugins often interface to WP using hooks, for instance add_shortcode():

add_shortcode('sc_name', 'function_to_call')
        add_shortcode('sc_name', array($this, 'method_to_call'))

A plugin can do more than one thing. So we'll use a function and a class.

TMBP™ #3, pt. 1

A function and a shortcode

<?php
/*
Plugin Name: SSWD Most Basic Plugin 3
…
*/


// Add a shortcode using a function.
function sswd_hello_func () {
    return 'Hello world';
}
add_shortcode('sswd_hello_f', 'sswd_hello_func');

// …

That is all that is needed.

TMBP™ #3, pt. 2

Also use a class

// Add a shortcode using a class.
class SSWD_Most_Basic_3
{
    const THE_HTML = '

Hello red world.

'; // Setup the class. function __construct() { // Use the WP hook to make the shortcode. add_shortcode('sswd_hello_c', array($this, 'get_html')); } // This is the method that will be called when the shortcode runs. function get_html() { return self::THE_HTML; } } // …

TMBP™ #3, pt. 3

Instantiate the class

// Create the object to attaching the hooks.
$sswd_tmbp3 = new SSWD_Most_Basic_3();
// …and keep using up this memory forever.

Done.

Using TMBP™ #3

The class was instantiated when the plugin was loaded. The hooks were set at that time, so the shortcodes are ready to use in any post or page.


USAGE
The shortcodes can be added to any post.
[sswd_hello_f]
[sswd_hello_c]

Note that one shortcode was made with a function, and one with a class. They aren't interacting in any way other than being in the same file.

Seen in the wild

After I put this presentation together I found this CSS-Tricks article that covers some of the same stuff:

https://css-tricks.com/wordpress-functionality-plugins/

It turns out that moving code out of functions.php and into a plugin has a name:

WordPress Functionality Plugins

Next?

That was the easy part about plugins: making them do what you want them to.

The hard part is creating an admin interface.

Our work here is done.

THE END

Reveal.js

The slideshow:
- Online editor: slides.com
- Source code & documentation: https://github.com/hakimel/reveal.js