Advertisement
firoze

wordpress widgets custom menu ul li a customization

Dec 14th, 2015
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. // source link: http://wordpress.stackexchange.com/questions/125122/how-to-avoid-the-div-and-ul-which-added-when-custon-menu-widget-is-used
  2.  
  3. // wordpress widgets custom menu ul li a update
  4.  
  5. /**
  6. * WTI Custom Navigation Menu widget class
  7. *
  8. * @since 3.0.0
  9. */
  10.  
  11. class Wti_Custom_Nav_Menu_Widget extends WP_Widget {
  12.  
  13. function __construct() {
  14. $widget_ops = array( 'description' => __('Use this widget to add one of your custom menus as a widget.') );
  15. parent::__construct( 'custom_nav_menu', __('WTI Custom Menu'), $widget_ops );
  16. }
  17.  
  18. function widget($args, $instance) {
  19. // Get menu
  20. $nav_menu = ! empty( $instance['nav_menu'] ) ? wp_get_nav_menu_object( $instance['nav_menu'] ) : false;
  21.  
  22. if ( !$nav_menu )
  23. return;
  24.  
  25. $instance['title'] = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
  26.  
  27. echo $args['before_widget'];
  28.  
  29. if ( !empty($instance['title']) )
  30. echo $args['before_title'] . $instance['title'] . $args['after_title'];
  31.  
  32. wp_nav_menu(
  33. array(
  34. 'fallback_cb' => '',
  35. 'container' => '',
  36. 'menu_class' => $instance['menu_class'],
  37. 'menu' => $nav_menu
  38. )
  39. );
  40.  
  41. echo $args['after_widget'];
  42. }
  43.  
  44. function update( $new_instance, $old_instance ) {
  45. $instance['title'] = strip_tags ( stripslashes ( $new_instance['title'] ) );
  46. $instance['menu_class'] = strip_tags ( stripslashes ( trim ( $new_instance['menu_class'] ) ) );
  47. $instance['nav_menu'] = (int) $new_instance['nav_menu'];
  48.  
  49. return $instance;
  50. }
  51.  
  52. function form( $instance ) {
  53. $title = isset( $instance['title'] ) ? $instance['title'] : '';
  54. $menu_class = isset( $instance['menu_class'] ) ? $instance['menu_class'] : '';
  55. $nav_menu = isset( $instance['nav_menu'] ) ? $instance['nav_menu'] : '';
  56.  
  57. // Get menus
  58. $menus = get_terms( 'nav_menu', array( 'hide_empty' => false ) );
  59.  
  60. // If no menus exists, direct the user to go and create some.
  61. if ( !$menus ) {
  62. echo '<p>'. sprintf( __('No menus have been created yet. <a href="%s">Create some</a>.'), admin_url('nav-menus.php') ) .'</p>';
  63. return;
  64. }
  65. ?>
  66. <p>
  67. <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:') ?></label>
  68. <input type="text" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $title; ?>" />
  69. </p>
  70. <p>
  71. <label for="<?php echo $this->get_field_id('menu_class'); ?>"><?php _e('Menu Class:') ?></label>
  72. <input type="text" class="widefat" id="<?php echo $this->get_field_id('menu_class'); ?>" name="<?php echo $this->get_field_name('menu_class'); ?>" value="<?php echo $menu_class; ?>" />
  73. </p>
  74. <p>
  75. <label for="<?php echo $this->get_field_id('nav_menu'); ?>"><?php _e('Select Menu:'); ?></label>
  76. <select id="<?php echo $this->get_field_id('nav_menu'); ?>" name="<?php echo $this->get_field_name('nav_menu'); ?>">
  77. <?php
  78. foreach ( $menus as $menu ) {
  79. echo '<option value="' . $menu->term_id . '"'
  80. . selected( $nav_menu, $menu->term_id, false )
  81. . '>'. $menu->name . '</option>';
  82. }
  83. ?>
  84. </select>
  85. </p>
  86. <?php
  87. }
  88. }
  89.  
  90. function wti_custom_nav_menu_widget() {
  91. register_widget('Wti_Custom_Nav_Menu_Widget');
  92. }
  93.  
  94. add_action ( 'widgets_init', 'wti_custom_nav_menu_widget', 1 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement