Using Actions and Filters in WordPress to Make a Plugin Your Own

17
Making a Plugin Your Own Using Actions and Filters in WordPress Brian Hogg brianhogg.com | @brianhogg WordCamp Toronto 2015

Transcript of Using Actions and Filters in WordPress to Make a Plugin Your Own

Page 1: Using Actions and Filters in WordPress to Make a Plugin Your Own

Making a Plugin Your OwnUsing Actions and Filters in WordPress

Brian Hoggbrianhogg.com | @brianhogg

WordCamp Toronto 2015

Page 2: Using Actions and Filters in WordPress to Make a Plugin Your Own

brianhogg.com

Subtitle

Page 3: Using Actions and Filters in WordPress to Make a Plugin Your Own

wpontheside.com

Subtitle

Page 4: Using Actions and Filters in WordPress to Make a Plugin Your Own
Page 5: Using Actions and Filters in WordPress to Make a Plugin Your Own

Actions and Filters

Page 6: Using Actions and Filters in WordPress to Make a Plugin Your Own

Actions

do_action( $identifier, [ $arg1, $arg2, ... ] )

add_action( $identifier, $function_name, [ $priority, $arg_count ] )

Page 7: Using Actions and Filters in WordPress to Make a Plugin Your Own

Actions (Example)do_action( 'the_plugin_action' );

function handle_the_plugin_action() {echo 'The action is happening now!';

}

add_action( 'the_plugin_action', 'handle_the_plugin_action' );

Page 8: Using Actions and Filters in WordPress to Make a Plugin Your Own

Filters

apply_filters( $identifier, $value, [ $arg1, $arg2, ... ] )

add_filter( $identifier, $function_name, [ $priority, $arg_count ] )

Page 9: Using Actions and Filters in WordPress to Make a Plugin Your Own

Filters (Example)

echo apply_filters( 'plugin_title', 'Title' );

function change_plugin_title( $title ) {return 'New Title';

}

add_filter( 'plugin_title', 'change_plugin_title' );

Page 10: Using Actions and Filters in WordPress to Make a Plugin Your Own

Filters (Example)

echo apply_filters( 'plugin_title', 'Title' );

function change_plugin_title( $title ) {return 'New ' . $title;

}

add_filter( 'plugin_title', 'change_plugin_title' );

Page 11: Using Actions and Filters in WordPress to Make a Plugin Your Own

How do you find what you can change in a plugin?

Search for do_action and apply_filters

Page 12: Using Actions and Filters in WordPress to Make a Plugin Your Own

The Events Calendar Plugin

•apply_filters•442 occurrences

•do_action•190 occurrences

Page 13: Using Actions and Filters in WordPress to Make a Plugin Your Own

How to Find The One You Need?▪ Look at the plugin documentation (if any)▪ Look at the code▪ Look at the HTML output and backtrack

Page 14: Using Actions and Filters in WordPress to Make a Plugin Your Own

DEMO

Page 15: Using Actions and Filters in WordPress to Make a Plugin Your Own

The Events Calendar▪ Message to verify location▪ Venue -> Location▪ Cost formatting

Page 16: Using Actions and Filters in WordPress to Make a Plugin Your Own

Can’t find the one you need?▪ (Nicely) ask the developer of the plugin

▪ Direct contact information / website▪ wordpress.org support forums

▪ Look for a paid support or Pro version

▪ Submit a patch

Page 17: Using Actions and Filters in WordPress to Make a Plugin Your Own

Go make plugins your own!