user5057930
user5057930

Reputation:

WordPress trigger action filter in if condition

I am trying to add a body class only when in function so_32165017_conditionally_remove_sidebar if condition triggers mean to say when the remove action removes the side bar on that time for me a add_filter( 'body_class', 'my_class_names' ); this add filter should be triggered but in my case if i add add_filter statement as independent it is working fine if i add the same in if condition it is not, where am i going wrong am not sure,.

function so_32165017_get_product_cat_depth($catid)
{
    $depth = 0;
    while ($catid) {
        $cat = get_term_by('id', $catid, 'product_cat'); // get the object for the catid
        if ($cat->parent > 0) {
            $depth++;
        }
        $catid = $cat->parent; // assign parent ID (if exists) to $catid
// the while loop will continue whilst there is a $catid
    }
    return $depth;
}


add_action('woocommerce_before_main_content', 'so_32165017_conditionally_remove_sidebar');
/**
 * Hide the sidebar for items 2 levels deep or more
 */
function so_32165017_conditionally_remove_sidebar()
{
    if (is_product_category()) {
        $t_id = get_queried_object()->term_id;
        if (so_32165017_get_product_cat_depth($t_id) < 2) {

            remove_action('storefront_sidebar', 'storefront_get_sidebar', 10);

// ****when added hear it is not working ****??//
            add_filter('body_class', 'my_class_names');
        }
    }
}

Add class Code:

// **** works fine hear ****//
add_filter( 'body_class', 'my_class_names' );

function my_class_names( $classes ) {
    // add 'class-name' to the $classes array
    $classes[] = 'My-Class';
    // return the $classes array
    return $classes;
}

Upvotes: 2

Views: 2385

Answers (2)

Noman
Noman

Reputation: 4116

What you need is to call your action separately. your action woocommerce_before_main_content run after the body class so adding the filter after action wont work.

add_action('woocommerce_before_main_content', 'so_32165017_conditionally_remove_sidebar');
function so_32165017_conditionally_remove_sidebar()
{
    if (is_product_category()) {
        $t_id = get_queried_object()->term_id;
        if (so_32165017_get_product_cat_depth($t_id) < 2) {
            remove_action('storefront_sidebar', 'storefront_get_sidebar', 10);
        }
    }
}

add_filter('body_class', 'my_class_names',10,2);
function my_class_names( $classes ) {
    if (is_product_category()) {
        $t_id = get_queried_object()->term_id;
        if (so_32165017_get_product_cat_depth($t_id) < 2) {
            $classes[] = 'My-Class';
        }
    }
    // return the $classes array
    return $classes;
}

Upvotes: 3

d79
d79

Reputation: 3848

It doesn't work because the action woocommerce_before_main_content is triggered after body_class() has already been called, so you should use a previous one.

Or you could use the body_class filter directly:

add_filter('body_class', 'so_32165017_conditionally_remove_sidebar');
/**
 * Hide the sidebar for items 2 levels deep or more
 */
function so_32165017_conditionally_remove_sidebar( $classes )
{
    if (is_product_category()) {
        $t_id = get_queried_object()->term_id;
        if (so_32165017_get_product_cat_depth($t_id) < 2) {

            remove_action('storefront_sidebar', 'storefront_get_sidebar', 10);
            $classes[] = 'my_class_names';

        }
    }
    return $classes;
}

Upvotes: 0

Related Questions