2016 WordCamp Pittsburgh - Let's Write a Plugin

Preview:

Citation preview

Let’s write a pluginStupid WordPress tricks Part 0.1A

WordCamp Pittsburgh 2016Brian Layman

HTTP://EHERMITSINC.COMhttp://slideshare.net/brianlayman

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

My World

(This word cloud replaces one I made on my own back in 2008 in which I foolishly referenced browser versions making it reach end of life when IE9 was no longer in beta. This cloud was ripped and edited from http://swordstudios.net/wp-content/themes/swordtheme/images/word-cloud.png with apologies to Jessie Friedman.

http://imgs.xkcd.com/comics/tech_support_cheat_sheet.png

OurDarkSecret

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

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

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

FilterIn 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

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

Ok – How are we doing?

Hello Dolly

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

Brian did miraculous stuff you can’t see in the downloaded slideshow• Bet you wish you were at Pittsburgh’s first 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…

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 );

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 )

Plugin Survival Kit

• http://wordpress.org/plugins/ - Even if your plugin exists, write it anyway• http://codex.wordpress.org/ - Your guide to all things WordPress

• http://codex.wordpress.org/Writing_a_Plugin - A starting point• http://codex.wordpress.org/Plugin_API/Action_Reference - Common actions• http://codex.wordpress.org/Plugin_API/Filter_Reference - List of common Filters

• https://developer.wordpress.org/plugins/ - The Plugin Developer handbook

Plugin Survival Kit• http://wordpress.org/plugins/about/ - Describes how to release your plugin

• https://wordpress.org/plugins/about/readme.txt - The official readme standard• https://wordpress.org/plugins/about/validator/ - Readme Validator• https://wordpress.org/plugins/add/ - Submit your plugin to the repository

• https://wordpress.org/plugins/hello-dolly/ - Hello Dolly!

• Unofficial Tools• http://sudarmuthu.com/wordpress/wp-readme/ - Readme Generator• http://generatewp.com/ - Helps add Features• http://wppb.io/ & http://wppb.me/ - Full set of starter files for a plugin

Brian Laymanhttp://eHermitsinc.comhttp://thecodecave.comhttp://www.slideshare.net/brianlaymanhttp://twitter.com/brianlayman @eHermitsMy info->your phone, Txt “ehermit” to 50500Brian@eHermitsInc.com

Recommended