an intorduction to wordpress plugin development

Post on 08-May-2015

496 Views

Category:

Technology

4 Downloads

Preview:

Click to see full reader

DESCRIPTION

Slides from my Plugin Development talk at WordPress Sydney May 2014. I cover the basics to get you started in WordPress plugin development, looking at implementing shortcodes and then a simple branding plugin to modify the Dashboard areas and the login box.

TRANSCRIPT

• Adds extra functionality to your website

• Keeps functionality away from your theme – You could add code to your theme’s functions.php

– You lose that functionality if you change theme

• Avoids hacking the core

• It could earn you money

• It’s fun!

• We’ll look at creating some shortcodes

• Basic shortcodes

• Shortcodes with attributes (inputs)

• Then we’ll look at building a “branding” plugin

– Replace login logo & links

– Remove & filter Dashboard items

There are two types of plugins.

Regular These are the normal plugins you will find in the plugin repo at wordpress.org. You can install, activate, deactivate, update and delete from Dashboard

Must Use Have to be installed manually (ftp/unzip).

Can’t uninstall, update or delete from Dashboard

Useful for supplying critical functionality (user tamper proof)

• Single PHP file in mu-plugins folder – i.e. they don’t have their own plugin folder within mu-

plugins

• MU-Plugins activate before regular plugins

• They are always on

• They don’t call activation hooks

– Some regular plugins setup data on activation and remove data on removal/deactivation

Name

Give your plugin a unique and relevant name

e.g. “Call to Action Shortcode Buttons”

Not: CTASB1 Whaaat?

Location

Plugins sit in /wp-content/plugins (default)

MU-Plugins sit in /wp-content/mu-plugins (default)

Note Default locations can be overridden by wp-config.php defines. Don’t assume or hardcode these paths into your plugin

Folder & Filename

Choose a unique folder name (usually same as bootstrap file)

e.g. /call-to-action-shortcodes-button/

Plugins must have a bootstrap (startup) PHP file in it’s folder. Follow same naming convention as the folder

e.g. call-to-action-shortcodes-button.php

Other Files (if uploading to WordPress.org)

Read Me File

readme.txt http://wordpress.org/plugins/about/readme.txt

http://generatewp.com/plugin-readme/

Contains all info used to populate plugin page

Other Files (if uploading to WordPress.org)

Screen Shots

Usually screenshot-1.png, screenshot-2.png

Linked to from readme.txt file

Example File & Folder

/wp-contents

/plugins

/call-to-action-shortcode-buttons

|call-to-action-shortcode-buttons.php

|readme.txt

|screenshot-1.png

Needs a header <?php

/**

* Plugin Name: Name Of The Plugin

* Plugin URI: http://URI_Of_Plugin_Page

* Description: A brief description of the Plugin.

* Version: The Plugin's Version Number, e.g.: 1.0

* Author: Name Of The Plugin Author

* Author URI: http://URI_Of_The_Plugin_Author

* License: A "Slug" license name e.g. GPL2

*/

Three uses for shortcodes

1. On its own e.g. [company_address]

2. With attributes e.g. [button text=“Buy Now”]

3. Surrounding content e.g. [make_red]Blah blah blah[/make_red]

Always stop people from accessing your PHP plugin files directly.

No arguments. Just outputs from a variable.

Prefix your functions with a unique prefix

“lc_” for us stands for Lime Canvas

First argument to add_shortcode is the shortcode name, second is the function name

Shortcode that accepts arguments or use default args.

Shortcode applied to content.

i.e. [make_red]This is red text[/make_red]

Lines 81 & 96 use do_shortcode() making them recursive in nature.

Wrap this shortcode around some content and it will only be shown if users are logged in.

Obfuscate email and “hide” it from web bots looking for emails to spam. Uses WordPress’ antispambot() function.

The HTML output

This shortcode only shows content within 24 hours of the post date, thereafter outputting an expired message.

Remember

Never ‘echo’ output in a shortcode.

‘Return’ the output from your function

Shortcodes are not recursive by default

Brand Your Clients Dashboard

• Custom login logo

• Remove “helpful” login errors

• New Dashboard footer

• Remove Dashboard widgets

– Primary, Quick Draft

• Remove WordPress “node” from Admin Bar

Sets up some global variables

plugin_slug = “my-branding”

plugin_url = http://wpsyd-local/wp-content/plugins/my-branding

Having global variables makes it easier and safer to reference in later functions.

You can and should hook into plugin activation and deactivations.

This activation stores the plugin version number in the DB options table using the plugin slug as the option name. Nothing on deactivation yet.

Clean up your mess when uninstalling plugin.

You should give users the choice if keeping or deleting the data (as an settings option).

This plugin deletes the DB option we set on activation. Nice and clean now.

The “plugins_loaded” action is one of the earliest hooks you can use. Plugins loaded but not pluggable function. WordPress hasn’t executed anything yet.

Some WordPress features not available here though. We don’t use it here.

The “init” action is a popular hook to use for applying your plugin functions.

Most of WordPress has now fully loaded and you have access to functions for post types and the database.

We use this hook to apply most of our branding functions

Here we run functions to: – Replace default login logo with our own logo

– Replace the login logo header title and URL with our own

– Replace the login error text (it’s a bit too helpful for hackers)

We use “admin_menu” hook to apply our functions which apply changes to the admin Dashboard.

We don’t want to use init because we only want our functions to be run in the Dashboard, not on the front-end.

We could have also used the “admin_init” hook.

Here we run functions to: – Replace the Dashboard footer text

– Remove some Dashboard meta boxes such as Quick Draft and WordPress News

– Remove the WordPress menu “node” from the Admin Bar

Here’s the structure and files for our two test plugins.

Very basic but good learning guides for unleashing your plugin potential.

We don’t need to give hackers any additional help!

WordPress logo links to wordpress.org and has the title “Powered by WordPress”

Login error is now general.

Our logo replaces WordPress logo.

Links to our www.limecanvas.com website.

Title reads “<site-title> - Powered by Lime Canvas”

Hopefully you can see that plugin development isn’t as scary as you may have thought it to be.

Learn PHP

Learn WordPress API

Make Amazeballs plugins

• [1] wikimedia.org

• [2] imgflip.com

• [13] imgflip.com

top related