K
K
Kirill Michenus2015-10-30 02:09:35
WordPress
Kirill Michenus, 2015-10-30 02:09:35

How to create a widget in Wordpress. What's wrong?

There is a code:

class Penta_Royal_Nav_Menu_Widget extends WP_Widget {

        function __construct() {
            $widget_ops = array( 'description' => __('Add a custom menu to your sidebar.') );
            $control_ops = array('width' => 400, 'height' => 300);
            parent::__construct( 'nav_menu', __('Matt Royal Custom Menu'), $widget_ops, $control_ops );
        }

        function widget($args, $instance) {
            // Get menu
            $nav_menu = ! empty( $instance['nav_menu'] ) ? wp_get_nav_menu_object( $instance['nav_menu'] ) : false;

            if ( !$nav_menu )
                return;

            /** This filter is documented in wp-includes/default-widgets.php */
            $instance['title'] = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
            $class = empty( $instance['class_menu'] ) ? '' : $instance['class_menu'];
            if ( !empty($instance['title']) )
                echo $args['before_title'] . $instance['title'] . $args['after_title'];

            wp_nav_menu( array( 'fallback_cb' => '', 'menu' => $nav_menu, 'container_class' => '', 'menu_class' => 'submenu ' . $class ) );

          // echo $args['after_widget'];
        }

        public function update( $new_instance, $old_instance ) {
            $instance = $old_instance;
            $instance['title'] = strip_tags( stripslashes($new_instance['title']) );
            $instance['class_menu'] = strip_tags( $new_instance['class_menu'] );
            $instance['nav_menu'] = (int) $new_instance['nav_menu'];
            return $instance;
        }

        function form( $instance ) {

            $title = isset( $instance['title'] ) ? $instance['title'] : '';
            $class = isset( $instance['class_menu'] ) ? $instance['class_menu'] : '';
            $nav_menu = isset( $instance['nav_menu'] ) ? $instance['nav_menu'] : '';

            // Get menus
            $menus = wp_get_nav_menus( array( 'orderby' => 'name' ) );

            // If no menus exists, direct the user to go and create some.
            if ( !$menus ) {
                echo '<p>'. sprintf( __('No menus have been created yet. <a href="%s">Create some</a>.'), admin_url('nav-menus.php') ) .'</p>';
                return;
            }
            ?>
            <p>
                <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:') ?></label>
                <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; ?>" />
            </p>
            <p>
                <label for="<?php echo $this->get_field_id('class_menu'); ?>"><?php _e( 'Class menu:' ); ?></label>
                <input type="text" value="<?php echo $class; ?>" name="<?php echo $this->get_field_name('class_menu'); ?>" id="<?php echo $this->get_field_id('class_menu'); ?>" class="widefat" />
            </p>
            <p>
                <label for="<?php echo $this->get_field_id('nav_menu'); ?>"><?php _e('Select Menu:'); ?></label>
                <select id="<?php echo $this->get_field_id('nav_menu'); ?>" name="<?php echo $this->get_field_name('nav_menu'); ?>">
                    <option value="0"><?php _e( '&mdash; Select &mdash;' ) ?></option>
            <?php
                foreach ( $menus as $menu ) {
                    echo '<option value="' . $menu->term_id . '"'
                        . selected( $nav_menu, $menu->term_id, false )
                        . '>'. esc_html( $menu->name ) . '</option>';
                }
            ?>
                </select>
            </p>
            <?php
        }
    }

add_action('widgets_init', create_function('', 'register_widget("Penta_Royal_Nav_Menu_Widget");'));

What is the problem, why is the class_menu field not saved and updated?
The code is located in function.php.
wordpress version 4.3.1

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question