Wordpress小工具下拉菜单未保存所选内容


Wordpress widget dropdown menu not saving selection

我正在为Wordpress主题创建一个小部件,该小部件允许用户通过使用网站内所有页面的下拉菜单来选择按钮将链接到哪个页面。

我正在使用wp_dropdown_pages()创建下拉菜单。

目前,一切都在网站的前端正常工作,但当您保存小部件时,下拉菜单中的选择不会被保存。

add_action( 'widgets_init', 'app_button_widget' );

function app_button_widget() {
register_widget( 'app_button_widget' );
}
class app_button_widget extends WP_Widget {
function app_button_widget() {
    $widget_ops = array( 'classname' => 'button-widget', 'description' => __('A widget that displays a button and supporting text ', 'example') );
    $control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'example-widget' );
    $this->WP_Widget( 'example-widget', __('Button Widget', 'example'), $widget_ops, $control_ops );
}
function widget( $args, $instance ) {
    extract( $args );
    //Our variables from the widget settings.
    $title = apply_filters('widget_title', $instance['title'] );
    $url = $instance['url'];
    $text = $instance['text'];
    $new_window = isset( $instance['new_window'] ) ? $instance['new_window'] : false;
    echo $before_widget;
    if ( $new_window ) 
        $target = ('_blank');
    // Display the widget title 
    if ( $title )
        echo '<p><a href="' . get_page_link($url) . '" target="'.$target.'"><button class="btn btn-large btn-danger span12" type="button">' . $title . '</button></a></p>';
    //Display the name 
    if ( $name )
        printf( '<p>' . $text . '</p>' );
    echo $after_widget;
}
//Update the widget 
function update( $new_instance, $old_instance ) {
    $instance = $old_instance;
    //Strip tags from title and name to remove HTML 
    $instance['title'] = strip_tags( $new_instance['title'] );
    $instance['url'] = strip_tags( $new_instance['url'] );
    $instance['text'] = strip_tags( $new_instance['text'] );
    $instance['new_window'] = ($new_instance['new_window'] );
    return $instance;
}
function form( $instance ) {
    //Default widget settings.
    $defaults = array( 'title' => __('Sample Button Text', 'example'), 'url' => __('URL the button will link to', 'example'), 'text' => __('Call to action text', 'example'), 'show_info' => true );
    $instance = wp_parse_args( (array) $instance, $defaults ); ?>
    <p>
        <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'example'); ?></label>
        <input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:90%;" />
    </p>
    <p>
        <p>
        <label for="<?php echo $this->get_field_id('url'); ?>"><?php _e('Target URL:'); ?></label>
        <?php wp_dropdown_pages(array('id' => $this->get_field_id('url'),'name' => $this->get_field_name('url'))); ?>
    </p>
    </p>
    <p>
        <label for="<?php echo $this->get_field_id( 'text' ); ?>"><?php _e('Text:', 'example'); ?></label>
        <input id="<?php echo $this->get_field_id( 'text' ); ?>" name="<?php echo $this->get_field_name( 'text' ); ?>" value="<?php echo $instance['text']; ?>" style="width:100%;" />
    </p>
    <p>
        <input class="checkbox" type="checkbox" <?php checked( (bool) $instance['new_window'], true ); ?> id="<?php echo $this->get_field_id( 'new_window' ); ?>" name="<?php echo $this->get_field_name( 'new_window' ); ?>" /> 
        <label for="<?php echo $this->get_field_id( 'new_window' ); ?>"><?php _e('Open link in a new tab', 'example'); ?></label>
    </p>

<?php } ?> 

如何保存选定的下拉值?

我已经解决了下拉列表中的值没有保存的问题。

根据Wordpress Codex,有一个我没有在wp_dropdown_array()中设置的"选定"参数。

<?php wp_dropdown_pages(array('id' => $this->get_field_id('url'),'name' => $this->get_field_name('url'), 'selected' => $instance['url'])); ?>