首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Wordpress获取在自定义字段中注册的日期,但可以编辑它吗?

Wordpress获取在自定义字段中注册的日期,但可以编辑它吗?
EN

Stack Overflow用户
提问于 2018-07-16 04:16:32
回答 1查看 650关注 0票数 0

我正在尝试在用户配置文件中添加一个自定义字段到wordpress的后端。我想让它注册日期,但是有了它,你就可以编辑字段并覆盖它了?

我目前已经让它拉取注册的日期,但需要一种方法,所以它只是一开始拉日期注册值,但如果修改将覆盖?

代码语言:javascript
复制
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>Date Registered</h3>
<?php $userr_ID = get_current_user_id(); ?>
    <table class="form-table">
    <tr>
        <th><label for="date_reg"><?php _e("Date Registered"); ?></label></th>
        <td>
            <input type="text" name="datereg" id="datereg" value="<?php echo the_author_meta( 'user_registered', $userr_ID ); ?>" class="regular-text" /><br />
        </td>
    </tr>

    </table>
<?php }
EN

回答 1

Stack Overflow用户

发布于 2018-07-16 04:29:15

旧的坏代码。

代码语言:javascript
复制
add_action('edit_user_profile_update', 'update_extra_profile_fields');

function update_extra_profile_fields($userID) {
     if ( current_user_can('edit_user',$userID) )
         update_user_meta($userID, 'user_registered', current_time( 'mysql' ));
}
function extra_user_profile_fields( $user ) {
    ?>
    <h3>Date Registered</h3>
    <table class="form-table">
        <tr>
            <th><label for="date_reg"><?php _e("Date Registered"); ?></label></th>
            <td>
                <input type="text" name="datereg" id="datereg" value="<?php echo get_the_author_meta( 'user_registered', $user->ID); ?>" class="regular-text" /><br />
            </td>
        </tr>
    </table>
    <?php
}
add_action( 'show_user_profile', 'extra_user_profile_fields', 10, 1 );
add_action( 'edit_user_profile', 'extra_user_profile_fields', 10, 1 );

已更新工作代码。

代码语言:javascript
复制
add_action('profile_update', 'update_extra_profile_fields', 10, 2);
function update_extra_profile_fields($userID, $oldData) {
    if ( current_user_can('edit_user',$userID) ) {
        $updattionDate = date('Y-m-d H:i:s');
        $update = update_user_meta($userID, 'user_registered', $updattionDate);
    }
}
function extra_user_profile_fields( $user ) {
    ?>
    <h3>Date Registered</h3>
    <table class="form-table">
        <tr>
            <th><label for="date_reg"><?php _e("Date Registered"); ?></label></th>
            <td>
                <?php 
                $registrationTime = get_user_meta($user->ID, 'user_registered', true);; 
                $registrationDate = date('Y-m-d H:i:s', strtotime(date($registrationTime)));
                ?>
                <input type="text" name="datereg" id="datereg" value="<?php echo $registrationDate;  ?>" class="regular-text" /><br />
            </td>
        </tr>
    </table>
    <?php
}
add_action( 'show_user_profile', 'extra_user_profile_fields', 10, 1 );
add_action( 'edit_user_profile', 'extra_user_profile_fields', 10, 1 );

我注意到我的个人资料更新代码中有一个错误,即错误的钩子。用户配置文件更新事件的正确值是profile_update,而不是edit_user_profile_update

即使修复了这个问题,你仍然看不到正确的注册日期,因为我们为它调用了错误的函数。我们使用get_the_author_meta检索日期,它检索当前帖子的作者的数据。正确的函数是get_user_meta。因此,上面编写的更新后的代码工作正常。

下面是在我的一个项目中实现的相同代码的屏幕截图。

函数文件:

更新配置文件之前

更新配置文件后

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51351914

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档