Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Disable WooCommerce default category
- Plugin URI: https://www.willhosts.com/
- Description: Disables the default category behaviour for WooCommerce v3.3.1 onwards
- Version: 1.0.0
- Author: WillHosts Ltd
- Author URI: https://www.willhosts.com/
- */
- /**
- * @author WillHosts Ltd
- * @since v1.0.0
- * @copyright Copyright (c) 2018 and Onwards, WillHosts Ltd. All rights reserved.
- * @license GPL
- * By viewing, using, or actively developing this application in any way, you are
- * henceforth bound the license agreement, and all of its changes, set forth by
- * WillHosts Ltd.
- */
- // Cache default product cat option for WP_Term_Query
- $default_product_cat = get_option('default_product_cat');
- // Filter WP_Term_Query to exclude default category
- add_action('pre_get_terms', function ($term_query) use ($default_product_cat) {
- if (in_array('product_cat', $term_query->query_vars['taxonomy'])) {
- $term_query->query_vars['exclude'][] = $default_product_cat;
- }
- });
- // Filter default product category option in case WooCommerce add a user option
- add_filter('pre_option_default_product_cat', function () {
- return -1;
- });
- // Hide 'uncategorised' instruction from CMS
- add_action('admin_head', function () {
- echo(
- <<<HTML
- <style type="text/css">
- .edit-tags-php.taxonomy-product_cat .edit-term-notes {
- display:none;
- }
- </style>
- HTML
- );
- });
- // Trigger 404 for product category on frontend
- add_action('get_header', function () use ($default_product_cat) {
- if (is_tag($default_product_cat)) {
- status_header(404);
- }
- });
Add Comment
Please, Sign In to add comment