Skip to content

Instantly share code, notes, and snippets.

@morvy
Last active September 11, 2024 19:35
Show Gist options
  • Save morvy/74fb5c6e3142a71a9e912ba97fc80960 to your computer and use it in GitHub Desktop.
Save morvy/74fb5c6e3142a71a9e912ba97fc80960 to your computer and use it in GitHub Desktop.
Toggle Blocksy sidebar for specified WooCommerce categories
<?php
/**
* Plugin Name: Blocksy Sidebar toggle for WooCommerce categories
* Description: This plugin disables sidebar for selected WooCommerce categories.
* Version: 1.0.0
* Author: Peter Morvay <[email protected]>
* License: A-GPL-3.0
* License URI: https://www.gnu.org/licenses/agpl-3.0.html
*/
namespace MoPed;
defined( 'ABSPATH' ) || exit;
class Blocksy_Sidebar_Toggle {
public function __construct() {
add_action( 'product_cat_add_form_fields', [ $this, 'add_disabled_checkbox' ] );
add_action( 'product_cat_edit_form_fields', [ $this, 'edit_disabled_checkbox' ] );
add_action( 'edited_product_cat', [ $this, 'save_disabled_checkbox' ] );
add_action( 'create_product_cat', [ $this, 'save_disabled_checkbox' ] );
add_filter( 'blocksy:general:sidebar-position', [$this, 'filter_sidebar_position'], 10, 1 );
}
public function add_disabled_checkbox() {
?>
<div class="form-field">
<label for="blocksy_sidebar_disabled"><?php esc_html_e( 'Disable sidebar', 'bst' ); ?></label>
<input type="checkbox" name="term_meta[blocksy_sidebar_disabled]" id="blocksy_sidebar_disabled" value="1">
<p class="description"><?php esc_html_e( 'Check this box to disable the sidebar for this category.', 'bst' ); ?></p>
</div>
<?php
}
public function edit_disabled_checkbox( $term ) {
$disabled = get_term_meta( $term->term_id, 'blocksy_sidebar_disabled', true );
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="blocksy_sidebar_disabled"><?php esc_html_e( 'Disable sidebar', 'bst' ); ?></label></th>
<td>
<input type="checkbox" name="term_meta[blocksy_sidebar_disabled]" id="blocksy_sidebar_disabled" value="1" <?php checked( $disabled, '1' ); ?>>
<p class="description"><?php esc_html_e( 'Check this box to disable the sidebar for this category.', 'bst' ); ?></p>
</td>
</tr>
<?php
}
public function save_disabled_checkbox( $term_id ) {
if ( isset( $_POST['term_meta']['blocksy_sidebar_disabled'] ) ) {
update_term_meta( $term_id, 'blocksy_sidebar_disabled', '1' );
} else {
delete_term_meta( $term_id, 'blocksy_sidebar_disabled' );
}
}
public function filter_sidebar_position($position) {
if (function_exists('is_product_category') && !is_product_category()) {
return $position;
}
$term_id = get_queried_object_id();
$disabled = get_term_meta($term_id, 'blocksy_sidebar_disabled', true);
if ($disabled === '1') {
return 'none';
}
return $position;
}
}
new Blocksy_Sidebar_Toggle();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment