简体   繁体   English

将额外字段添加到WordPress用户配置文件

[英]Adding Extra Fields to the WordPress User Profile

I have a strange problem. 我有一个奇怪的问题。 I have added custom field to User Profile and used the_author_meta() function for display fields content. 我已将自定义字段添加到用户配置文件,并使用the_author_meta()函数显示字段内容。 Yeasterday works everything great. 昨天的一切都很棒。 But today it doesn't work. 但是今天它不起作用。

Custom field is in User Profile. 自定义字段位于用户个人资料中。 When I click update profile - the content of fields remains in place (so information should be saved). 当我单击更新配置文件时-字段的内容保持不变(因此应保存信息)。 But on authors page where I have <?php the_author_meta( 'skills' ); ?> 但是在作者页面上,我有<?php the_author_meta( 'skills' ); ?> <?php the_author_meta( 'skills' ); ?> happens nothing. <?php the_author_meta( 'skills' ); ?>什么也没发生。

I followed this tutorial when I was creating my own custom field: http://justintadlock.com/archives/2009/09/10/adding-and-using-custom-user-profile-fields 在创建自己的自定义字段时,我遵循了本教程: http : //justintadlock.com/archives/2009/09/10/adding-and-using-custom-user-profile-fields

And this one when I tried to find the error in my code: http://bavotasan.com/2009/adding-extra-fields-to-the-wordpress-user-profile/ 而当我尝试在我的代码中查找错误时,此代码: http : //bavotasan.com/2009/adding-extra-fields-to-the-wordpress-user-profile/

They both are similar. 他们都是相似的。 I worked with page templates since yesterday, so it maybe caused the problem. 从昨天开始我就使用页面模板,因此可能导致了问题。 So I deleted everything what I've added since yesterday. 因此,我删除了自昨天以来添加的所有内容。 But nothing changed. 但是什么都没有改变。

Here is my code: 这是我的代码:

// CUSTOM FIELD IN USER ADMIN - SAVE
add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );


function my_show_extra_profile_fields( $user ) { ?>

    <h3>Skills - Färdigheter</h3>

    <table class="form-table">

        <tr>
            <th><label for="skills">Skills</label></th>

            <td>
      <textarea type="text" name="skills" rows="4" cols="50" id="skills" class="regular-text"><?php echo esc_attr( get_the_author_meta( 'skills', $user->ID ) ); ?></textarea><br>      
                <span class="description">Please enter your skills.</span>
            </td>
        </tr>

    </table>
<?php }  

// CUSTOM FIELD IN USER ADMIN
add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );

function my_save_extra_profile_fields( $user_id ) {

    if ( !current_user_can( 'edit_user', $user_id ) )
        {return false;}

    /* Copy and paste this line for additional fields. Make sure to change 'twitter' to the field ID. */
    update_user_meta( $user_id, 'skills', $_POST['skills'] );
}

Now I think that you will save the user meta successfully. 现在,我认为您将成功保存用户元数据。 Only you need to display from them. 只有您需要显示它们。

Now you can use the function "get_user_meta($user_id, $key, $single);". 现在,您可以使用函数“ get_user_meta($ user_id,$ key,$ single);”。 Visit it here: https://codex.wordpress.org/Function_Reference/get_user_meta Thanks 在这里访问: https : //codex.wordpress.org/Function_Reference/get_user_meta谢谢

Problem solved! 问题解决了! The solution was to use <?php echo $curauth->skills; ?> 解决方案是使用<?php echo $curauth->skills; ?> <?php echo $curauth->skills; ?> instead of <?php the_author_meta( 'skills' ); ?> <?php echo $curauth->skills; ?>代替<?php the_author_meta( 'skills' ); ?> <?php the_author_meta( 'skills' ); ?> or <?php get_user_meta($user_id, $key, $single); ?> <?php the_author_meta( 'skills' ); ?><?php get_user_meta($user_id, $key, $single); ?> <?php get_user_meta($user_id, $key, $single); ?>

I don't know why, but it doesn't matter :D 我不知道为什么,但是没关系:D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM