Your SlideShare is downloading. ×
0
Actions & Filters in WordPress<br />
Filters are functions that take in some kind of input, modify it and return it<br />Source: http://ottopress.com/2011/acti...
Actions are places where a function is called and we don‘t care what it returns.<br />In a sense, a Filter w/o args and re...
In Core<br />/**<br />* Hooks a function on to a specific action.<br /> */<br />functionadd_action( $tag, $function_to_add...
In a Plugin<br />/**<br />* Does something with the content<br /> */<br />functionmy_custom_function( $content ) {<br />  ...
In a Class<br />classMy_Custom_Class {<br />    /**<br />* Construct<br />     */<br />public function __construct() {<br ...
Plugin API in WordPress<br />Filter Functions<br /><ul><li>has_filter()
add_filter()
Upcoming SlideShare
Loading in...5
×

Actions & Filters In WordPress

282

Published on

A very quick overview about Actions and Filters in WordPress

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
282
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Actions & Filters In WordPress"

  1. 1. Actions & Filters in WordPress<br />
  2. 2. Filters are functions that take in some kind of input, modify it and return it<br />Source: http://ottopress.com/2011/actions-and-filters-are-not-the-same-thing/<br />
  3. 3. Actions are places where a function is called and we don‘t care what it returns.<br />In a sense, a Filter w/o args and return value<br />Source: http://ottopress.com/2011/actions-and-filters-are-not-the-same-thing/<br />
  4. 4. In Core<br />/**<br />* Hooks a function on to a specific action.<br /> */<br />functionadd_action( $tag, $function_to_add, $priority = 10, $accepted_args= 1 ) {<br />returnadd_filter( $tag, $function_to_add, $priority, $accepted_args);<br />}<br />/**<br /> * Hooks a function or method to a specific filter action.<br /> */<br />functionadd_filter( $tag, $function_to_add, $priority = 10, $accepted_args= 1 ) {<br />global$wp_filter, $merged_filters;<br />$idx= _wp_filter_build_unique_id($tag, $function_to_add, $priority);<br />$wp_filter[$tag][$priority][$idx] = array(<br />'function‘ => $function_to_add,<br />'accepted_args' => $accepted_args<br /> );<br />unset( $merged_filters[ $tag ] );<br />returntrue;<br />}<br />
  5. 5. In a Plugin<br />/**<br />* Does something with the content<br /> */<br />functionmy_custom_function( $content ) {<br /> // Do something with the content<br />return$content;<br />}<br />add_filter( 'the_content', 'my_custom_function' );<br />
  6. 6. In a Class<br />classMy_Custom_Class {<br /> /**<br />* Construct<br /> */<br />public function __construct() {<br />add_filter( 'the_content', array(<br />&$this,<br /> 'my_custom_method'<br />));<br /> }<br /> /**<br /> * Does something with the content<br /> */<br /> public functionmy_custom_method( $content ) {<br /> // Do something with the content<br />return$content;<br /> }<br />}<br />
  7. 7. Plugin API in WordPress<br />Filter Functions<br /><ul><li>has_filter()
  8. 8. add_filter()
  9. 9. apply_filters()
  10. 10. current_filter()
  11. 11. merge_filters()
  12. 12. remove_filter()
  13. 13. remove_all_filters() </li></ul>Action Functions<br /><ul><li>has_action()
  14. 14. add_action()
  15. 15. do_action()
  16. 16. do_action_ref_array()
  17. 17. did_action()
  18. 18. remove_action()
  19. 19. remove_all_actions() </li></li></ul><li>In Core<br />/**<br /> * Call the functions added to a filter hook.<br />* Simplified<br />*/<br />functionapply_filters( $tag, $value) {<br />global$wp_filter, $merged_filters, $wp_current_filter;<br />// Dealing with 'all' filters and sorting<br />do {<br />foreach( (array) current($wp_filter[$tag]) as $the_ )<br /> if ( !is_null($the_['function']) ){<br />$args[1] = $value;<br />$value = call_user_func_array(<br />$the_['function'],<br />array_slice($args, 1, (int) $the_['accepted_args'])<br /> );<br /> }<br /> } while ( next($wp_filter[$tag]) !== false);<br />return$value;<br />}<br />
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×