How to Temporarily Hide Categories in WooCommerce (Using the Functions File)
So, you need to temporarily hide some product categories in your WooCommerce store, but don’t want to delete them? Maybe you’re running a seasonal promotion and want to declutter the shop page, or perhaps you’re working on a new product line and aren’t ready to reveal the related categories yet. Whatever the reason, this guide will show you how to achieve this using your `functions.php` file. No plugins needed!
Why use the `functions.php` file? Because it’s a powerful way to customize your WordPress/WooCommerce site without directly modifying the core files. It’s a safe and flexible approach, especially for simple modifications like hiding categories.
Important: Before making any changes to your `functions.php` file, it’s strongly recommended to back up your website. If something goes wrong, you’ll have a safety net to restore your site to its previous state. Consider using a child theme for your customizations. A child theme prevents your changes from being overwritten when you update your main theme.
Understanding the Basics
WooCommerce uses categories to organize your products. These categories are often displayed in the shop page, product category archive pages, and in menus. To temporarily hide a category, we’ll use a small snippet of PHP code within the `functions.php` file that tells WooCommerce *not* to display those specific categories.
Step-by-Step Guide: Hiding Categories
1. Access Your `functions.php` File:
There are a few ways to access your `functions.php` file:
- Via WordPress Theme Editor: Go to *Appearance > Theme File Editor*. Select your child theme (if you’re using one – highly recommended!) in the dropdown menu. Then, find the `functions.php` file in the list on the right side.
- Via FTP/SFTP: Connect to your website’s server using an FTP client (like FileZilla). Navigate to your theme directory (usually `/wp-content/themes/your-theme-name/` or `/wp-content/themes/your-child-theme-name/`) and find the `functions.php` file.
2. Add the Code Snippet:
Add the following code snippet to the bottom of your `functions.php` file:
function hide_woocommerce_categories( $terms, $taxonomies, $args ) { $hidden_categories = array( Read more about How To Add On Custom Options Woocommerce 'clothing', 'accessories', 'sale' ); // Add category slugs to hide here
if ( ! is_admin() && in_array( ‘product_cat’, $taxonomies ) ) {
foreach ( $terms as $key => $term ) {
if ( in_array( $term->slug, $hidden_categories ) ) {
unset( $terms[ $key ] );
}
}
}
return $terms;
}
add_filter( ‘get_terms’, ‘hide_woocommerce_categories’, 10, 3 );
3. Customize the Code:
- `$hidden_categories = array( ‘clothing’, ‘accessories’, ‘sale’ );`: This is the crucial part. Replace `’clothing’`, `’accessories’`, and `’sale’` with the slugs of the categories you want to hide. The *slug* is the URL-friendly version of your category name (e.g., “Men’s Clothing” might have the slug “mens-clothing”).
- How to Find Category Slugs: Go to *Products > Categories* in your WordPress dashboard. Hover over the category you want to hide and look at the URL in your browser’s status bar. The slug is the part after `tag_ID=`. Alternatively, edit the category and find the “Slug” field.
- Real-life example: Let’s say you are running a Christmas store and sell ‘Christmas Decorations’, ‘Christmas Trees’ and ‘Christmas Lights’. After Christmas is over, you might want to hide those categories until the following year. In that case, you’d change `$hidden_categories` to: `$hidden_categories = array( ‘christmas-decorations’, ‘christmas-trees’, ‘christmas-lights’ );`
4. Save the Changes:
Click the “Update File” button in the Theme File Editor, or save the file to your server via FTP.
5. Test Your Changes:
Visit your shop page, product category archive pages, and menu (if you’re displaying categories in the menu). The categories you specified should now be hidden.
Reasoning Behind the Code
Let’s break down the code snippet:
- `function hide_woocommerce_categories( $terms, $taxonomies, $args )`: This defines a new function named `hide_woocommerce_categories`. It takes three arguments: `$terms` (the categories), `$taxonomies` (the type of taxonomy, in this case, `product_cat`), and `$args` (other arguments).
- `$hidden_categories = array( ‘clothing’, ‘accessories’, ‘sale’ );`: This array holds the slugs of the categories we want to hide. This is the most important part to customize!
- `if ( ! is_admin() && in_array( ‘product_cat’, $taxonomies ) )`: This condition ensures that the code only runs on the front end of your website (not in the admin dashboard) and only when dealing with product categories (`product_cat`). This is important to avoid breaking other parts of your site.
- `foreach ( $terms as $key => $term )`: This loops through each of Discover insights on How To Make Instagram Shoppable Woocommerce the existing categories.
- `if ( in_array( $term->slug, $hidden_categories ) )`: This checks if the slug of the current category (`$term->slug`) is in the `$hidden_categories` array.
- `unset( $terms[ $key ] );`: If the category’s slug is in the `$hidden_categories` array, this line removes it from the list of categories that will be displayed.
- `return $terms;`: This returns the modified list of categories (with the hidden ones removed).
- `add_filter( ‘get_terms’, ‘hide_woocommerce_categories’, 10, 3 );`: This line “hooks” our function into the `get_terms` filter. The `get_terms` filter Discover insights on How To Capture Customer Emails With Woocommerce is used by WordPress to retrieve a list of taxonomy terms (like categories). By hooking into Learn more about How To Customize Woocommerce Checkout this filter, our function gets a chance to modify the list of categories *before* they are displayed. The `10` is the priority (lower numbers run earlier), and `3` indicates the number of arguments our function accepts.
Temporarily Re-enabling Categories
To re-enable the categories, simply:
1. Go back to your `functions.php` Explore this article on How To Disable Checkout And Cart In Woocommerce file.
2. Remove the slugs from the `$hidden_categories` array, or comment out (disable) the entire code snippet by adding `//` at the beginning of each line.
// function hide_woocommerce_categories( $terms, $taxonomies, $args ) { // $hidden_categories = array( 'clothing', 'accessories', 'sale' ); // Add category slugs to hide here // // if ( ! is_admin() && in_array( 'product_cat', $taxonomies ) ) { // foreach ( $terms as $key => $term ) { // if ( in_array( $term->slug, $hidden_categories ) ) { // unset( $terms[ $key ] ); // } // } // } // // return $terms; // } // add_filter( 'get_terms', 'hide_woocommerce_categories', 10, 3 );
3. Save the changes.
Important Considerations
- Caching: If you are using a caching plugin, clear your cache after making changes to the `functions.php` file. Caching can sometimes prevent the changes from being immediately visible.
- Visibility Settings: Make sure the categories themselves are not set to “Hidden” in the WordPress backend. This method focuses on hiding them from display on the frontend. If the category is hidden on the backend, you’ll need to change that setting too.
- Child Theme: Using a child theme is crucial. If you directly modify your parent theme’s `functions.php` file, your changes will be lost when you update the theme.
- Error Handling: If you encounter errors after adding the code, carefully review the code for typos or syntax errors. Use a PHP linter or validator to help identify potential issues. Double check that the category slugs are correct.
- Search Engine Optimization (SEO): Even though the categories are hidden, Google might still index them if they are linked to from other parts of your site (internal links). If you want to *completely* remove them from Google’s index, you’ll need to add a `noindex` meta tag to the category archive pages, or use a more complex approach involving redirects. The method described in this article is best for temporarily hiding them from *display* on your site.
By following these steps, you can effectively and temporarily hide WooCommerce categories without the need for plugins, providing greater control over your store’s presentation. Remember to always back up your site before making changes!