• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Actions & Filters In WordPress
 

Actions & Filters In WordPress

on

  • 371 views

A very quick overview about Actions and Filters in WordPress

A very quick overview about Actions and Filters in WordPress

Statistics

Views

Total Views
371
Views on SlideShare
371
Embed Views
0

Actions

Likes
0
Downloads
1
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

CC Attribution-ShareAlike LicenseCC Attribution-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Actions & Filters In WordPress Actions & Filters In WordPress Presentation Transcript

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