如何在配置文件页面中添加复选框和单选按钮

时间:2012-04-21 作者:dev-jim

我喜欢通过添加新字段来定制我的个人资料页面,以从用户那里获取更多信息,如“性别”或“语言”。我设法让文本输入表单工作,现在的问题是收音机和复选框类型的输入。这是我的代码:

function my_user_field( $user ) {
            ?>
                <h3><?php _e(\'More About You\'); ?></h3>
                <table class="form-table">
                    <tr>
                        <th>
                            <label for="Dealing Type"><?php _e(\'Gender\'); ?>
                        </label></th>
                        <td><span class="description"><?php _e(\'Gender?\'); ?></span><br>
                        <label><input type="radio" name="dealing" value="Male">Male<br /></label>
                        <label><input type="radio" name="dealing" value="Female">Female<br /></label>

                        </td>
                    </tr>

                    <tr>
                        <th>
                            <label for="company"><?php _e(\'Company\'); ?>
                        </label></th>
                        <td>
                          <span class="description"><?php _e(\'Insert Your Company name\'); ?></span><br>
                            <input type="text" name="company" id="company" value="<?php echo esc_attr( get_the_author_meta( \'company\', $user->ID ) ); ?>" class="regular-text" /><br />

                        </td>
                    </tr>

                    <tr>
                        <th>
                            <label for="language"><?php _e(\'Language\'); ?>
                        </label></th>
                        <td><input type="checkbox" name="Engilsh" value="Malay" /> English<br />
                            <input type="checkbox" name="language" value="Mandarin" /> Mandarin<br />


                        </td>
                    </tr>
                </table>

            <?php }


            function my_save_custom_user_profile_fields( $user_id ) {
                if ( !current_user_can( \'edit_user\', $user_id ) )
                    return FALSE;
                update_usermeta( $user_id, \'gender\', $_POST[\'gender\'] );
                update_usermeta( $user_id, \'company\', $_POST[\'company\'] );
                update_usermeta( $user_id, \'language\', $_POST[\'language\'] );

            }
            add_action( \'show_user_profile\', \'my_user_field\' );
            add_action( \'edit_user_profile\', \'my_user_field\' );
            add_action( \'personal_options_update\', \'my_save_custom_user_profile_fields\' );
            add_action( \'edit_user_profile_update\', \'my_save_custom_user_profile_fields\' );
“性别”和“语言”没有通过$\\u post[]传递。我是新来的编码,希望你们能帮忙。

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

您缺少输入的“已检查”值

<input type="checkbox" name="language" <?php if (get_the_author_meta( \'language\', $user->ID) == \'Mandarin\' ) { ?>checked="checked"<?php }?> value="Mandarin" /> Mandarin<br />
此外,usermeta正在进行交易,但您正在检查$_POST[\'gender\']

最后,您应该有一个用于英语的usermeta和另一个用于普通话的usermeta,因为它们不是相互排斥的

[编辑:工作代码][编辑2:多种语言]

<?php
$lingo = array(\'en\' => \'English\', \'md\' => \'普通話\', \'es\' => \'Español\', \'fr\' => \'Français\', \'pt\' => \'Português\');

function my_user_field( $user ) {
    $gender = get_the_author_meta( \'dealing\', $user->ID);
    $company = esc_attr( get_the_author_meta( \'company\', $user->ID ) );
?>
    <h3><?php _e(\'More About You\'); ?></h3>
    <table class="form-table">
        <tr>
            <th>
                <label for="Dealing Type"><?php _e(\'Gender\'); ?>
            </label></th>
            <td><span class="description"><?php _e(\'Gender?\'); ?></span><br>
            <label><input type="radio" name="dealing" <?php if ($gender == \'Male\' ) { ?>checked="checked"<?php }?> value="Male">Male<br /></label>
            <label><input type="radio" name="dealing" <?php if ($gender == \'Female\' ) { ?>checked="checked"<?php }?> value="Female">Female<br /></label>

            </td>
        </tr>
        <tr>
            <th>
                <label for="company"><?php _e(\'Company\'); ?>
            </label></th>
            <td>
              <span class="description"><?php _e(\'Insert Your Company name\'); ?></span><br>
                <input type="text" name="company" id="company" value="<?php echo $company; ?>" class="regular-text" /><br />
            </td>
        </tr>
        <tr>
            <th>
                <?php _e(\'Language\'); ?>
            </th>
            <td><?php
                global $lingo;
                foreach($lingo as $key => $value) {
                    $code = \'language_\'.$key;
                    $lang = get_the_author_meta( $code, $user->ID);
                     ?>
                    <label><input type="checkbox" name="<?php echo $code; ?>" <?php if ($lang == \'yes\' ) { ?>checked="checked"<?php }?> value="yes" /> <?php echo $value; ?></label><br />
                <?php }
            ?>
            </td>
        </tr>
    </table>
<?php 
}


function my_save_custom_user_profile_fields( $user_id ) {
    if ( !current_user_can( \'edit_user\', $user_id ) )
        return FALSE;

    update_usermeta( $user_id, \'dealing\', $_POST[\'dealing\'] );
    update_usermeta( $user_id, \'company\', $_POST[\'company\'] );

    global $lingo;
    foreach($lingo as $key => $value) {
        $code = "language_".$key;
        update_usermeta( $user_id, $code, $_POST[$code] );
    }
}

add_action( \'show_user_profile\', \'my_user_field\' );
add_action( \'edit_user_profile\', \'my_user_field\' );
add_action( \'personal_options_update\', \'my_save_custom_user_profile_fields\' );
add_action( \'edit_user_profile_update\', \'my_save_custom_user_profile_fields\' );

结束

相关推荐

Admin Theme customization

我遵循wordpress codex网站上关于通过插件创建管理主题的说明。我激活了插件,但我的样式表没有包含在<head>.. 这是我的代码:add_action( \'admin_init\', \'kd_plugin_admin_init\' ); add_action( \'admin_menu\', \'kd_plugin_admin_menu\' ); function kd_plugin_admin_init() { /* Register