an intorduction to wordpress plugin development

48

Upload: lime-canvas

Post on 08-May-2015

496 views

Category:

Technology


4 download

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

Page 2: An Intorduction to WordPress Plugin Development

• 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!

Page 3: An Intorduction to WordPress Plugin Development

• 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

Page 4: An Intorduction to WordPress Plugin Development

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)

Page 5: An Intorduction to WordPress Plugin Development

• 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

Page 6: An Intorduction to WordPress Plugin Development

Name

Give your plugin a unique and relevant name

e.g. “Call to Action Shortcode Buttons”

Not: CTASB1 Whaaat?

Page 7: An Intorduction to WordPress Plugin Development

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

Page 8: An Intorduction to WordPress Plugin Development

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

Page 9: An Intorduction to WordPress Plugin Development

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

Page 10: An Intorduction to WordPress Plugin Development

Other Files (if uploading to WordPress.org)

Screen Shots

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

Linked to from readme.txt file

Page 11: An Intorduction to WordPress Plugin Development

Example File & Folder

/wp-contents

/plugins

/call-to-action-shortcode-buttons

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

|readme.txt

|screenshot-1.png

Page 12: An Intorduction to WordPress Plugin Development

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

*/

Page 13: An Intorduction to WordPress Plugin Development
Page 14: An Intorduction to WordPress Plugin Development

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]

Page 16: An Intorduction to WordPress Plugin Development
Page 17: An Intorduction to WordPress Plugin Development
Page 18: An Intorduction to WordPress Plugin Development
Page 19: An Intorduction to WordPress Plugin Development
Page 20: An Intorduction to WordPress Plugin Development

Always stop people from accessing your PHP plugin files directly.

Page 21: An Intorduction to WordPress Plugin Development

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

Page 22: An Intorduction to WordPress Plugin Development

Shortcode that accepts arguments or use default args.

Page 23: An Intorduction to WordPress Plugin Development

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.

Page 24: An Intorduction to WordPress Plugin Development

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

Page 25: An Intorduction to WordPress Plugin Development

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

The HTML output

Page 26: An Intorduction to WordPress Plugin Development

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

Page 27: An Intorduction to WordPress Plugin Development

Remember

Never ‘echo’ output in a shortcode.

‘Return’ the output from your function

Shortcodes are not recursive by default

Page 28: An Intorduction to WordPress Plugin Development

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

Page 30: An Intorduction to WordPress Plugin Development

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.

Page 31: An Intorduction to WordPress Plugin Development

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.

Page 32: An Intorduction to WordPress Plugin Development

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.

Page 33: An Intorduction to WordPress Plugin Development

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.

Page 34: An Intorduction to WordPress Plugin Development

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

Page 35: An Intorduction to WordPress Plugin Development

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)

Page 36: An Intorduction to WordPress Plugin Development

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.

Page 37: An Intorduction to WordPress Plugin Development

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

Page 38: An Intorduction to WordPress Plugin Development
Page 39: An Intorduction to WordPress Plugin Development
Page 40: An Intorduction to WordPress Plugin Development
Page 41: An Intorduction to WordPress Plugin Development

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

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

Page 42: An Intorduction to WordPress Plugin Development
Page 43: An Intorduction to WordPress Plugin Development
Page 44: An Intorduction to WordPress Plugin Development

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

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

Page 45: An Intorduction to WordPress Plugin Development

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”

Page 46: An Intorduction to WordPress Plugin Development

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

Page 47: An Intorduction to WordPress Plugin Development

• [1] wikimedia.org

• [2] imgflip.com

• [13] imgflip.com

Page 48: An Intorduction to WordPress Plugin Development