【问题标题】:retrieve value widget form option wordpress检索值小部件表单选项 wordpress
【发布时间】:2016-12-13 04:44:39
【问题描述】:

我是新手 wordpress 和 php。 我写了一个带有 select 的表单并将其放入我的小部件文件中的function form()

但我不知道如何获得选项的价值。

这是我的表单 html:

<label>Chose one:</label>
<select name="test">
<option value="One">Select One</option>
<option value="Two">Select Two</option>
<option value="Three">Select Three</option>
</select> 

我曾尝试通过以下方式检索值:$get_var = $POST[test]; 并在我的小部件代码显示的地方尝试var_dump (function widget()) 但它返回 null。

请帮助我,非常感谢!

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    按照以下格式制作表格

    public function form( $instance ) {
            $title = ! empty( $instance['title'] ) ? $instance['title'] : esc_html__( 'New title', 'text_domain' );
            ?>
            <p>
            <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_attr_e( 'Title:', 'text_domain' ); ?></label> 
            <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
            </p>
            <?php 
        }
    

    以以下格式将值更新到数据库

    public function update( $new_instance, $old_instance ) {
            $instance = array();
            $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
    
            return $instance;
        }
    

    然后您可以通过以下方式在前端获取值:

    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'];
            }
            echo esc_html__( 'Hello, World!', 'text_domain' );
            echo $args['after_widget'];
        }
    

    详情请阅读:https://codex.wordpress.org/Widgets_API

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多