the Right Way to Extend WordPress
WordPress Core Contributor, WordPress Kaunas Meetup co-organizer, WordCamp (Lithuania, Riga, Stockholm, Jyväskylä) speaker and one of the editors of the Lithuanian WordPress translation team.
Free & premium WordPress plugin developer:
Lecturer at Kaunas College.
deployer.seravo.com - a service that syncs WordPress plugins from GitHub to WordPress.org automatically.
TryoutWP.com - a service to spin up live temporary demo sites for WordPress plugins and themes.
A plugin in WordPress.org repository;
<?php
/*
Plugin Name: Health Check
Plugin URI: https://wordpress.org/plugins/health-check/
Description: Checks the health of your WordPress install
Version: 0.1.0
Author: The Health Check Team
Author URI: http://health-check-team.example.com
Text Domain: health-check
Domain Path: /languages
*/
?>
MVC
Event-driven
images by Tom McFarlin
Actions are points in the WordPress lifecycle that allow you to add, remove, or modify certain functionality.
- Tom McFarlin
You, as a developer, can hook a custom function to be executed at that particular point.
<?php add_action( 'wp_head', 'my_function' ); ?>
This line will execute my_function when WordPress is printing it's code in the <head> part of HTML document.
<?php
add_action( 'wp_head', 'my_function' );
function my_function() {
echo '<!-- hello! -->';
}
?>
The above code will print a '<!-- hello! -->' comment in the <head> part of any WordPress website.
Filters, on the other hand, are points in the WordPress lifecycle in which you can add, remove, or modify data.
- Tom McFarlin
You, as a developer, can hook a custom function to modify that piece of data.
<?php add_filter( 'the_title', 'title_filter' ); ?>
This line will make WordPress to pass article title to title_filter function and use the return value whenever WordPress prints an article title.
<?php
add_filter( 'the_title', 'title_filter' );
function title_filter( $title ) {
$title = $title . ' :)';
return $title;
}
?>
The above code will make WordPress display a ':)' at the end of every post/page/menu item title.
<?php
add_filter( 'the_title', 'function_1', 10 );
add_filter( 'the_title', 'function_2', 9 );
add_filter( 'the_title', 'function_3', 90 );
?>
Functions will be executed in this order: function_2, function_1, function_3
<?php
add_filter( 'the_title', 'function_1', 10, 2 );
add_filter( 'the_title', 'function_2', 10, 1 );
add_filter( 'the_title', 'function_3', 10 );
?>
function_1 will receive 2 arguments (post title and post ID), while function_2 and function_3 will only get post title.
Note: filter functions will always receive one argument.
An awesome plugin by John Blackbourn
Top Menu > Hooks
Main action hooks in their execution order;
Together with all the functions that are attached to those hooks;
You can also add hooks to Your own code, to make it extendable and/or leverage features of other plugins or WordPress Core.
<?php
// trigger `wp_head` action
do_action( 'wp_head' );
// trigger `the_title` filter
$title = apply_filters( 'the_title', $title, $post_id );
?>
Finally, You can also create Your own custom hooks, specific to Your code. Just use Your own hook name.
<?php
// trigger `arunas_action` action
do_action( 'arunas_action' );
// trigger `arunas_filter` filter
$data = apply_filters( 'arunas_filter', $data );
?>