【问题标题】:Wordpress Custom Widget Select Options Not SavingWordpress 自定义小部件选择选项不保存
【发布时间】:2013-07-14 12:28:25
【问题描述】:

我对 Wordpress 插件/小部件的编码非常陌生(这是我的第一次!)。我已经构建了一个非常基本的 Wordpress 插件,它由 2 个文本输入和一个选择字段组成。文本输入工作正常,但是当我点击“保存”按钮时,选择框似乎没有保存。

这是我的插件代码:

<?php
/* Plugin Name: Sidebar Box
Plugin URI: http://www.website.com
Description: Displays contact box in sidebar
Version: 1.0
Author: JM
Author URI: N/A
*/

// use widgets_init action hook to execute custom function
add_action( 'widgets_init', 'jm_box_widget' );

 //register our widget
function jm_box_widget() {
    register_widget( 'jm_box_widget_my_info' );
}

//boj_widget_my_info class
class jm_box_widget_my_info extends WP_Widget {

    //process the new widget
    function jm_box_widget_my_info() {
        $widget_ops = array( 
            'classname' => 'jm_box_widget_class', 
            'description' => 'Sidebar Box Widget.'
            ); 
        $this->WP_Widget( 'jm_box_widget_my_info', 'Box Widget', $widget_ops );
    }

     //build the widget settings form
    function form($instance) {
        $defaults = array( 'title' => 'Box Page Widget', 'description' => '', 'boxtype' => '' ); 
        $instance = wp_parse_args( (array) $instance, $defaults );
        $title = $instance['title'];
        $description = $instance['description'];
        $boxtype = $instance['boxtype'];

        ?>
            <p>Title: <input class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>"  type="text" value="<?php echo esc_attr( $title ); ?>" /></p>
            <p>Description: <textarea class="widefat" name="<?php echo $this->get_field_name( 'description' ); ?>" / ><?php echo esc_attr( $description ); ?></textarea></p>
    <p>Sex:
            <select id="<?php echo $this->get_field_id( 'boxtype' ); ?>" name="<?php echo $this->get_field_name( 'boxtype' ); ?>" class="widefat" style="width:100%;">
            <option <?php if ( 'box1' == $instance['format'] ) echo 'selected="selected"'; ?> value="box1">box1</option>
    <option <?php if ( 'box2' == $instance['format'] ) echo 'selected="selected"'; ?> value="box2">box2</option>
            </select>
        </p>
        <?php
    }

    //save the widget settings
    function update($new_instance, $old_instance) {
        $instance = $old_instance;
        $instance['title'] = strip_tags( $new_instance['title'] );
        $instance['description'] = strip_tags( $new_instance['description'] );
        $instance['boxtype'] = ( $new_instance['boxtype'] ); 
        return $instance;
    }

    //display the widget
    function widget($args, $instance) {
        extract($args);

        echo $before_widget;
        $title = apply_filters( 'widget_title', $instance['title'] );
        $description = empty( $instance['description'] ) ? '&nbsp;' : $instance['description']; 
        $boxtype = empty( $instance['boxtype'] ) ? '&nbsp;' : $instance['boxtype']; 

        echo '<div class="sidebar-box" id="' . $boxtype . '" onmouseover="this.style.cursor=\'pointer\'" onmouseup="window.location=\'' . $boxtype . '\'">
                <h3>' . $title . '</h3>
                <p>' . $description . '</p>
                </div>';
        echo $after_widget;
    }
}

?>

我这辈子都不能锻炼,为什么不省钱。

任何帮助将不胜感激。

谢谢,

詹姆斯

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    您必须在某处将 $instance['format'] 更改为 $instance['boxtype'] 但不在表单中。选项需要更改。

    <option <?php if ('box1' == $boxtype )
    

    【讨论】:

      猜你喜欢
      • 2013-10-15
      • 1970-01-01
      • 1970-01-01
      • 2013-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多