Post on 05-Jan-2016
description
Hooking into WordPress
Craig Ralston
craig@websolutions.com
Why use hooks?
• Extend core functionality
• Allow others to alter your work
craig@websolutions.com
What are hooks?
ActionsDO something new
• do_action()• add_action()• has_action()• remove_action()
FiltersMODIFY existing data
• apply_filters()• add_filter()• has_filter()• remove_filter()
craig@websolutions.com
Actions – How are they used?
Add an alert to the admin area using a core hook
function cor_admin_notice() { ?>
<div class="cor_alert">
<h3>Hello World!</h3>
</div>
<?php }
add_action( 'admin_notices', 'cor_admin_notice' );
craig@websolutions.com
Core hook Our function
add_action( $hook, $function, $priority, $args );
$hook – (required) Name of the hook you are attaching to
$function – (required) Name of the function you wish to call
$priority – (optional) Prioritize multiple functions on the same hook
$args – (optional) Number of arguments $function takes
add_action( 'admin_notices', 'cor_admin_notice' );
craig@websolutions.com
Creating Actions
function cor_admin_notice() { ?>
<div class="cor_alert">
<h3>Hello World!</h3>
</div>
<?php }
craig@websolutions.com
function cor_admin_notice() {
echo '<div class="cor_alert">';
do_action( 'before_alert_text' );
echo apply_filters( 'filter_alert_text', '<h3>Hello World</h3>');
do_action( 'after_alert_text' );
echo '</div>';
}
Action
Filter
Action
craig@websolutions.com
Let’s add in our own stuff now
Add our image before the alert message
function add_image_above_alert() {
$image_src = '/images/alert.png';
echo '<img src="'. $image_src . '" alt="alert image" />';
}
add_action( 'before_alert_text', 'add_image_above_alert');
Add a paragraph after the alert message
function add_text_below_alert() {
echo '<p>Some really awesome text</p>';
}
add_action( 'after_alert_text', 'add_text_below_alert');
craig@websolutions.com
function cor_admin_notice() {
echo '<div class="cor_alert">';
do_action( 'before_alert_text' );
echo apply_filters( 'filter_alert_text', '<h3>Hello World</h3>');
do_action( 'after_alert_text' );
echo '</div>';
}
Action
Filter
Action
craig@websolutions.com
echo apply_filters( 'filter_alert_text', '<h3>Hello World</h3>');
apply_filters( $hook, $value, $var );
$hook – (required) Name of the filter hook
$value – (required) Value to be modified
$var – (optional) One or more additional variables passed to the filter functions
craig@websolutions.com
Let’s change the original alert message
function change_alert_text( $text ) {
$text = '<h3>Our new custom alert message</h3>';
return $text;
}
add_filter( 'filter_alert_text', 'change_alert_text', 10, 1 );
craig@websolutions.com
function cor_admin_notice() {
echo '<div class="cor_alert">';
do_action( 'before_alert_text' );
echo apply_filters( 'filter_alert_text', '<h3>Hello World</h3>');
do_action( 'after_alert_text' );
echo '</div>';
}
Action
Filter
Action
craig@websolutions.com
Filter profanity in the content area
function cor_filter_profanity( $content ) {
// Store an array of what we want to consider profanity
$profanities = array( 'badword','alsobad','reallybad' );
// Check the content for profanities before output
// Replace them with {censored}
$content = str_ireplace($profanities, '{censored}', $content);
return $content;
}
add_filter ( 'the_content', 'cor_filter_profanity', 10, 1 );
craig@websolutions.com
Action or Filter?
• When Sarah arrives home, tell her to call me.
• When Sarah makes her grocery list, make sure she adds beer to the list
craig@websolutions.com
When Sarah gets home, have her call me.
// When Sarah gets home, have her call me.
add_action( 'after_sarah_arrives' , 'sarah_call_craig', 10 , 1 );
function sarah_call_craig( $sarah_has_phone ) {
// if $sarah_has_phone is true
if ( $sarah_has_phone ) {
echo 'Sarah, would you please call Craig? Thanks.';
}
}
craig@websolutions.com
When Sarah makes her grocery list, add beer.
// Modify Sarah's grocery list
add_filter( 'sarahs_grocery_list' , 'cor_add_beer', 10, 1 );
function cor_add_beer( $grocery_list ) {
// Append another item to the list
$grocery_list = $grocery_list . ' Beer.';
return $grocery_list;
}
craig@websolutions.com
Helpful Resources
http://codex.wordpress.org/Plugin_API
http://adambrown.info/p/wp_hooks
craig@websolutions.com
Questions?