Php Wordpress小部件下拉菜单未保存选择
我正在为Wordpress主题创建一个小部件,允许用户使用站点内所有页面的下拉菜单选择按钮链接到的页面 我正在使用Php Wordpress小部件下拉菜单未保存选择,php,drop-down-menu,wordpress,Php,Drop Down Menu,Wordpress,我正在为Wordpress主题创建一个小部件,允许用户使用站点内所有页面的下拉菜单选择按钮链接到的页面 我正在使用wp\u dropdown\u pages()创建下拉菜单 目前,网站前端的一切工作正常,但当您保存小部件时,下拉菜单中的选择不会被保存 add_action( 'widgets_init', 'app_button_widget' ); function app_button_widget() { register_widget( 'app_button_widget' );
wp\u dropdown\u 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 } ?>
add_action('widgets_init','app_button_widget');
功能应用程序\按钮\小部件(){
注册小部件(“应用程序按钮小部件”);
}
类应用程序按钮小部件扩展了WP小部件{
功能应用程序\按钮\小部件(){
$widget_ops=array('classname'=>'button widget','description'=>uuu('显示按钮和支持文本的小部件','example');
$control_ops=array('width'=>300,'height'=>350,'id_base'=>'example widget');
$this->WP_小部件('example Widget','Button Widget','example'),$Widget_ops,$control_ops);
}
函数小部件($args$instance){
摘录($args);
//我们的变量来自小部件设置。
$title=apply_filters('widget_title',$instance['title']);
$url=$instance['url'];
$text=$instance['text'];
$new\u window=isset($instance['new\u window'])?$instance['new\u window']:false;
echo$before_小部件;
如果($新窗口)
$target=(“空白”);
//显示小部件标题
如果($标题)
回声“”;
//显示名称
如果($name)
printf(“”.$text.“”);
echo$after_小部件;
}
//更新小部件
函数更新($new\u实例,$old\u实例){
$instance=$old_实例;
//从标题和名称中删除标记以删除HTML
$instance['title']=strip_标记($new_instance['title']);
$instance['url']=strip_标记($new_instance['url']);
$instance['text']=strip_标记($new_instance['text']);
$instance['new_window']=($new_instance['new_window']);
返回$instance;
}
函数形式($instance){
//默认小部件设置。
$defaults=array('title'=>\('Sample Button Text','example'),'url'=>\('url'按钮将链接到的url','example'),'Text'=>\('Call to action Text','example'),'show_info'=>true);
$instance=wp_parse_参数((数组)$instance$defaults);?>
我已经解决了下拉列表中的值未保存的问题
根据,在wp\u下拉列表\u array()
中有一个我没有设置的“选定”参数
<?php wp_dropdown_pages(array('id' => $this->get_field_id('url'),'name' => $this->get_field_name('url'), 'selected' => $instance['url'])); ?>