您需要做两件事。
- 注册字段
- 保存字段
注意:以下示例仅适用于administrator
用户角色。
1.注册字段
对于“ 添加新用户”使用操作user_new_form
对于用户个人资料使用操作show_user_profile
,edit_user_profile
注册字段摘录:
/**
* Add fields to user profile screen, add new user screen
*/
if( !function_exists('m_register_profile_fields') ) {
// This action for 'Add New User' screen
add_action( 'user_new_form', 'm_register_profile_fields' );
// This actions for 'User Profile' screen
add_action( 'show_user_profile', 'm_register_profile_fields' );
add_action( 'edit_user_profile', 'm_register_profile_fields' );
function m_register_profile_fields( $user ) {
if ( !current_user_can( 'administrator', $user_id ) )
return false;
?>
<h3>Client Portal</h3>
<table class="form-table">
<tr>
<th><label for="dropdown">Portal Category</label></th>
<td>
<input type="text" class="regular-text" name="portal_cat" value="<?php echo esc_attr( get_the_author_meta( 'portal_cat', $user->ID ) ); ?>" id="portal_cat" /><br />
</td>
</tr>
</table>
<?php }
}
2.保存字段
对于“ 添加新用户”使用操作user_register
对于用户个人资料使用操作personal_options_update
,edit_user_profile_update
保存字段摘录:
/**
* Save portal category field to user profile page, add new profile page etc
*/
if( !function_exists('m_register_profile_fields') ) {
// This action for 'Add New User' screen
add_action( 'user_register', 'cp_save_profile_fields' );
// This actions for 'User Profile' screen
add_action( 'personal_options_update', 'cp_save_profile_fields' );
add_action( 'edit_user_profile_update', 'cp_save_profile_fields' );
function cp_save_profile_fields( $user_id ) {
if ( !current_user_can( 'administrator', $user_id ) )
return false;
update_usermeta( $user_id, 'portal_cat', $_POST['portal_cat'] );
}
}
完整的代码段:
/**
* Add fields to user profile screen, add new user screen
*/
if( !function_exists('m_register_profile_fields') ) {
// This action for 'Add New User' screen
add_action( 'user_new_form', 'm_register_profile_fields' );
// This actions for 'User Profile' screen
add_action( 'show_user_profile', 'm_register_profile_fields' );
add_action( 'edit_user_profile', 'm_register_profile_fields' );
function m_register_profile_fields( $user ) {
if ( !current_user_can( 'administrator', $user_id ) )
return false;
?>
<h3>Client Portal</h3>
<table class="form-table">
<tr>
<th><label for="dropdown">Portal Category</label></th>
<td>
<input type="text" class="regular-text" name="portal_cat" value="<?php echo esc_attr( get_the_author_meta( 'portal_cat', $user->ID ) ); ?>" id="portal_cat" /><br />
</td>
</tr>
</table>
<?php }
}
/**
* Save portal category field to user profile page, add new profile page etc
*/
if( !function_exists('m_register_profile_fields') ) {
// This action for 'Add New User' screen
add_action( 'user_register', 'cp_save_profile_fields' );
// This actions for 'User Profile' screen
add_action( 'personal_options_update', 'cp_save_profile_fields' );
add_action( 'edit_user_profile_update', 'cp_save_profile_fields' );
function cp_save_profile_fields( $user_id ) {
if ( !current_user_can( 'administrator', $user_id ) )
return false;
update_usermeta( $user_id, 'portal_cat', $_POST['portal_cat'] );
}
}