你需要做两件事。
注册字段保存字段
Note: Below example works only for administrator
user role.
1。注册字段Add New User 使用操作user_new_form
对于User Profile 使用操作show_user_profile
, edit_user_profile
Register fields Snippet:
/**
* 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。保存字段
Add New User 使用操作
user_register
对于User Profile 使用操作personal_options_update
, edit_user_profile_update
Save fields Snippet:
/**
* 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\'] );
}
}