1 / 17

Hooking into WordPress

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?. Actions. Filters. MODIFY existing data apply_filters () add_filter () has_filter () remove_filter ().

tannerk
Download Presentation

Hooking into WordPress

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Hooking into WordPress Craig Ralston craig@websolutions.com

  2. Why use hooks? • Extend core functionality • Allow others to alter your work craig@websolutions.com

  3. What are hooks? Actions Filters MODIFY existing data apply_filters() add_filter() has_filter() remove_filter() DO something new • do_action() • add_action() • has_action() • remove_action() craig@websolutions.com

  4. Actions – How are they used? Add an alert to the admin area using a core hook function cor_admin_notice(){?> <divclass="cor_alert"> <h3>Hello World!</h3> </div> <?php} add_action('admin_notices','cor_admin_notice'); Core hook Our function craig@websolutions.com

  5. add_action( 'admin_notices', 'cor_admin_notice' ); 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 craig@websolutions.com

  6. Creating Actions function cor_admin_notice(){?> <divclass="cor_alert"> <h3>Hello World!</h3> </div> <?php} craig@websolutions.com

  7. Action 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>'; } Filter Action craig@websolutions.com

  8. 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

  9. Action 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>'; } Filter Action craig@websolutions.com

  10. 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

  11. 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

  12. functioncor_admin_notice(){ echo'<div class="cor_alert">'; do_action('before_alert_text'); echoapply_filters('filter_alert_text','<h3>Hello World</h3>'); do_action('after_alert_text'); echo'</div>'; } Action Filter Action craig@websolutions.com

  13. 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

  14. 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

  15. 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); functionsarah_call_craig($sarah_has_phone){ // if $sarah_has_phoneis true if($sarah_has_phone){ echo'Sarah, would you please call Craig? Thanks.'; } } craig@websolutions.com

  16. When Sarah makes her grocery list, add beer. // Modify Sarah's grocery list add_filter('sarahs_grocery_list','cor_add_beer', 10, 1 ); functioncor_add_beer($grocery_list){ // Append another item to the list $grocery_list=$grocery_list.' Beer.'; return$grocery_list; } craig@websolutions.com

  17. Helpful Resources Questions? http://codex.wordpress.org/Plugin_API http://adambrown.info/p/wp_hooks craig@websolutions.com

More Related