Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Confusion with action and filter hooks

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 322
    Comment on it

    Welcome to FindNerd.

    Today I am going to discuss the confusion which has been created by word-press developers. If you check the source code of add_action then you will realize what I am asking. Please check the code below.

    function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
          return add_filter($tag, $function_to_add, $priority, $accepted_args);
    	} 

    Have you checked the above code? You can see the add_filter function inside add_action function. Now the question raises, why word-press uses the one different function inside the other. Most of the developers till the date are confused with actions and filters. They knows only that these two are hooks. If you check the source of add_filter then you will find a new core function.

    Please have a look.

    function add_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 ] );
        return true;
    }

    If you see the above function and try to understand the functionality then we will find two global variables wp_filter and merged_filters. There is new core function named wp_filter_build_unique_id. I shall describe that function later but one thing to understand here that add_action function is calling add_filter function itself so question is why add_action is created by word-press developers. It is still a question for me. If you know the answer then definitely post your comments below.

    Now I will explain the function named  wp_filter_build_unique_id. It  takes three arguments such as hook name, callback function and priority.
    This function basically is making array for coming filter in a sequence. You can check the source of this function below.

    function _wp_filter_build_unique_id($tag, $function, $priority) {
            global $wp_filter;
          static $filter_id_count = 0;
    
            if ( is_string($function) )
                  return $function;
    
            if ( is_object($function) ) {
                    // Closures are currently implemented as objects
    	                $function = array( $function, '' );
    	        } else {
    	                $function = (array) $function;
            }
    
    	        if (is_object($function[0]) ) {
                    // Object Class Calling
                    if ( function_exists('spl_object_hash') ) {
                           return spl_object_hash($function[0]) . $function[1];
                    } else {
                           $obj_idx = get_class($function[0]).$function[1];
                            if ( !isset($function[0]->wp_filter_id) ) {
    	                                if ( false === $priority )
                                            return false;
                                    $obj_idx .= isset($wp_filter[$tag][$priority]) ? count((array)$wp_filter[$tag][$priority]) : $filter_id_count;
    	                                $function[0]->wp_filter_id = $filter_id_count;
                                    ++$filter_id_count;
    	                        } else {
                                    $obj_idx .= $function[0]->wp_filter_id;
                            }
    	
                            return $obj_idx;
    	                }
           } elseif ( is_string( $function[0] ) ) {
                    // Static Calling
    	                return $function[0] . '::' . $function[1];
            }
    	}

    I shall not recommend you to make the changes in this function because this is core function of word-press. Any change can break the project and it will be overwrite as word-press updates.

    Thank you for joining us.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: