Let’s write a plugin

Post on 06-May-2015

304 views 1 download

Transcript of Let’s write a plugin

LET’S WRITE A PLUGINSTUPID WORDPRESS TRICKS PART 0.1A

BRIAN LAYMANHTTP://EHERMITSINC.COMHTTP://SLIDESHARE.NET/BRIANLAYMAN

• 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

MY WORLD

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

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: Pluginshttp://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

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

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 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…

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 )

BRIAN LAYMAN

http://eHermitsinc.com

http://thecodecave.com

http://www.slideshare.net/brianlayman

http://twitter.com/brianlayman

@eHermits

My info->your phone, Txt “ehermit” to 50500

Brian@eHermitsInc.com