我试图在我的窗口小部件后端中包含一个复选框。但是在用户提交后,我无法获取该值(打开或关闭)。我以为该值将存储在“ esc_attr($ check)”中(使用文本输入时就是这样),但我无法检索它。

这是我现在正在尝试的操作:

public function form( $instance ) {
    $check = isset( $instance[ 'check' ] ) ? $instance[ 'check' ] : 'off';
    echo esc_attr( $check ); // If the input is type text it outputs the value
    ?>
    <input class="widefat" id="<?php echo $this->get_field_id( 'check' ); ?>" name="<?php echo $this->get_field_name( 'check' ); ?>" type="checkbox" />
    <?php 
}


我该如何使用它?另外,如何获取前端复选框的值?

#1 楼

首先,在功能窗口小部件上:

function widget( $args, $instance ) {
    extract( $args );
    // Add this line
    $your_checkbox_var = $instance[ 'your_checkbox_var' ] ? 'true' : 'false';
    // Change 'your_checkbox_var' for your custom ID
    // ...
}


在功能更新上:

function update( $new_instance, $old_instance ) {
    $instance = $old_instance;
    // Add this line
    $instance[ 'your_checkbox_var' ] = $new_instance[ 'your_checkbox_var' ];
    // Change 'your_checkbox_var' for your custom ID
    // ...
    return $instance;
}


最后,在功能表上,添加以下内容:

<p>
    <input class="checkbox" type="checkbox" <?php checked( $instance[ 'your_checkbox_var' ], 'on' ); ?> id="<?php echo $this->get_field_id( 'your_checkbox_var' ); ?>" name="<?php echo $this->get_field_name( 'your_checkbox_var' ); ?>" /> 
    <label for="<?php echo $this->get_field_id( 'your_checkbox_var' ); ?>">Label of your checkbox variable</label>
</p>
<!-- Remember to change 'your_checkbox_var' for your custom ID, as well -->


编辑:
让我们使用复选框显示/隐藏头像图像来查看“关于我们”小部件的完整代码:

class about_us extends WP_Widget {

function about_us() {
    $widget_ops = array( 'classname' => 'about_us', 'description' => __( 'About Us', 'wptheme' ) );
    $this->WP_Widget( 'about_us', __( 'About Us', 'wptheme' ), $widget_ops, $control_ops );
}

function widget( $args, $instance ) {
    extract( $args );
    $title = apply_filters( 'widget_title', $instance[ 'title' ] );
    $text = $instance[ 'text' ];
    // The following variable is for a checkbox option type
    $avatar = $instance[ 'avatar' ] ? 'true' : 'false';

    echo $before_widget;

        if ( $title ) {
            echo $before_title . $title . $after_title;
        }

        // Retrieve the checkbox
        if( 'on' == $instance[ 'avatar' ] ) : ?>
            <div class="about-us-avatar">
                <?php echo get_avatar( get_the_author_meta( 'user_email' ), '50', '' ); ?>
            </div>
        <?php endif; ?>

        <div class="textwidget">
            <p><?php echo esc_attr( $text ); ?></p>
        </div>

        <?php 
    echo $after_widget;
}

function update( $new_instance, $old_instance ) {
    $instance = $old_instance;
    $instance[ 'title' ] = strip_tags( $new_instance[ 'title' ] );
    $instance[ 'text' ] = strip_tags( $new_instance[ 'text' ] );
    // The update for the variable of the checkbox
    $instance[ 'avatar' ] = $new_instance[ 'avatar' ];
    return $instance;
}

function form( $instance ) {
    $defaults = array( 'title' => __( 'About Us', 'wptheme' ), 'avatar' => 'off' );
    $instance = wp_parse_args( ( array ) $instance, $defaults ); ?>
    <p>
        <label for="<?php echo $this->get_field_id( 'title' ); ?>">Title</label>
        <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( $instance[ 'title' ] ); ?>" />
    </p>
    <!-- The checkbox -->
    <p>
        <input class="checkbox" type="checkbox" <?php checked( $instance[ 'avatar' ], 'on' ); ?> id="<?php echo $this->get_field_id( 'avatar' ); ?>" name="<?php echo $this->get_field_name( 'avatar' ); ?>" /> 
        <label for="<?php echo $this->get_field_id( 'avatar' ); ?>">Show avatar</label>
    </p>
    <p>
        <label for="<?php echo $this->get_field_id( 'text' ); ?>">About Us</label>
        <textarea class="widefat" id="<?php echo $this->get_field_id( 'text' ); ?>" rows="10" cols="10" name="<?php echo $this->get_field_name( 'text' ); ?>"><?php echo esc_attr( $instance[ 'text' ] ); ?></textarea>
    </p>
<?php
}

}
register_widget( 'about_us' );


经过测试,可以正常工作。

编辑(2015年9月8日):重要!该小部件示例使用PHP4样式的构造函数,但是WordPress 4.3切换到PHP5,因此您也应该切换构造函数。更多信息,在这里。

如果您使用“主题检查”插件,则会看到一条提示,建议您使用__construct()而不是WP_Widget。删除第一个功能并添加以下功能:

function __construct() {
    parent::__construct(
        'about_us', // Base ID
        __( 'About US', 'wptheme' ), // Name
        array( 'description' => __( 'About Us', 'wptheme' ), ) // Args
    );
}


评论


是的,我在小部件上使用它来启用/禁用头像图片。对我来说很好。

–杰拉德
2013年9月2日在16:25

好。我想为了更清楚起见,您应该在答案中添加函数形式上$ instance ['your_checkbox_var']的默认分配。

– gmazzap♦
2013年9月2日在16:55



默认分配是“头像”,而不是“ your_checkbox_var”。实际上,我写了“ your_checkbox_var”以使其更加清晰。无论如何,我将使用默认示例修改答案。谢谢你的建议 :)

–杰拉德
2013年9月2日17:20在

@杰拉德,尝试将头像默认设置为打开,您将无法将其关闭

–贝恩
17 Mar 6 '17 at 15:19

当我使用“ on”和“ off”代替功能中的TRUe和Falso时,这对我有用。我还对if语句使用了简单的检查。if('on'= $ myinstance){...我的代码...}

–熟练的家庭
19年4月6日在18:40