Created
March 20, 2015 11:33
-
-
Save webmasterninjay/2ad07b8fc72dead23cd5 to your computer and use it in GitHub Desktop.
Wordpress: Adding custom classes on widget
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function jay_widget_form_extend( $instance, $widget ) { | |
if ( !isset($instance['classes']) ) | |
$instance['classes'] = null; | |
$row = "<p>"; | |
$row .= "<label for='widget-{$widget->id_base}-{$widget->number}-classes'>Additional Classes <small>(separate with spaces)</small></label>"; | |
$row .= "<input type='text' name='widget-{$widget->id_base}[{$widget->number}][classes]' id='widget-{$widget->id_base}-{$widget->number}-classes' class='widefat' value='{$instance['classes']}'/>"; | |
$row .= "</p>"; | |
echo $row; | |
return $instance; | |
} | |
add_filter('widget_form_callback', 'jay_widget_form_extend', 10, 12); | |
function jay_widget_update( $instance, $new_instance ) { | |
$instance['classes'] = $new_instance['classes']; | |
return $instance; | |
} | |
add_filter( 'widget_update_callback', 'jay_widget_update', 10, 2 ); | |
function jay_dynamic_sidebar_params( $params ) { | |
global $wp_registered_widgets; | |
$widget_id = $params[0]['widget_id']; | |
$widget_obj = $wp_registered_widgets[$widget_id]; | |
$widget_opt = get_option($widget_obj['callback'][0]->option_name); | |
$widget_num = $widget_obj['params'][0]['number']; | |
if ( isset($widget_opt[$widget_num]['classes']) && !empty($widget_opt[$widget_num]['classes']) ) | |
$params[0]['before_widget'] = preg_replace( '/class="/', "class=\"{$widget_opt[$widget_num]['classes']} ", $params[0]['before_widget'], 1 ); | |
return $params; | |
} | |
add_filter( 'dynamic_sidebar_params', 'jay_dynamic_sidebar_params' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment