【问题标题】:Adding a custom field to the user edit page within an existing section将自定义字段添加到现有部分中的用户编辑页面
【发布时间】:2016-09-06 02:44:37
【问题描述】:

我正在尝试向 WordPress 上的 user-edit.php 页面添加一个额外的字段。

我想将其添加到“个人信息”字段正上方的“关于此用户/您”部分。我已经编辑了文件以添加另一个文件,但由于某种原因,它不会保存我输入的信息。

<table class="form-table">
<tr class="user-job-title-wrap">
    <th><label for="job_title"><?php _e('Job Title'); ?></label></th>
    <td><input type="text" name="job_title" id="job_title" value="<?php echo esc_attr( $profileuser->job_title ) ?>" class="regular-text ltr" />
</td>
</tr>
<tr class="user-description-wrap">
    <th><label for="description"><?php _e('Biographical Info'); ?></label></th>
    <td><textarea name="description" id="description" rows="5" cols="30"><?php echo $profileuser->description; // textarea_escaped ?></textarea>
    <p class="description"><?php _e('Share a little biographical information to fill out your profile. This may be shown publicly.'); ?></p></td>
</tr>
...

它确实将正确的 meta_key 添加到 wp_usermeta db,但 meta_value 保持空白。如果我在数据库中手动输入 meta_value 并重新加载用户编辑页面,则该值会正确加载到输入字段中,但对该字段所做的任何更改都不会记录到数据库中。

【问题讨论】:

  • 如果您愿意,请保持原样,只需使用 save_extra_user_fields() 挂钩函数,因为它解决了您的记录到数据库的问题。

标签: php html wordpress textfield


【解决方案1】:

这里是添加字段和保存更新的字段数据的 wordpress 钩子:

// ADDING CUSTOM FIELDS TO INDIVIDUAL USER SETTINGS PAGE AND TO USER LIST
add_action( 'show_user_profile', 'add_extra_user_fields' );
add_action( 'edit_user_profile', 'add_extra_user_fields' );
function add_extra_user_fields( $user )
{
    ?>
        <h3><?php _e("My TITLE", "my_theme_domain"); ?></h3>
        <table class="form-table">
            <tr class="user-job-title-wrap">
                <th><label for="job_title"><?php _e('Job Title'); ?></label></th>
                <td><input type="text" name="job_title" id="job_title" value="<?php echo esc_attr( $profileuser->job_title ) ?>" class="regular-text ltr" />
            </td>
            </tr>
            <tr class="user-description-wrap">
                <th><label for="description"><?php _e('Biographical Info'); ?></label></th>
                <td><textarea name="description" id="description" rows="5" cols="30"><?php echo $profileuser->description; // textarea_escaped ?></textarea>
                <p class="description"><?php _e('Share a little biographical information to fill out your profile. This may be shown publicly.'); ?></p></td>
            </tr>
        </table>
    <?php
}

--(已更新)-- 您只能使用下面的save_extra_user_fields 来解决您的问题(...保存信息...),如果您愿意,可以保留您的代码。

// Saving Updated fields data
add_action( 'personal_options_update', 'save_extra_user_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_fields' );
function save_extra_user_fields( $user_id )
{
    update_user_meta( $user_id, 'job_title', sanitize_text_field( $_POST['job_title'] ) );
    update_user_meta( $user_id, 'description', sanitize_text_field( $_POST['description'] ) );
}

此代码在您的活动子主题或主题的function.php 文件中...

在 php 中使用您的自定义用户字段数据:

  • 返回数据:get_the_author_meta( 'your_field_slug', $userID );
  • 显示数据(回显数据):the_author_meta( 'your_field_slug', $userID );

【讨论】:

  • 没有钩子可以做到这一点吗?由于钩子在页面底部创建了一个新的“部分”,但我想基本上将它添加到传记信息文本框之前。我不介意编辑核心文件。
  • 非常感谢,这真的很有帮助。我试图使用与您的编辑相同的代码,但我尝试使用 $profileuser-&gt;job_title 而不是 get_the_author_meta() 检索数据
  • 完成!对于那个很抱歉。我以前肯定是打算这样做的。再次感谢您的帮助。我不是 100% 我需要使用什么钩子。一切都按照现在需要的方式运行!
猜你喜欢
  • 2018-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-30
  • 1970-01-01
  • 2020-01-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多