Search code examples
phpwordpressgenesis

Why isn't this working in my Genesis!! remove_action with conditional tags


Hi I built a navigation widget which works and looks great, showing the logged in person from Buddypress, but obviously I want it to be hidden on the main navigation, the login pages etc..

I'm pulling my hair out here because by all purposes what I'm doing is right? - But it's simply not working, would really appreciate some assistance here! Thanks

/** Register Utility Bar Widget Areas. */
genesis_register_sidebar( array(
 'id' => 'utility-bar-left',
 'name' => __( 'TRN Member Utility Bar left', 'theme-prefix' ),
 'description' => __( 'This is the left utility bar above the header.', 'theme-prefix' ),
) );
genesis_register_sidebar( array(
 'id' => 'utility-bar-right',
 'name' => __( 'TRN Member Utility Bar Right', 'theme-prefix' ),
 'description' => __( 'This is the right utility bar above the header.', 'theme-prefix' ),
) );


add_action( 'genesis_before_header', 'utility_bar' );

function utility_bar() {

 echo '<div class="utility-bar">';

 genesis_widget_area( 'utility-bar-left', array(
 'before' => '<div class="utility-bar-left">',
 'after' => '</div>',
 ) );

 genesis_widget_area( 'utility-bar-right', array(
 'before' => '<div class="utility-bar-right">',
 'after' => '</div>',
 ) );

 echo '</div></div>';
}

add_action('genesis_before_header','remove_bar');

function remove_bar() {
if (is_home() || is_page(www.trnworld.com) || is_page(trn-login) || is_front_page() ) { //Replace post_type with your post type slug
remove_action( 'genesis_before_header', 'utility_bar', 5 );
remove_action( 'genesis_before_header', 'genesis_register_sidebar' );
}
}

Solution

  • From Wordpress documentation:

    Important: To remove a hook, the $function_to_remove and $priority arguments must match when the hook was added. This goes for both filters and actions. No warning will be given on removal failure.

    add_action( 'genesis_before_header', 'utility_bar' );

    does not match

    remove_action( 'genesis_before_header', 'utility_bar', 5 );

    Additionally You have added the action for displaying the widget and the action for removing the action that displays the widget to the same hook.

    From Wordpress documentation on add_action

    $priority (int) (Optional) Used to specify the order in which the functions associated with a particular action are executed. Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.

    Default value: 10

    You've added both actions with same default priority of 10 and since add_action( 'genesis_before_header', 'utility_bar' ); is added before add_action('genesis_before_header','remove_bar'); it is running first when the do_action function is called on genesis_before_header printing out the widget; after which the second function runs performing remove_action on an action which has already been performed.

    See what happens if you change

    add_action('genesis_before_header','remove_bar'); to add_action( 'genesis_before_header','remove_bar', 9 );

    and

    remove_action( 'genesis_before_header', 'utility_bar', 5 ); to remove_action( 'genesis_before_header', 'utility_bar' );

    Unrelated note in your function utility_bar() I see 2 closing divs but only one opening. I'm guessing this will cause layout problems in one of the states of being shown.