To use action hooks and filters we need to create a function in your themes functions.php (or child theme) as described below.
Using WordPress action hooks
Action Hooks are designated points in WordPress core, themes or plugins that are designed to allow users to insert custom code at that designated point.
An example of this would be adding a banner notice at the top of each of your blog posts. Canvas has a wide selection of custom action hooks, available for use with the Hook Manager as well as through custom functions. An example of an action hook, using our banner notice example from above, would look as follows if done using a custom functio:
add_action( 'woo_header_after', 'woo_add_banner_notice' );
function woo_add_banner_notice () {
// Echo an alert box
echo '
Welcome to our blog!
';
} // End woo_add_banner_notice()The above code snippet uses the Canvas hook, `woo_header_after`, to display the alert box below our #header DIV tag. The same HTML (without the PHP jargon) could also be inserted into the appropriate box in the Hook Manager, which would read as follows:
Welcome to our blog!
Using WordPress Filters:
A WordPress filter does what it says on the tin- it filters. A filter is a function that takes in one or more values, changes them depending on a condition and returns the modified result. An example of this would be to add a line or text (or a hyperlink, or a signature sign-off… whatever you’d like) to the end of the content of each of your blog posts. Filter hooks can also be used for truncating text, changing formatting of content, or just about any other programming manipulation requirement (for example, adding to or overriding an array of values).
Custom code is added as a filter using the `add_filter()` function. The following code adds a sign-off to the end of each blog post, only when viewing the full blog post screen:
add_filter( 'the_content', 'woo_filterhook_signoff' );
function woo_filterhook_signoff ( $content ) {
if ( is_single() ) {
$content .= '
Th-th-th-th-th That\'s all, folks!
' . "\n";
} // End IF Statement
return $content;
} // End woo_filterhook_signoff()The above code adds a new DIV tag to the end of the content of our blog post, only when on a single blog post screen.
Using pluggable functions:
A pluggable function is a function that can be overwritten by a child theme. This is done by coding the function into your child theme and making the necessary changes, as desired.
Take, for example, the woo_post_more() function, found in `theme-actions.php`, which reads as follows:
if ( ! function_exists( 'woo_post_more' ) ) {
function woo_post_more () {
if ( get_option( 'woo_disable_post_more' ) != 'true' ) {
$html = '';
?>
<?php
if ( get_option('woo_post_content') == 'excerpt' ) { $html .= '[view_full_article after=" •"] '; }
$html .= '[post_comments]';
$html = apply_filters( 'woo_post_more', $html );
echo $html;
?>
<?php
}
} // End woo_post_more()
}The IF statement surrounding the function is what makes it pluggable. If this IF statement isn’t wrapping the function you want to modify, it isn’t pluggable. If we wish to override this function in a child theme, we’d locate the function from the main theme, copy the code into our child theme’s `functions.php` file and make any changes we desire.