Skip to content

Instantly share code, notes, and snippets.

@anilmeena
Created October 16, 2016 15:06
Show Gist options
  • Select an option

  • Save anilmeena/19c107e28ad27aec25bb46b3465a19df to your computer and use it in GitHub Desktop.

Select an option

Save anilmeena/19c107e28ad27aec25bb46b3465a19df to your computer and use it in GitHub Desktop.
Adding Custom Options to Existing Widgets
<?php
/* Displaying a Custom Widget Option */
function thmfdn_add_menu_description_option( $widget, $return, $instance ) {
// Are we dealing with a nav menu widget?
if ( 'nav_menu' == $widget->id_base ) {
// Display the description option.
$description = isset( $instance['description'] ) ? $instance['description'] : '';
?>
<p>
<input class="checkbox" type="checkbox" id="<?php echo $widget->get_field_id('description'); ?>" name="<?php echo $widget->get_field_name('description'); ?>" <?php checked( true , $description ); ?> />
<label for="<?php echo $widget->get_field_id('description'); ?>">
<?php _e( 'Show descriptions', 'thmfdn_textdomain' ); ?>
</label>
</p>
<?php
}
}
add_filter('in_widget_form', 'thmfdn_add_menu_description_option', 10, 3 );
/* Saving a Custom Widget Option */
function thmfdn_save_menu_description_option( $instance, $new_instance ) {
// Is the instance a nav menu and are descriptions enabled?
if ( isset( $new_instance['nav_menu'] ) && !empty( $new_instance['description'] ) ) {
$new_instance['description'] = 1;
}
return $new_instance;
}
add_filter( 'widget_update_callback', 'thmfdn_save_menu_description_option', 10, 2 );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment