I'm creating a function in functions.php with a custom hook, and from what I've read somewhere, it's good practice to return instead of use echo in a WordPress function? Correct me if I'm wrong.
Anyway, so with echo, my function works, but breaks everything else. With return, the function is no longer outputting anything, but nothing's broken. I tried to use this question as a reference to fix mine.
Here's what I have:
function display_collections_menu(){
/* Get WooCommerce's product categories which is custom taxonomy */
$prod_cat_args = array(
'taxonomy' => 'product_cat', //woocommerce
'orderby' => 'name',
'empty' => 0
);
$woo_categories = get_categories( $prod_cat_args );
$woo_menu = '';
$woo_menu.= '<ul class="menu-collections">';
/* For each first level category, get the image, name, and link */
foreach ( $woo_categories as $woo_cat ) {
if( $woo_cat->category_parent == 0 ) {
$woo_cat_id = $woo_cat->term_id; //category ID
$woo_img_id = get_woocommerce_term_meta( $woo_cat_id, 'thumbnail_id', true ); //category image ID
$woo_parent_image = wp_get_attachment_url( $woo_img_id ); //category image url
$woo_cat_slug = $woo_cat->slug; //category slug for classes
$woo_cat_name = $woo_cat->name; //category name for link
/* return the image/link/name */
$orw_woo_menu.= '<li class="menu-product menu-item menu-item-object-' . $woo_cat_slug . ' ">
<a href="' . get_term_link( $woo_cat_slug, 'product_cat' ) . '" class="menu-product-link">
<img src="' . $woo_parent_image . '" alt=" ' . $woo_cat_name . ' " class="menu-product-thumb" />
<p class="menu-product-name">' . $woo_cat_name . '</p></a></li>';
}
}//end of $woo_categories foreach
$woo_menu.= '</ul>';
return $woo_menu;
}
add_action('woo_collections_menu', 'display_collections_menu');
It'll work if instead of using return and a variable, I just echo out the values, but it breaks things. Am I using return wrong?
Updated with an attempt Privateer's solution - still doesn't work:
function display_collections_menu( $input = '' ){
/* Get WooCommerce's product categories which is custom taxonomy */
$prod_cat_args = array(
'taxonomy' => 'product_cat', //woocommerce
'orderby' => 'name',
'empty' => 0
);
$woo_categories = get_categories( $prod_cat_args );
$woo_menu = '';
$woo_menu.= '<ul class="menu-collections">';
/* For each first level category, get the image, name, and link */
foreach ( $woo_categories as $woo_cat ) {
if( $woo_cat->category_parent == 0 ) {
$woo_cat_id = $woo_cat->term_id; //category ID
$woo_img_id = get_woocommerce_term_meta( $woo_cat_id, 'thumbnail_id', true ); //category image ID
$woo_parent_image = wp_get_attachment_url( $woo_img_id ); //category image url
$woo_cat_slug = $woo_cat->slug; //category slug for classes
$woo_cat_name = $woo_cat->name; //category name for link
/* return the image/link/name */
$orw_woo_menu.= '<li class="menu-product menu-item menu-item-object-' . $woo_cat_slug . ' ">
<a href="' . get_term_link( $woo_cat_slug, 'product_cat' ) . '" class="menu-product-link">
<img src="' . $woo_parent_image . '" alt=" ' . $woo_cat_name . ' " class="menu-product-thumb" />
<p class="menu-product-name">' . $woo_cat_name . '</p></a></li>';
}
}//end of $woo_categories foreach
$woo_menu.= '</ul>';
return $woo_menu;
}
add_filter('collections_menu', 'display_collections_menu', 10, 1);
$html_block = apply_filters('collections_menu', $input_html);
And then this in a template:
<?php echo $html_block; ?>
Am I still doing this wrong?