introduction to plugin development

53
@Josh412 Introduction To Plugin Development Josh Pollock | CalderaLabs.org

Upload: josh-pollock

Post on 13-Jan-2017

550 views

Category:

Internet


1 download

TRANSCRIPT

@Josh412

Introduction To Plugin Development

Josh Pollock | CalderaLabs.org

@Josh412

CalderaLabs.org

Hi I'm JoshLead Developer: CalderaWP

I make WordPress pluginsI teach about WordPressI wrote a book about the WordPress REST APII am core contributor to WordPressI am a member of The WPCrowd

@Josh412

CalderaLabs.org

You Should Make Plugins

@Josh412

CalderaLabs.org

Turn Code OffEasy troubleshooting!

@Josh412

CalderaLabs.org

Reuse CodeShare Between Projects

@Josh412

CalderaLabs.org

Share CodeDistribute on Github or WordPress.org

@Josh412

CalderaLabs.org

Make $$Sell Plugins

@Josh412

CalderaLabs.org

What Is A Plugin?

@Josh412

How To Make A Plugin

Step 1: Add a new directory to the plugins directory (optional).

Step 2: Add a file in that directory or the plugins directory with a valid plugin header.

Step 3: Write code (optional).

@Josh412

CalderaLabs.org

Plugins AreContainers For Code

@Josh412

CalderaLabs.org

Plugins vs ThemesOr Can’t I Put That In

functions.php ??

@Josh412

CalderaLabs.org

It’s a free software, you can do whatever you

want, but...

@Josh412

Plugins vs Themes: Best Practices

Themes should be use to present a unique design

Everything else should be in a plugin.

@Josh412

Should This Code Go In functions.php ?

Is the answer to any of these questions no:

Do I ever want to reuse this code apart from this theme?Will I miss this code when I switch themes?

Switch for troubleshooting.Switch for new look.

Can I use this code for other purposes?

@Josh412

CalderaLabs.org

Making PluginsOrganization

@Josh412

Plugin Header: Goes In Main File

<?php/*Plugin Name: My Plugin*/

@Josh412

Main File: The File With A Plugin Header

Plugin HeaderLicense/ Copyright/ Contact InfoCheck Dependencies & RequirementsLoad PluginStart Plugin

@Josh412

Directory Structure

There Are No RulesYou Should Have RulesI like PSR-4 :)

@Josh412

Naming Things

There Are No RulesYou Should Have Rules

I like to document naming conventions:)

@Josh412

Naming Things: naming-conventions.txt

Plugin Name -- Text domain -- Function prefix -- Class prefix -- Root namespace -- Hook Prefix --

@Josh412

CalderaLabs.org

Making PluginsThe Plugins API

@Josh412

CalderaLabs.org

Hooks Are EventsWhen We Get Here:

Do This

@Josh412

CalderaLabs.org

Don’t

Hack Core

Modifying Core/ Plugins / Themes Is A Bad Idea

You Will Lose Your Changes On Update

@Josh412

CalderaLabs.org

Hooks Let You Change WordPress/ Plugins/

ThemesWithout Editing Files

@Josh412

CalderaLabs.org

FiltersChange A Variable’s Value

At A Specific Time

@Josh412

CalderaLabs.org

ActionsDo Something

At A Specific Time

@Josh412

ActionsDo something

Don’t return values*

Actions Vs Filters

FiltersChange something

Must return a value*

@Josh412

CalderaLabs.org

Use Hooks In Your PluginsTo Add New Functionality

@Josh412

CalderaLabs.org

Add Hooks In Your PluginsTo Make Your Plugin

Extensible

@Josh412

do_action()

Triggers the action

Using Actions

add_action()

Hooks a function to an action

@Josh412

Using Actions: Print Tracking Pixel

function my_prefix_pixel() { echo '<img src="https://adnetwork.com/pixel.gif" width="0" height="0">';}

add_action( 'wp_footer', 'my_prefix_pixel' );

@Josh412

Using Actions: Modify WP_Query

function search_filter($query) { if ( !is_admin() && $query->is_main_query() ) { if ($query->is_search) { $query->set('post_type', array( 'post', 'movie' ) ); } }}

add_action('pre_get_posts','search_filter');

@Josh412

Using Actions

@Josh412

apply_filters()

Creates the filter

Using Filters

add_filter()

Hooks a function to a filter

@Josh412

Using Filters: Add To Content

add_filter( 'the_content', 'slug_callback' );

function slug_callback( $content ){

return $content . '<p class="cta">Hey Sign Up for My Thing!</p>';

}

@Josh412

Using Filters: Add To Content

add_filter( 'the_content', function( $content ){

return $content . '<p class="cta">Hey Sign Up for My Thing!</p>';

});

@Josh412

Creating Filters: Before

function prefix_get_form( $id ){

return get_option( $id, [] ); }

@Josh412

Creating Filters: After

function prefix_get_form( $id ){

$form = get_option( $id, [] ); $form = apply_filters( 'prefix_get_form', $form, $id ); return $form; }

@Josh412

Hook Priority

add_filter( 'name','callback', 2 );

add_filter( 'name', 'callback_two', 55 );

@Josh412

Hook Arguments

$form = apply_filters( 'prefix_get_form', $form, $id );

add_filter( 'prefix_get_form', function( $form, $id ){

if( 'CF1234567' == $id ){

// do something to $form

}

return $form;

}, 10, 2 );

@Josh412

CalderaLabs.org

Making PluginsJosh’s Rules For Plugin

Development

@Josh412

CalderaLabs.org

Security Is Not Optional

@Josh412

CalderaLabs.org

Trust No Inputs

@Josh412

HTTP requestsThat functions will be used how you intended themData returned from the databaseYourself

Some Things Not To Trust

@Josh412

CalderaLabs.org

Write For Reuse

@Josh412

CalderaLabs.org

Use Version Control

@Josh412

CalderaLabs.org

Commit EarlyCommit Often

@Josh412

Single Responsibility PrincipleFunctions should do one thing.

Do Not Repeat Yourself (DRY)Functions, not copypaste

Making Code Reusable

@Josh412

CalderaLabs.org

Abstraction!!

@Josh412

CalderaLabs.org

Using Classes Doesn’t Make Your

Code Object-Oriented

@Josh412

CalderaLabs.org

Use Object-Oriented Programming

When Appropriate

@Josh412

CalderaLabs.org

Small Simple Classes Are Good

@Josh412

CalderaLabs.org

Josh Pollock

JoshPress.net

CalderaLabs.orgCalderaWP.com

IngotHQ.com@Josh412