WordPress自定义用户个人资料字段

时间:2018-07-18 10:09:35

标签: php wordpress metadata

我只喜欢一些代码,似乎无法使其正常工作。

我经验不是很多,但是我的目标是在用户个人资料中添加其他字段。

这是代码

add_action( 'show_user_profile', 'display_user_address' );
add_action( 'edit_user_profile', 'display_user_address' );
add_action( 'show_user_profile', 'display_user_age' );
add_action( 'edit_user_profile', 'display_user_age' );
add_action( 'show_user_profile', 'display_user_birthdate' );
add_action( 'edit_user_profile', 'display_user_birthdate' );
add_action( 'show_user_profile', 'display_user_gender' );
add_action( 'edit_user_profile', 'display_user_gender' );
add_action( 'show_user_profile', 'display_user_height' );
add_action( 'edit_user_profile', 'display_user_height' );
add_action( 'show_user_profile', 'display_user_weight' );
add_action( 'edit_user_profile', 'display_user_weight' );
add_action( 'show_user_profile', 'display_user_school_name' );
add_action( 'edit_user_profile', 'display_user_school_name' );
add_action( 'show_user_profile', 'display_user_grade' );
add_action( 'edit_user_profile', 'display_user_grade' );
add_action( 'show_user_profile', 'display_user_uniform_size' );
add_action( 'edit_user_profile', 'display_user_uniform_size' );
add_action( 'show_user_profile', 'display_user_medical_conditions' );
add_action( 'edit_user_profile', 'display_user_medical_conditions' );
add_action( 'show_user_profile', 'display_user_concussion' );
add_action( 'edit_user_profile', 'display_user_concussion' );
add_action( 'show_user_profile', 'display_user_er_contact_1' );
add_action( 'edit_user_profile', 'display_user_er_contact_1' );
add_action( 'show_user_profile', 'display_user_er_contact_2' );
add_action( 'edit_user_profile', 'display_user_er_contact_2' );
add_action( 'show_user_profile', 'display_user_roster_freeze' );
add_action( 'edit_user_profile', 'display_user_roster_freeze' );


function display_user_address( $user ) { ?>
function display_user_age( $user ) { ?>
function display_user_birthdate( $user ) { ?>
function display_user_gender( $user ) { ?>
function display_user_height( $user ) { ?>
function display_user_weight( $user ) { ?>
function display_user_school_name( $user ) { ?>
function display_user_grade( $user ) { ?>
function display_user_uniform_size( $user ) { ?>
function display_user_medical_conditions( $user ) { ?>
function display_user_concussion( $user ) { ?>
function display_user_er_contact_1( $user ) { ?>
function display_user_er_contact_2( $user ) { ?>
function display_user_roster_freeze( $user ) { ?>

    <h3>Player Stats / Additional Information</h3>
    <table class="form-table">
        <tr>
            <th><label>Address</label></th>
            <td><input type="text" value="<?php echo get_user_meta( $user->ID, 'user_address', true ); ?>" class="regular-text" readonly=readonly /></td>
        </tr>
        
        <tr>
            <th><label>Age</label></th>
            <td><input type="text" value="<?php echo get_user_meta( $user->ID, 'user_age', true ); ?>" class="regular-text" readonly=readonly /></td>
        </tr>
        
        <tr>
            <th><label>Birthdate</label></th>
            <td><input type="text" value="<?php echo get_user_meta( $user->ID, 'user_birthdate', true ); ?>" class="regular-text" readonly=readonly /></td>
        </tr>

        <tr>
            <th><label>Gender</label></th>
            <td><input type="text" value="<?php echo get_user_meta( $user->ID, 'user_gender', true ); ?>" class="regular-text" readonly=readonly /></td>
        </tr>

        <tr>
            <th><label>Height</label></th>
            <td><input type="text" value="<?php echo get_user_meta( $user->ID, 'user_height', true ); ?>" class="regular-text" readonly=readonly /></td>
        </tr>

        <tr>
            <th><label>Weight</label></th>
            <td><input type="text" value="<?php echo get_user_meta( $user->ID, 'user_weight', true ); ?>" class="regular-text" readonly=readonly /></td>
        </tr>

        <tr>
            <th><label>School Name</label></th>
            <td><input type="text" value="<?php echo get_user_meta( $user->ID, 'user_school_name', true ); ?>" class="regular-text" readonly=readonly /></td>
        </tr>

        <tr>
            <th><label>School Grade</label></th>
            <td><input type="text" value="<?php echo get_user_meta( $user->ID, 'user_grade', true ); ?>" class="regular-text" readonly=readonly /></td>
        </tr>
        
        <tr>
            <th><label>Uniform Size</label></th>
            <td><input type="text" value="<?php echo get_user_meta( $user->ID, 'user_uniform_size', true ); ?>" class="regular-text" readonly=readonly /></td>
        </tr>

        <tr>
            <th><label>Medical Contidions</label></th>
            <td><input type="text" value="<?php echo get_user_meta( $user->ID, 'user_medical_conditions', true ); ?>" class="regular-text" readonly=readonly /></td>
        </tr>

        <tr>
            <th><label>Have you ever had a concussion?</label></th>
            <td><input type="text" value="<?php echo get_user_meta( $user->ID, 'user_concussion', true ); ?>" class="regular-text" readonly=readonly /></td>
        </tr>

        <tr>
            <th><label>Emergency contact 1</label></th>
            <td><input type="text" value="<?php echo get_user_meta( $user->ID, 'user_er_contact_1', true ); ?>" class="regular-text" readonly=readonly /></td>
        </tr>

        <tr>
            <th><label>Emergency contact 2</label></th>
            <td><input type="text" value="<?php echo get_user_meta( $user->ID, 'user_er_contact_2', true ); ?>" class="regular-text" readonly=readonly /></td>
        </tr>
        
        <tr>
            <th><label>Roster Freeze</label></th>
            <td><input type="text" value="<?php echo get_user_meta( $user->ID, 'user_roster_freeze', true ); ?>" class="regular-text" readonly=readonly /></td>
        </tr>
    </table>
    <?php
}

我希望将代码添加到functions.php文件中,因为我希望能够在更新父主题时保留所做的修改。

不需安装插件的任何帮助或建议,将不胜感激。

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以使用用户的挂钩在用户个人资料中添加其他字段。将此代码添加到您的functions.php

<?php 
add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );
function extra_user_profile_fields( $user ) { ?>
<h3><?php _e("Extra profile information", "blank"); ?></h3>
<table class="form-table">
<tr>
<th><label for="address"><?php _e("Address"); ?></label></th>
<td><input type="text" name="address" id="address" value="<?php echo esc_attr( get_the_author_meta( 'address', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter your address."); ?></span>
</td>
</tr>
<tr>
<th><label for="city"><?php _e("age"); ?></label></th>
<td>
<input type="text" name="age" id="age" value="<?php echo esc_attr( get_the_author_meta( 'age', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter your age."); ?></span>
</td>
</tr>
</table>
<?php }
add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );
function save_extra_user_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }
update_user_meta( $user_id, 'address', $_POST['address'] );
update_user_meta( $user_id, 'age', $_POST['age'] );
}
?>