自定义小部件不保存下拉框中的值

时间:2014-12-30 作者:defiant

我正在尝试保存我创建的自定义小部件上下拉框中的值。将保存同一小部件的标题,但不会保存下拉框中的值。谁能帮帮我吗?代码如下:

    <?php // Creating the widget 
class rspwidget extends WP_Widget {

    function __construct() {
    parent::__construct(
    // Base ID of your widget
    \'rspwidget\', 

    // Widget name will appear in UI
    __(\'RSP Ad Widget\', \'rspwidget_ad\'), 

    // Widget description
    array( \'description\' => __( \'Revenue share Plugin(RSP) Widget\', \'rspwidget_ad\' ), ) 
        );
    }

    // Creating widget front-end
    // This is where the action happens
    public function widget( $args, $instance ) {
        echo $args[\'before_widget\'];
        if ( ! empty( $instance[\'title\'] ) ) {
            echo $args[\'before_title\'] . apply_filters( \'widget_title\', $instance[\'title\'] ). $args[\'after_title\'];
        }
        // before and after widget arguments are defined by themes

        // This is where you run the code and display the output
        //echo __( \'Hello, World!\', \'rspwidget_ad\' );
        echo adsensewidgetad();
        echo $args[\'after_widget\'];
    }

    // Widget Backend 
    public function form( $instance ) {
        $title = ! empty( $instance[\'title\'] ) ? $instance[\'title\'] : __( \'\', \'rspwidget_ad\' );
        $widgetads = ! empty( $instance[\'$widgetads\'] ) ? $instance[\'$widgetads\'] : __( \'\', \'rspwidget_ad\' );
    // Widget admin form
?>
    <p>
        <input class="widefat" id="<?php echo $this->get_field_id( \'title\' ); ?>" name="<?php echo $this->get_field_name( \'title\' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />

        <label for="<?php echo $this->get_field_id( \'widgetads\' ); ?>"><?php _e( \'Ad dimensions:\', \'widgetads\' ); ?></label>
        <select id="<?php echo $this->get_field_id( \'widgetads\' ); ?>" name="<?php echo $this->get_field_name( \'widgetads\' ); ?>" type="text">
            <option value="300x250">300x250px</option>
            <option value="336x280">336x280px</option>
            <option value="300x600">300x600px</option>
            <option value="320x100">320x100px</option>
        </select>
    </p>
<?php 
    }

    // Updating widget replacing old instances with new
    public function update( $new_instance, $old_instance ) {
            $instance = array();
            //$instance = $old_instance;
            $instance[\'title\'] = ( ! empty( $new_instance[\'title\'] ) ) ? strip_tags( $new_instance[\'title\'] ) : \'\';
            $instance[\'widgetads\'] = ( ! empty( $new_instance[\'widgetads\'] ) ) ? strip_tags( $new_instance[\'widgetads\'] ) : \'\';
            return $instance;
    }
} // Class rspwidget ends here

//create the ad for the widget
function adsensewidgetad() {  
            $options = get_option(\'RSP_options\');
            if (!get_option(\'RSP_options\')) {
                return \'Configure RSP settings!\';
            }
            $position = \'\';
            if (array_key_exists(\'radio_option1\', $options)) {
                $position =  $options[\'radio_option1\'];
            }
            /*if (array_key_exists(\'radio_option2\', $options)) {
            $position =  $options[\'radio_option2\'];
            }
            if (array_key_exists(\'radio_option3\', $options)) {
            $position =  $options[\'radio_option3\'];
            }*/
            if(get_the_author_meta( \'RSP_text_string\' )){
                $input = array($options[\'RSP_text_string\'], get_the_author_meta( \'RSP_text_string\' ));
            } else {
                $input = array($options[\'RSP_text_string\']);
            }
            shuffle($input);

            if ($input[0] == \'pub-0000\') {
                return \'Configure PUB ID!\';
            }
        $ad_content = \'<div align=center><script type="text/javascript"><!--  
google_ad_client = "ca-\'.$input[0].\'";  
google_ad_width = 300;  
google_ad_height = 250;  
//-->
</script>  
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script></div>\';
        if($position == \'Top\') {
            return $ad_content.\'<br />\'.$content;
        }
        if($position == \'Bottom\') {
            return $content.\'<br />\'.$ad_content;
        }
        if($position == \'Middle\') {
            $count_words = strlen($content)/2;
            $insert_ad = strpos($content, \'. \', $count_words);
            $ad_content = \'<br />\'.$ad_content.\'<br />\';
            return substr_replace($content, $ad_content, $insert_ad+2, 0);
        }
            return $ad_content;
    }

// Register and load the widget
function rsp_load_widget() {
    register_widget( \'rspwidget\' );
}
add_action( \'widgets_init\', \'rsp_load_widget\' );
?>

1 个回复
最合适的回答,由SO网友:jep 整理而成

值已保存,但在打印select时必须选择正确的选项。打印输入字段并添加时相同value="<?php echo esc_attr( $title ); ?>".

您可以尝试在<select></select> 用这个

<option value="300x250" <?php echo "300x250" == $widgetads ? "selected" : ""; ?> >300x250px</option>
<option value="336x280" <?php echo "336x280" == $widgetads ? "selected" : ""; ?>>336x280px</option>
<option value="300x600" <?php echo "300x600" == $widgetads ? "selected" : ""; ?>>300x600px</option>
<option value="320x100" <?php echo "320x100" == $widgetads ? "selected" : ""; ?>>320x100px</option>

结束

相关推荐

必须使用插件自动加载器:如何正确使用get_plugins()?

我的autoloader类负责加载必须使用的插件,这些插件不位于mu-plugins 文件夹要定位它们,我需要使用get_plugins() 作用According to Codex, 该函数接受一个参数:$plugin\\u folder(string)(可选):单个插件文件夹的相对路径。我的文件层次结构如下所示:|-- /mu-plugins | |-- autoload.php // only includes wpmu/autoload.php&#