
有时候我们用wordpress,想给用户存个电话在用户表单,或者是存用户的生日等,方便我们后期开发各种东西,这就需要添加自定义用户字段。从wordpress上找到了一段代码,特别好用。
效果如下:
代码如下:
<?php /** * The field on the editing screens. * * @param $user WP_User user object */ function wporg_usermeta_form_field_birthday( $user ) { ?> <h3>It's Your Birthday</h3> <table class="form-table"> <tr> <th> <label for="birthday">Birthday</label> </th> <td> <input type="date" class="regular-text ltr" id="birthday" name="birthday" value="<?= esc_attr( get_user_meta( $user->ID, 'birthday', true ) ) ?>" title="Please use YYYY-MM-DD as the date format." pattern="(19[0-9][0-9]|20[0-9][0-9])-(1[0-2]|0[1-9])-(3[01]|[21][0-9]|0[1-9])" required> <p class="description"> Please enter your birthday date. </p> </td> </tr> </table> <?php } /** * The save action. * * @param $user_id int the ID of the current user. * * @return bool Meta ID if the key didn't exist, true on successful update, false on failure. */ function wporg_usermeta_form_field_birthday_update( $user_id ) { // check that the current user have the capability to edit the $user_id if ( ! current_user_can( 'edit_user', $user_id ) ) { return false; } // create/update user meta for the $user_id return update_user_meta( $user_id, 'birthday', $_POST['birthday'] ); } // Add the field to user's own profile editing screen.将该字段添加到用户自己的资料编辑界面。 add_action( 'show_user_profile', 'wporg_usermeta_form_field_birthday' ); // Add the field to user profile editing screen.将该字段添加到用户资料编辑界面。//管理员编辑其他用户 add_action( 'edit_user_profile', 'wporg_usermeta_form_field_birthday' ); // Add the save action to user's own profile editing screen update.在用户自己的资料编辑屏幕上增加保存动作的更新。 add_action( 'personal_options_update', 'wporg_usermeta_form_field_birthday_update' ); // Add the save action to user profile editing screen update.在用户资料编辑界面更新中添加保存动作。//管理员编辑其他用户 add_action( 'edit_user_profile_update', 'wporg_usermeta_form_field_birthday_update' );
还没有评论,来说两句吧...