add_filter to ‘woocommerce_before_main_content’ [closed]

I added a filter in functions.php and change priority very high and low to 1 but every time the parameter is an empty string””;

add_filter( 'woocommerce_before_main_content','wc_start_layout');
function wc_start_layout($content){
    return $content;

What I’m doing wrong?

1 Answer

woocommerce_before_main_content is not a filter hook, it’s an action hook, so don’t expect it to return something, because nothing cares about what an action hook callback function returns, whereas for filters hooks, you will always see something waiting for the returned result:

// Action hook 

// Filter hook 
$bar = apply_filters('foo_filter');


That action hook is used by WooCommerce itself to insert the breadcrumbs for example, here is a place (among others) where WooCommerce exposes this action hook, see this code (templates/archive-product.php:16-24):

* woocommerce_before_main_content hook
* @hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
* @hooked woocommerce_breadcrumb - 20
do_action( 'woocommerce_before_main_content' );

And here is a place where includes/wc-template-hooks.php:46-51 WooCommerce attaches the breadcrumbs function to that action hook:

* Breadcrumbs
* @see woocommerce_breadcrumb()
add_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20, 0 );

Now if you investigate the function woocommerce_breadcrumb(), you will see that it doesn’t modify something it received, in fact, it’s purpose is to output the breadcrumbs.

Moral of the story, an action hook will not allow you to alter a variable, you will need to find a filter hook for that.

Leave a Comment