Question

I stuck at my code. I have some trouble to save the selected page or rather to set the selected option to selected=selected. In my opinion this code should work, but it doesnt, it doesn't update the option.

Have someone an idea or a solution?

Thanks. :)

<?php
add_action( 'widgets_init', create_function( '', 'register_widget( "ok_bubble" );' ) );

function ok_enqueue_bubble_widget_scripts() {
    if ( is_active_widget( false, false, 'ok_bubble', true ) ) {
        //wp_enqueue_script('validate');
    }
}

add_action('wp_enqueue_scripts', 'ok_enqueue_bubble_widget_scripts');


/**
 * Adds Foo_Widget widget.
 */
class ok_bubble extends WP_Widget {

    /**
     * Register widget with WordPress.
     */
    public function __construct() {
        $widget_ops = array('classname' => 'widget_bubble', 'description' => __('Write your information in a bubble.'));
        parent::__construct('ok_bubble', __('Bubble'), $widget_ops);
    }

    /**
     * Front-end display of widget.
     *
     * @see WP_Widget::widget()
     *
     * @param array $args     Widget arguments.
     * @param array $instance Saved values from database.
     */
    public function widget( $args, $instance ) {
        extract($args);

        $current_page = $this->_get_current_page($instance);
        $link = get_post($current_page);

        echo $before_widget;
        if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?>
        <div class="bubblewidget" data-rev="<?php $link->post_title; ?>"></div>
        <?php
        echo $after_widget;

      }
    /**
     * Sanitize widget form values as they are saved.
     *
     * @see WP_Widget::update()
     *
     * @param array $new_instance Values just sent to be saved.
     * @param array $old_instance Previously saved values from database.
     *
     * @return array Updated safe values to be saved.
     */
    public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance['page'] = stripslashes($new_instance['page']);
        return $instance;
    }


    /**
     * Back-end widget form.
     *
     * @see WP_Widget::form()
     *
     * @param array $instance Previously saved values from database.
     */
    public function form( $instance ) {
        $instance = wp_parse_args( (array) $instance, array( ) );
        $current_page = $this->_get_current_page($instance);
?>
    <p><label for="<?php echo $this->get_field_id('page'); ?>"><?php _e('Leistung verlinken:') ?></label>
    <select class="widefat" id="<?php echo $this->get_field_id('page'); ?>" name="<?php echo $this->get_field_name('page'); ?>">
        <option value=""><?php echo esc_attr( __( 'Seite auswählen' ) ); ?></option>
    <?php
        $postlist = get_posts( 'posts_per_page=-1&post_type=skills' );
        $posts = array();
        foreach ( $postlist as $post ) :
        $option = '<option value="' . get_page_link( $post->ID ) . '"' . selected($page, $current_page) . '>';
        $option .= $post->post_title;
        $option .= '</option>';
        echo $option;
        endforeach; ?>
    </select></p>
    <?php 
    }

    function _get_current_page($instance) {
        return $instance['page'];
    }
} // class Foo_Widget
?>
Was it helpful?

Solution

Just change two lines:

add the $page variable and then update the value with it. So your foreach postlist should be:

<?php
        $postlist = get_posts( 'posts_per_page=-1&post_type=skills' );
        $posts = array();
        foreach ( $postlist as $post ) :
        $page = get_page_link( $post->ID );
        $option = '<option value="' . $page . '"' . selected($page, $current_page, false) . '>';
        $option .= $post->post_title;
        $option .= '</option>';
        echo $option;
        endforeach; 
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top