1. LET’S WRITE A PLUGIN
STUPID WORDPRESS TRICKS PART 0.1A
BRIAN LAYMAN
HTTP://EHERMITSINC.COM
HTTP://SLIDESHARE.NET/BRIANLAYMAN
2. • Brian Layman
• I work from home
• I spend all day on the computer
• All best my friends are online
• I don’t hang out at parties
• I spend my day in a cave like room
• Yes, I am an eHermit
http://eHermitsInc.com
INTRODUCTION
5. BEFORE WE START TO PLAY
• What is a plugin?
• What can plugins do?
• What do you need to know?
• “But I heard plugins make your site slow!”
• “But I heard that plugins make your site insecure!”
6. PLUGIN SURVIVAL KIT
• http://wordpress.org/plugins/ - Look for a plugin that is similar
• http://wordpress.org/plugins/about/ - Describes how to create a plugin
• http://codex.wordpress.org/ - Your guide
• http://codex.wordpress.org/Writing_a_Plugin
• http://codex.wordpress.org/Plugin_API/Action_Reference - When actions are called
• http://codex.wordpress.org/Plugin_API/Filter_Reference - List of common Filters
• http://themergency.com/generators/wordpress-plugin-boilerplate/ - Basics
• http://generatewp.com/ - Helps add Features
7. PLUGIN
• A Plugin is a group of php functions that can extend the functionality present in a standard
WordPress weblog. These functions may all be defined in one php file, or may be spread
among more than one file. Usually, a plugin is a php file that can be uploaded to the "wp-
content/plugins" directory on your webserver, where you have installed WordPress. Once you
have uploaded the plugin file, you should be able to "turn it on" or Enable it from the "Plugins"
page in the administration interface of your weblog. The WordPress source code contains
hooks that can be used by plugins.
• See also: Hack, Hacking
• Related articles: Plugins
http://codex.wordpress.org/Glossary
8. HOOK
• Hooks are specified, by the developer, in Actions and Filters. Here is a (hopefully) complete
list of all existing Hooks within WordPress.
Because Hooks are required by Actions and Filter you may hear the phrase "Action Hooks" and
"Filter Hooks" used from time to time.
In technical and strict terms: a Hook is an event, i.e. event as understood by Observer pattern,
invoked by the do_action() orapply_filters() call that afterwards triggers all
the action or filter functions, previously hooked to that event usingadd_action() or add_filter(),
respectively.
•See also: Action, Filter
•Related articles: Hooks, Actions and Filters, Plugin API/Hooks
http://codex.wordpress.org/Glossary
9. FILTER
In WordPress, a Filter is a function that is associated with an existing Action by
specifying any existing Hook.
Developers can create custom Filters using the Filter API to replace code from
an existing Action. This process is called "hooking".
Custom Filters differ from custom Actions because custom Actions allow you
to add or remove code from existing Actions. Whereas custom Filters allow you
to replace specific data (such as a variable) found within an existing Action.
•See also: Action, Hook, Terminology Confusion
•Related articles: Filters, Filter Reference, add_filter()
http://codex.wordpress.org/Glossary
10. ACTION
• In WordPress; an Action is a PHP function that is executed at specific points throughout the
WordPress Core.
Developers can create a custom Action using the Action API to add or remove code from an
existing Action by specifying any existing Hook. This process is called "hooking".
For example: A developer may want to add code to the footer of a Theme. This could be
accomplished by writing new function, then Hooking it to the wp_footer Action.
Custom Actions differ from custom Filters because custom Actions allow you to add or
remove code from existing Actions. Whereas custom Filters allow you to replace specific data
(such as a variable) found within an existing Action.
•See also: Filter, Hook, Terminology Confusion
•Related articles: Actions, Action Reference, add_action()
•Forum posts: Filters vs. Actions Discussion and Explanation
http://codex.wordpress.org/Glossary
13. LET’S PLAY
• We’re going to use the tools we have seen to create a plugin
• Let’s mess with Dolly’s head a little bit
• Using actions
• Using filters
• Including CSS
• Including Scripts
14. BRIAN DID MIRACULOUS STUFF YOU CAN’T SEE IN
THE DOWNLOADED SLIDESHOW
• Bet you wish you were at Ohio’s 2014 North Canton WordCamp
• Then you could have seen us do all the fun stuff we did
• We’re sorry you missed it
• Maybe you can make it next year..
• OK next slide…
15. FIGURING OUT WHERE YOU MESSED UP
• http://codex.wordpress.org/Editing_wp-config.php
@ini_set( 'log_errors', On' );
@ini_set( 'error_log', '/home/example/php_error.log' );
@ini_set( 'display_errors', 'On' );
@ini_set( 'error_reporting', E_ALL );
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', false );
define( 'WP_DEBUG_DISPLAY', true );
16. DOING THINGS RIGHT
• Data Validation
• https://codex.wordpress.org/Data_Validation
• esc_attr, wp kses, esc_url
• Use $wpdb for database access
• get_var, get_row, insert, update, delete
• Use prepare for custom queries
• $wpdb->query( $wpdb->prepare(“select blah from foo where i= %d”, $myintvar )