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:

    'My name',
    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.

  • 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, 2013 at 21:04

1 Answer 1


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 )

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

add_action( 'widgets_init', 'myplugin_register_widgets' );

function myplugin_register_widgets() {

      register_widget( 'MyWidget' );

  • Thank You for your reply Amino, but I forgot mention that I cant edit function.php. Im working on many sites with remote calls of wp methods (xml-rpc). I dont 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 dont 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, 2013 at 8:23
  • oh! so, I don't know :D
    – Amino
    Mar 12, 2013 at 11:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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