Skip to content

Instantly share code, notes, and snippets.

@tradesouthwest
Last active July 2, 2020 23:24
Show Gist options
  • Save tradesouthwest/ec12e12393688ce6934fa4be4be9a64c to your computer and use it in GitHub Desktop.
Save tradesouthwest/ec12e12393688ce6934fa4be4be9a64c to your computer and use it in GitHub Desktop.
Remove styles via wp_enqueue and wp_dequeue. Bonus at bottom of file to select children of parent category
<?php
/** PB-1
* Remove woo specific styles that are added by this plugin if option checked
* Then re-enqueue when option is not isset (default)
*/
add_action( 'wp_enqueue_scripts', 'unitizr_products_enqueue_woospecific' );
function unitizr_products_enqueue_woospecific()
{
$options = get_option('unitizr_options');
$value = ( !isset($options['unitizr_removestyles_field'] ) )
? '' : $options['unitizr_removestyles_field'];
if ( !$value )
{
wp_register_style( 'unitizr-woospecific',
plugins_url() . '/unitizr-plus/lib/unitizr-woospecific.css',
array(), UNITIZR_VER, false );
wp_enqueue_style( 'unitizr-woospecific' );
} else {
wp_dequeue_style( 'unitizr-woospecific' );
wp_deregister_style( 'unitizr-woospecific' );
}
}
/**
* Set inline style to enqued if parent has two children
* @param $terms get_queried_object Taking long-route method to avoid getting ids.
* @param string $ispage_term Is true
*/
function storefront_child_detect_archive_pages()
{
global $post;
$term = $styl = '';
$ispage_term = false;
// looking for term to display tables (archive tax-product_cat term-spa-packages term-7 )
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$parent = get_term($term->parent, get_query_var('taxonomy') ); // get parent term
$children = get_term_children($term->term_id, get_query_var('taxonomy')); // get children
if(($parent->term_id!="" && sizeof($children)>0)) {
// has parent and child
$ispage_term = false;
}elseif(($parent->term_id!="") && (sizeof($children)==0)) {
// has parent, no child
$ispage_term = false;
}elseif(($parent->term_id=="") && (sizeof($children)>0)) {
// no parent, has child
$ispage_term = true;
}
// be sure inline styles are not queued
wp_dequeue_style( 'strfrnt-entry-set' );
if( $ispage_term ) {
wp_enqueue_style( 'strfrnt-entry-set' );
$styl .= '.archive.tax-product_cat.term-' . $term->term_id . ' .wpt_product_table_wrapper{display:none;} .archive.tax-product_cat.term-' . $term->term_id . ' ul.products.columns-3{display:block;}';
} else {
wp_enqueue_style( 'strfrnt-entry-set' );
$styl .= '.archive.tax-product_cat.term-' . $term->term_id . ' .wpt_product_table_wrapper{display:block;} .archive.tax-product_cat.term-' . $term->term_id . ' ul.products.columns-3{display:none;}';
}
wp_add_inline_style( 'strfrnt-entry-set', $styl );
unset($term); //clean house
}
add_action( 'wp_enqueue_scripts', 'storefront_child_detect_archive_pages', 15 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment