【问题标题】:Adding Extra Fields to the WordPress User Profile向 WordPress 用户配置文件添加额外字段
【发布时间】:2015-03-31 23:02:04
【问题描述】:

我有一个奇怪的问题。我已将自定义字段添加到 User Profile 并使用 the_author_meta() 函数显示字段内容。昨天一切都很好。但是今天它不起作用。

自定义字段位于用户配置文件中。当我单击更新配置文件时 - 字段内容保持不变(因此应保存信息)。但是在我有<?php the_author_meta( 'skills' ); ?> 的作者页面上什么也没发生。

我在创建自己的自定义字段时遵循了本教程: http://justintadlock.com/archives/2009/09/10/adding-and-using-custom-user-profile-fields

当我试图在我的代码中找到错误时,这是​​一个: http://bavotasan.com/2009/adding-extra-fields-to-the-wordpress-user-profile/

它们都相似。我从昨天开始使用页面模板,所以它可能会导致问题。因此,我删除了自昨天以来添加的所有内容。但什么都没有改变。

这是我的代码:

// 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'] );
}

【问题讨论】:

  • 我试过你的代码,它工作正常。你确定你在the loop 中调用&lt;?php the_author_meta( 'skills' ); ?&gt; 吗?

标签: php wordpress wordpress-theming


【解决方案1】:

现在我认为您将成功保存用户元数据。只有您需要从它们中显示。

现在您可以使用函数“get_user_meta($user_id, $key, $single);”。 访问这里:https://codex.wordpress.org/Function_Reference/get_user_meta 谢谢

【讨论】:

  • 感谢您的建议,但它不起作用(可能是因为我没有循环..我不知道)。我使用了echo $curauth-&gt;skills,它可以工作。
【解决方案2】:

问题解决了!解决方案是使用 &lt;?php echo $curauth-&gt;skills; ?&gt; 而不是 &lt;?php the_author_meta( 'skills' ); ?&gt;&lt;?php get_user_meta($user_id, $key, $single); ?&gt;

我不知道为什么,但没关系:D

【讨论】:

    猜你喜欢
    • 2013-04-01
    • 2013-05-15
    • 1970-01-01
    • 1970-01-01
    • 2019-07-07
    • 1970-01-01
    • 2021-09-29
    • 1970-01-01
    • 2015-08-26
    相关资源
    最近更新 更多