Hooking into WordPress
description
Transcript of Hooking into WordPress
![Page 3: Hooking into WordPress](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813b21550346895da3daf8/html5/thumbnails/3.jpg)
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()
![Page 4: Hooking into WordPress](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813b21550346895da3daf8/html5/thumbnails/4.jpg)
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' );
Core hook Our function
![Page 5: Hooking into WordPress](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813b21550346895da3daf8/html5/thumbnails/5.jpg)
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' );
![Page 6: Hooking into WordPress](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813b21550346895da3daf8/html5/thumbnails/6.jpg)
Creating Actions
function cor_admin_notice() { ?>
<div class="cor_alert">
<h3>Hello World!</h3>
</div>
<?php }
![Page 7: Hooking into WordPress](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813b21550346895da3daf8/html5/thumbnails/7.jpg)
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
![Page 8: Hooking into WordPress](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813b21550346895da3daf8/html5/thumbnails/8.jpg)
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');
![Page 9: Hooking into WordPress](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813b21550346895da3daf8/html5/thumbnails/9.jpg)
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
![Page 10: Hooking into WordPress](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813b21550346895da3daf8/html5/thumbnails/10.jpg)
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
![Page 11: Hooking into WordPress](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813b21550346895da3daf8/html5/thumbnails/11.jpg)
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 );
![Page 12: Hooking into WordPress](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813b21550346895da3daf8/html5/thumbnails/12.jpg)
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
![Page 13: Hooking into WordPress](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813b21550346895da3daf8/html5/thumbnails/13.jpg)
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 );
![Page 14: Hooking into WordPress](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813b21550346895da3daf8/html5/thumbnails/14.jpg)
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
![Page 15: Hooking into WordPress](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813b21550346895da3daf8/html5/thumbnails/15.jpg)
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.';
}
}
![Page 16: Hooking into WordPress](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813b21550346895da3daf8/html5/thumbnails/16.jpg)
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;
}
![Page 17: Hooking into WordPress](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813b21550346895da3daf8/html5/thumbnails/17.jpg)
Helpful Resources
http://codex.wordpress.org/Plugin_API
http://adambrown.info/p/wp_hooks
Questions?