Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to register a custom menu widget programmatically with wp_register_sidebar_widget, but it doesn't appear in wordpress. I'm using remote call of that method (xml-rpc). Referring to codex I'm passing parametres like that to that method:

wp_register_sidebar_widget(
    'custom_menu_widget-1',
    'My name',
    'WP_Nav_Menu_Widget',
    array( 'description' => 'blogroll' )
);

It should create custom menu widget named 'My name', but it registers nothing. I think that third parameter $output_callback may be wrong, but I can't find nowhere, what callback should be for custom menus. I've got no output or error from that call, so I can't see what is wrong. Maybe I miss something or there is another way to register custom menu widget? I don`t want to edit function.php, I just want to register standard custom menu widget using wp methods.

share|improve this question
1  
I am not sure this is even supposed to work. Could you elaborate on what exactly you are doing with XML-RPC here? –  Rarst Jun 25 '13 at 21:04
add comment

1 Answer

Try this code (in function.php)

class MyWidget extends WP_Widget {

    function __construct() {
        $widget_ops = array( 'description' => __('Use this widget to add one of your custom menu as a link list widget.') );
        parent::__construct( 'custom_menu_widget-1', __('My name'), $widget_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;

        $instance['title'] = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );



        echo $args['before_widget'];

        if ( !empty($instance['title']) )
            echo $args['before_title'] . $instance['title'] . $args['after_title'];

        wp_nav_menu( array( 'menu' => $nav_menu) );

        echo $args['after_widget'];

    }

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

    function form( $instance ) {
        $title = isset( $instance['title'] ) ? $instance['title'] : '';
        $nav_menu = isset( $instance['nav_menu'] ) ? $instance['nav_menu'] : '';

        // Get menus
        $menus = get_terms( 'nav_menu', array( 'hide_empty' => false ) );

        // 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('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'); ?>">
        <?php
            foreach ( $menus as $menu ) {
                $selected = $nav_menu == $menu->term_id ? ' selected="selected"' : '';
                echo '<option'. $selected .' value="'. $menu->term_id .'">'. $menu->name .'</option>';
            }
        ?>
            </select>
        </p>
        <?php
    }
}

add_action( 'widgets_init', 'myplugin_register_widgets' );

function myplugin_register_widgets() {

      register_widget( 'MyWidget' );

}
share|improve this answer
    
Thank You for your reply Amino, but I forgot mention that I can`t edit function.php. I`m working on many sites with remote calls of wp methods (xml-rpc). I don`t want to create a new class of widget, but just register standard custom menu widget, which I can add to sidebar. When You drag&drop custom menu widget to sidebar You don`t have to add code to function.php. Wp just call some method with apropriate parametres, but reading source code of widgets.php and widgets.js doesn`t give me an answer. –  bigwolk Mar 12 '13 at 8:23
    
oh! so, I don't know :D –  Amino Mar 12 '13 at 11:41
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.