Reputation: 308
I am creating a widget for a Wordpress Theme that allows the user to select which page a button will link to, by using a dropdown menu of all pages within the site.
I am using wp_dropdown_pages()
to create the dropdown menu.
Currently everything works on the front end of the site as expected, but when you save the widget the selection made in the dropdown menu is not being saved.
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 } ?>
How do you save the selected dropdown value?
Upvotes: 0
Views: 2148
Reputation: 308
I have solved the issue of the value in the dropdown not being saved.
Per the Wordpress Codex, there is a "selected" parameter that I was not setting in the wp_dropdown_array()
.
<?php wp_dropdown_pages(array('id' => $this->get_field_id('url'),'name' => $this->get_field_name('url'), 'selected' => $instance['url'])); ?>
Upvotes: 2