remove_action conditionally for Custom Post Type

By default, my theme is showing an author box below my posts. I am using a custom post type, and would like to remove this author box only on this post type. Here is how the theme is calling the author box:

add_action( 'wp_head', 'woo_author', 10 );

if ( ! function_exists( 'woo_author' ) ) {
function woo_author () {
    // Author box single post page
    if ( is_single() && get_option( 'woo_disable_post_author' ) != 'true' ) { add_action( 'woo_post_inside_after', 'woo_author_box', 10 ); }
    // Author box author page
    if ( is_author() ) { add_action( 'woo_loop_before', 'woo_author_box', 10 ); }
} // End woo_author()
}

Here is the function I have tried to remove the post author box from the wpseo_locations single posts, but it is not working:

Read More
add_action('init', 'kiwi_remove_post_author_box'); 

function kiwi_remove_post_author_box () {
    if (is_singular('wpseo_locations')  ) {
        remove_action( 'woo_post_inside_after', 'woo_author_box', 9 );
    }  
}

I have also tried:

add_action('init', 'kiwi_remove_post_author_box'); 

function kiwi_remove_post_author_box () {
    if ( 'wpseo_locations' == get_post_type() ) {
        remove_action( 'woo_post_inside_after', 'woo_author_box', 9 );
    }  
}

I cannot seem to get that box to go away. I know that there is a check box in the theme options panel that will make it go away globally, but I want it removed only for this post type. Any help would be greatly appreciated.

Related posts

2 comments

  1. Query variable only can be accessed after wp_query has been called. In you call, the get_post_type() function is actually returning an empty value. It should be working, if you change the hook to something which fires after wp_query is called.
    So, You should use –

    add_action('wp', 'kiwi_remove_post_author_box');
    

    OR

    add_action('template_redirect', 'kiwi_remove_post_author_box');
    

    Also, hooks can removed with the same priority value the have been added on.
    So final code it would be –

    add_action('template_redirect', 'kiwi_remove_post_author_box');
    function kiwi_remove_post_author_box()
    {
        if( is_singular('wpseo_locations') )
            remove_action( 'woo_post_inside_after', 'woo_author_box', 10 );
    }
    
  2. Thanks to @Shazzad, I was able to come up with the final code that worked.

    add_action('template_redirect', 'kiwi_remove_post_author_box'); 
    
    function kiwi_remove_post_author_box () {
        if ( 'wpseo_locations' == get_post_type() ) {
            remove_action( 'wp_head', 'woo_author', 10 );
        }  
    }
    

    Notice how the remove_action has changed inside the function. As soon as I did this it worked. Does this mean that you cannot use hooks if they are inside another function, and being called conditionally?

Comments are closed.