douxian5076 2018-10-26 16:03
浏览 45

无法获取HTML输入复选框以使用WordPress正常工作

There's something wrong with how I'm managing my checkbox with in my HTML input element - as in the database, the value stays at 1, and the form always shows 1, no matter if it's un-ticked.

Any idea please?

I've googled a lot but I'm also struggling to understand the concept of how the checkbox works with the setting of (in HTML) and storing of (in PHP), as most of the results are for post_meta, but this is for user_meta key / value.

<?php

// set the HTML for the inputs
function show_extra_profile_fields( $user ) {
    $is_user_archived = get_user_meta( $user->ID, 'archived_user', true );
    ?>
    <table class="form-table">
        <tr>
            <th><label for="archived_user"><?php esc_html_e( 'Archive user', 'xlearn' ); ?></label></th>
            <td>
                <input type="checkbox"
                       id="archived_user"
                    name="archived_user"
                    class="checkbox"
                    value="yes"
                    <?php if (get_user_meta( $user->ID, 'archived_user', true ) == "1") echo "checked" ;?>
                />
            </td>
        </tr>
    </table>
    <?php
}
add_action( 'edit_user_profile', 'show_extra_profile_fields' );

// update the user meta on save
function update_profile_fields( $user_id ) {
    if ( !current_user_can( 'edit_user', $user_id ) ) {
        return false;
    }
    if ( isset( $_POST['archived_user'] ) ) {
         update_user_meta( $user_id, 'archived_user', $_POST['archived_user'] );
    }    
}
add_action( 'edit_user_profile_update', 'update_profile_fields' ); 
  • 写回答

1条回答 默认 最新

  • dousi1994 2018-10-27 11:19
    关注

    Ok so I eventually figured this out - I changed some of the form inputs & also some of the PHP validation:

    I was also missing: form method="post" in the FORM tag

        // set the HTML for the inputs
        function show_extra_profile_fields( $user ) {
            $is_user_archived = get_user_meta( $user->ID, 'archived_user', true );
    
            /* if you want a heading
            // insert below into HTML area after PHP end tag
            <h3><?php esc_html_e( 'Personal Information', 'xlearn' ); ?></h3> */
    
            ?>
            <table class="form-table">
                <tr>
                    <th><label for="archived_user"><?php esc_html_e( 'Archive user', 'xlearn' ); ?></label></th>
                    <td>
                        <form method="post">
                            <input type="checkbox"
                                   id="archived_user"
                                name="archived_user"
                                class="checkbox"
                                value="checked"
    
                                <?php
                                    // GET VALUES FROM THE DATABSE:
    
                                    // if meta key is set
                                    if ($is_user_archived == "1") {
                                        echo 'value="1"';
                                        echo 'checked="1"';
                                    }
                                    // if meta key is not present or = 0
                                    else if (($is_user_archived == "") | ($is_user_archived == "0")) {
                                        echo 'value="0"';
                                    }
                                ?>
    
                                />
                        </form>
                        <label for="archived_user">This will disable the user from logging in, but will not delete the user</label>
                    </td>
                </tr>
            </table>
            <?php
        }
        add_action( 'edit_user_profile', 'show_extra_profile_fields' );
    

    Take note of the 2 x $_POST['archived_user'] areas below:

        // update the user meta on save
        function update_profile_fields( $user_id ) {
            // check for capabilities
            if ( !current_user_can( 'edit_user', $user_id ) ) {
                return false;
            }
            // ticked
            if ( ( isset( $_POST['archived_user'] ))) {
                 update_user_meta( $user_id, 'archived_user', "1" );
            }
            // not ticked
            if ( ( !isset( $_POST['archived_user'] ))) {
                 update_user_meta( $user_id, 'archived_user', "0" );
            }
        }
        add_action( 'edit_user_profile_update', 'update_profile_fields' ); 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大