An Intorduction to WordPress Plugin Development
-
Upload
lime-canvas -
Category
Technology
-
view
509 -
download
4
description
Transcript of An Intorduction to WordPress Plugin Development
![Page 2: An Intorduction to WordPress Plugin Development](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/2.jpg)
• 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](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/3.jpg)
• 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](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/4.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/5.jpg)
• 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](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/6.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/7.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/8.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/9.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/10.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/11.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/12.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/13.jpg)
![Page 14: An Intorduction to WordPress Plugin Development](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/14.jpg)
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 15: An Intorduction to WordPress Plugin Development](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/15.jpg)
DEMO
Unfortunately the screencast failed
Zip of Plugin at
https://dl.dropboxusercontent.com/u/6105671/shortcode-basic.zip
![Page 16: An Intorduction to WordPress Plugin Development](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/16.jpg)
![Page 17: An Intorduction to WordPress Plugin Development](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/17.jpg)
![Page 18: An Intorduction to WordPress Plugin Development](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/18.jpg)
![Page 19: An Intorduction to WordPress Plugin Development](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/19.jpg)
![Page 20: An Intorduction to WordPress Plugin Development](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/20.jpg)
Always stop people from accessing your PHP plugin files directly.
![Page 21: An Intorduction to WordPress Plugin Development](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/21.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/22.jpg)
Shortcode that accepts arguments or use default args.
![Page 23: An Intorduction to WordPress Plugin Development](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/23.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/24.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/25.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/26.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/27.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/28.jpg)
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 29: An Intorduction to WordPress Plugin Development](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/29.jpg)
DEMO Unfortunately the screencast failed
Zip of Plugin at
https://dl.dropboxusercontent.com/u/6105671/my-branding.zip
![Page 30: An Intorduction to WordPress Plugin Development](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/30.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/31.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/32.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/33.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/34.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/35.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/36.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/37.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/38.jpg)
![Page 39: An Intorduction to WordPress Plugin Development](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/39.jpg)
![Page 40: An Intorduction to WordPress Plugin Development](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/40.jpg)
![Page 41: An Intorduction to WordPress Plugin Development](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/41.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/42.jpg)
![Page 43: An Intorduction to WordPress Plugin Development](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/43.jpg)
![Page 44: An Intorduction to WordPress Plugin Development](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/44.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/45.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/46.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/47.jpg)
• [1] wikimedia.org
• [2] imgflip.com
• [13] imgflip.com
![Page 48: An Intorduction to WordPress Plugin Development](https://reader035.fdocuments.net/reader035/viewer/2022081400/554beb5db4c90556328b4fae/html5/thumbnails/48.jpg)