Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • has_filter and has_action

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 180
    Comment on it

     Welcome to FindNerd.

    Today I am going to discuss two word-press functions has_filter and has_action. You can use any of the function between two because indirectly has_action is calls has_filter. Both of them use global variable $wp_filter that contains all the actions or filters. You can check the source code of has_action function below.

    function has_action($tag, $function_to_check = false) {
    	        return has_filter($tag, $function_to_check);
    }

     

    We are going to explain has_filter function only and it will describe the other one as well. Has_filter takes two arguments. Please check the properties below.

     

    Argument Properties
    tag Name of hook
    function_to_check(callback/boolean) Check attached callback function with hook. Default value is set to false

     

    function has_filter($tag, $function_to_check = false) {
        // Don't reset the internal array pointer
        $wp_filter = $GLOBALS['wp_filter'];
     
        $has = ! empty( $wp_filter[ $tag ] );
     
        // Make sure at least one priority has a filter callback
        if ( $has ) {
            $exists = false;
            foreach ( $wp_filter[ $tag ] as $callbacks ) {
                if ( ! empty( $callbacks ) ) {
                    $exists = true;
                    break;
                }
            }
     
            if ( ! $exists ) {
                $has = false;
            }
        }
     
        if ( false === $function_to_check || false === $has )
            return $has;
     
        if ( !$idx = _wp_filter_build_unique_id($tag, $function_to_check, false) )
            return false;
     
        foreach ( (array) array_keys($wp_filter[$tag]) as $priority ) {
            if ( isset($wp_filter[$tag][$priority][$idx]) )
                return $priority;
        }
     
        return false;
    }

     

    If you check the source code for the has_filter then you can know the exact working. It is using global array $wp_filter to manage the attached filters/actions. This function checks your mentioned callback function with mentioned event. When a function is checking particular function then it will return the priority of that function and returned false if callback function not attached. If we do not use the second argument then it will return a boolean for an indication that event is attached or not with someone. It can return false or 0 for the non-exist case so use the operator === for checking purpose.

 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: