Skip to content

Instantly share code, notes, and snippets.

@lgedeon
Created November 29, 2015 01:06
Show Gist options
  • Save lgedeon/d9b836443379b02f3a07 to your computer and use it in GitHub Desktop.
Save lgedeon/d9b836443379b02f3a07 to your computer and use it in GitHub Desktop.
Target specific widgets by title
<?php
function helper__dynamic_sidebar_params__title_class ( $instance, $widget, $args ) {
if ( isset( $instance['title'] ) ) {
$new_class = implode( '_', array( 'widget', $widget->id_base, sanitize_title( $instance['title'] ) ) ) . " ";
$args['before_widget'] = str_replace( 'class="', 'class="' . $new_class, $args['before_widget'] );
// For lack of a proper filter, let's just do this ourselves - will have to keep up to date with WP, though.
$was_cache_addition_suspended = wp_suspend_cache_addition();
if ( $widget->is_preview() && ! $was_cache_addition_suspended ) {
wp_suspend_cache_addition( true );
}
$widget->widget( $args, $instance );
if ( $widget->is_preview() ) {
wp_suspend_cache_addition( $was_cache_addition_suspended );
}
// Return false to prevent display since we just did it
return false;
}
return $instance;
}
add_filter( 'widget_display_callback', 'helper__dynamic_sidebar_params__title_class', 10 , 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment