Developer's Roundtable Notes

Session: Creating a Plugin

Why Do You Want a Plugin?

Before deciding how to enhance your site, ask yourself these questions:

What’s the specific functionality you need?

Is there a core function already in place for this?

Is it a simple, single function like adding another sidebar or customizing the admin menu?

Do you (or your client) need to change options for this particular use?

 


 

This will help you decide if it’s more effective to install a third party plugin, or if you should write one yourself.

And, when weighing your time, remember to include how long it will take you to find, test, and vet someone else’s plugin.

Also remember that once you’ve written a plugin, you can easily modify and re-use it for other projects.

 

Do It Yourself Advantages

You only have the features you specifically need – no bloat

You can reduce database calls (and page load speed) by hard coding options

If there’s an issue or a conflict, you know where to find it

Once you’ve written it, you can use it over and over – just like any other plugin

An Example of DIY Savings

Scenario: Client wants to add various PayPal buttons throughout the site.

Solution 1:

Teach your client to create buttons in Paypal, find and copy the code into the text editor – every time they want to add a button.

Solution 2:

Find a plugin that creates a shortcode (this one makes 4 database calls to get options I know my client doesn’t need)

 

Solution 3:

Write a plugin that creates a shortcode

 

Client simply adds a shortcode every time they want a new button:

 

Some Easy DIY Plugins

  1. Custom Post Types
    • FAQs
    • Testimonials
    • Staff Members
  2. Share buttons
  3. Custom Login Logo
  4. Breadcrumbs

You’ve Decided to Do It Yourself

So, your next decision is whether to place your function in functions.php, or build a plugin.

From the client’s perspective

 

Does the function and the data need to survive a theme change?

 

No – functions.php

Yes – plugin

 


 

I always assume that at some point the client will want a new theme – or may hire another developer.

It’s just good practice to allow for that.

Some themes put site content into custom post types or content blocks created inside the theme. Not a best practice, in my book.

 

Some Examples

functions.php

plugin

theme features
sidebars
menus
widgetized areas
changes to <head>
custom excerpt lengths
anything that affects presentation of content

custom post types
shortcodes
analytics
dashboard customization
added avatar
custom profile fields

 

it depends

custom image sizes
scripts & styles
custom login styles

 


 

One more note: When I’m trying something I’m not completely sure about, even if it should go into the functions file, I put it in a plugin. Then, if it crashes, the whole site doesn’t go down, and I can just uninstall the plugin.

Let’s Create a Plugin

Following are three functions that could be placed in either a functions file, or a plugin, depending on the project and the client.

For my purposes here, I’ve chosen to include them in a plugin.

Add a Read More Tag to the End of Excerpts

Replace that confusing […] with a read more button.

 

 

 

Remove Query Strings from Script URLs

For load speed and clean URLs

Add a Custom Default Avatar Image

 

Upload the image to the media library. If you upload it to my-theme/images/ it will be lost with a new theme.

By default, avatars are 32px x 32px. If you want them larger, size your image accordingly.

Make the Plugin

1. Make a PHP file

2. Add the plugin header

3. Add your functions

4. Create a new folder in plugins directory

5. Upload the file (name it anything you like)

6. Activate

The New Plugin

 

Plugin Tip

To “hide” essential plugins so they don’t get de-activated accidentally:

Put your plugin.php file (not the whole folder) into a directory called “mu-plugins”.

 

 

Skip to toolbar