Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Wordpress在自定义字段中注册日期,但是否可以编辑?_Php_Wordpress_Field - Fatal编程技术网

Php Wordpress在自定义字段中注册日期,但是否可以编辑?

Php Wordpress在自定义字段中注册日期,但是否可以编辑?,php,wordpress,field,Php,Wordpress,Field,我试图在用户配置文件中将自定义字段添加到wordpress的后端。我想让它注册日期,然后让它可以编辑字段并覆盖它 我现在让它拉注册日期,但需要一种方法,使它只是最初拉注册日期值,但如果修改会覆盖 add_action( 'show_user_profile', 'extra_user_profile_fields' ); add_action( 'edit_user_profile', 'extra_user_profile_fields' ); function extra_user_pro

我试图在用户配置文件中将自定义字段添加到wordpress的后端。我想让它注册日期,然后让它可以编辑字段并覆盖它

我现在让它拉注册日期,但需要一种方法,使它只是最初拉注册日期值,但如果修改会覆盖

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 }
旧的坏代码

更新的工作代码

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

即使有了这个修正,你仍然看不到正确的注册日期,因为我们为它调用了错误的函数。我们使用get_the_author_meta来检索日期,它检索当前文章作者的数据。正确的函数是get\u user\u meta。因此,上面编写的更新代码工作正常

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

函数文件:

更新配置文件之前

更新配置文件后

但我如何用注册的数据填充字段,然后才能对其进行编辑?@jord49您在show_user_profile和edit_user_profile的action callback中编写的代码应该可以为您获取所需的数据,并且可以对其进行编辑。配置文件更新钩子编辑\用户\配置文件\更新将在$POST变量中为您提供所有提交的数据。@jord49请检查更新的代码,并对钩子的错误表示歉意。仍然是坏男人:当您加载页面时,该代码显示一个占位符1970年的日期,当您单击“更新”后,它将获得日期,如果您更改了它,则不会保存它。@jord49您是否更改了代码中有关日期显示功能或日期保存功能的任何内容?我添加了0个屏幕截图,显示完全相同的代码正常工作。
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 );
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 );