Altering the way WordPress works rarely requires changing any of the core WordPress code. The same often holds true for altering plugins and themes. That’s due to action and filter hooks, they provide a way to add your own code without changing the original code, meaning your added code will generally be safe when updates of the original code are made.
Most WordPress developers will provide various hooks in their plugin or theme code so that other developers can extend their built-in features, or so they can extend them later themselves.
The following are the two functions WordPress provides for developers to add hooks:
- do_action – plugins will use this to create actions for others to tie into.
- apply_filters – plugins will use this to create filters for others to manipulate data or content used by the plugins.
The difference between the two (actions and filters) is that any arbitrary code can be executed with action hooks, whereas filters let developers alter specific data or content.
Using existing hooks
The following two functions allow developers to tie into the action and filter hooks defined by the functions above:
- add_action – using this, you can tie into actions created with do_action.
- add_filter – using this, alter content made available through apply_filters.
More about hooks at the WordPress codex: Hooks, Actions and Filters
A simple, core example
A simple example would be to use the_content filter hook built into WordPress to easily replace certain content. In the following case, I’ve searched for just the first occurrence of the word ‘WordPress’ within all post content and added a link to WordPress.org.
In the WordPress core, in the file wp-includes/post-template.php, you’ll see a line like so:
$content = apply_filters( 'the_content', $content);
We tie into that defined filter using the add_filter function in the example, and simply reference a new function containing the code we’d like to run. Then, every time WordPress makes use of the_content hook, our code will be run!
You can browse and search through all the core hooks here: https://developer.wordpress.org/reference/hooks/
As a quick plugin example, MarketPress e-commerce plugin can be extended to give a price discount based on criteria such as the user’s role on the site. You’ll see a method to achieve that as follows:
You’ll see it uses a filter hook called ‘mp_product_price‘. That’s defined in the plugin file marketpress.php like so:
$price = apply_filters( 'mp_product_price', $price, $product_id );
MarketPress was designed to allow code-based price changes and provides two values for that operation, $price and $product_id. we’ve just tapped into that and returned an adjusted $price variable.
Adding code to WordPress
The best way to add code to WordPress is usually by creating a plugin, or by adding it to your theme’s functions.php file. This would generally involve creating the code in a text editor (I’d recommend Atom), then uploading that to your site via FTP.
It’s also possible to add code through plugins such as Code Snippets. I’ve often used it in the past myself, for quick testing purposes. There’s just an added layer of concern because if your code is problematic, it could cause your site to be inaccessible, so doing it by FTP would provide a way to quickly revert back.
I hope that’s all intuitive, please feel free to ask any questions. 🙂