1 | <?php |
---|
2 | /** |
---|
3 | * Menu Item Widget Class |
---|
4 | * |
---|
5 | * Creates a 'Menu Item' widget which can be dragged on to a sidebar that simulates a navigation menu. |
---|
6 | * Widget controls allow the page link to be selected from a drop down menu together with a custom title. |
---|
7 | * Outputs a menu item which is a link wrapped in a list item, the same HTML as wp_list_pages(). |
---|
8 | */ |
---|
9 | |
---|
10 | class Widget_Menu_Item extends WP_Widget { |
---|
11 | |
---|
12 | /** |
---|
13 | * Set up the widget's unique name and options |
---|
14 | */ |
---|
15 | function Widget_Menu_Item() { |
---|
16 | $widget_ops = array( 'classname' => 'widget_menuitem', 'description' => __( 'A navigation item for a menu') ); |
---|
17 | $this->WP_Widget( 'menuitem', __( 'Menu Item' ), $widget_ops ); |
---|
18 | } |
---|
19 | |
---|
20 | /** |
---|
21 | * Outputs the widget HTML |
---|
22 | */ |
---|
23 | function widget( $args, $instance ) { |
---|
24 | |
---|
25 | extract( $args ); |
---|
26 | |
---|
27 | $title = apply_filters( 'widget_title', $instance['title'] ); |
---|
28 | $page_id = apply_filters( 'widget_page_id', $instance['page_id'] ); |
---|
29 | |
---|
30 | echo $before_widget; |
---|
31 | |
---|
32 | // Store CSS classes |
---|
33 | $classes[] = 'page_item'; |
---|
34 | |
---|
35 | // Unique ID for each link |
---|
36 | if ( $page_id > 0 ) { |
---|
37 | $classes[] = 'page-item-' . $page_id; |
---|
38 | } else { |
---|
39 | $classes[] = 'page-item-front'; |
---|
40 | } |
---|
41 | |
---|
42 | // Identify the current page |
---|
43 | if ( $page_id == -1 ) { |
---|
44 | if ( is_front_page() && !is_paged() ) { |
---|
45 | $classes[] = 'current_page_item'; |
---|
46 | } |
---|
47 | } else { |
---|
48 | if ( get_option( 'show_on_front' ) == 'page' && get_option( 'page_for_posts' ) == $page_id ) { |
---|
49 | if ( is_home() ) { |
---|
50 | $classes[] = 'current_page_item'; |
---|
51 | } |
---|
52 | } else { |
---|
53 | if ( is_page( $page_id ) ) { |
---|
54 | $classes[] = 'current_page_item'; |
---|
55 | } |
---|
56 | } |
---|
57 | } |
---|
58 | |
---|
59 | $class = implode( ' ', $classes ); |
---|
60 | |
---|
61 | // URL for menu item |
---|
62 | if ( $page_id == -1 ) { |
---|
63 | $url = get_bloginfo( 'url' ); |
---|
64 | } else { |
---|
65 | $url = get_page_link( $page_id ); |
---|
66 | } |
---|
67 | |
---|
68 | // Output the menu item |
---|
69 | echo '<li class="' . $class . '"><a href="' . $url . '" title="' . $title . '">' . $link_before . $title . $link_after . '</a></li>'; |
---|
70 | |
---|
71 | echo $after_widget; |
---|
72 | |
---|
73 | } |
---|
74 | |
---|
75 | /** |
---|
76 | * Updates the widget control options |
---|
77 | */ |
---|
78 | function update( $new_instance, $old_instance ) { |
---|
79 | |
---|
80 | $instance = $old_instance; |
---|
81 | |
---|
82 | // Set the widget title as the page title by default |
---|
83 | if ( empty( $new_instance['title'] ) ) { |
---|
84 | if ( $new_instance['page_id'] > 0 ) { |
---|
85 | $instance['title'] = strip_tags( get_the_title( $new_instance['page_id'] ) ); |
---|
86 | } else { |
---|
87 | $instance['title'] = 'Front Page'; |
---|
88 | } |
---|
89 | } else { |
---|
90 | $instance['title'] = strip_tags( $new_instance['title'] ); |
---|
91 | } |
---|
92 | |
---|
93 | // Overwrite old options with new |
---|
94 | $instance['page_id'] = $new_instance['page_id']; |
---|
95 | |
---|
96 | return $instance; |
---|
97 | |
---|
98 | } |
---|
99 | |
---|
100 | /** |
---|
101 | * Displays the widget control options |
---|
102 | */ |
---|
103 | function form( $instance ) { |
---|
104 | $title = esc_attr( $instance['title'] ); |
---|
105 | $page_id = esc_attr( $instance['page_id'] ); |
---|
106 | ?> |
---|
107 | <style type="text/css">#pages-dropdown-menu select {width:100%;}</style> |
---|
108 | <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?> <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" /></label></p> |
---|
109 | <p id="pages-dropdown-menu"><?php wp_dropdown_pages( array( 'name' => $this->get_field_name( 'page_id' ), 'id' => $this->get_field_id( 'page_id' ), 'selected' => $page_id, 'show_option_none' => __( 'Front Page' ), 'option_none_value' => -1, 'sort_column' => 'post_title' ) ); ?></p> |
---|
110 | <?php |
---|
111 | } |
---|
112 | |
---|
113 | } |
---|
114 | |
---|
115 | register_widget( 'Widget_Menu_Item' ); |
---|
116 | ?> |
---|