Sie müssen 2 Dinge tun.
- Felder registrieren
- Felder speichern
Hinweis: Das folgende Beispiel funktioniert nur für die administrator
Benutzerrolle.
1. Felder registrieren
Für Add New User Verwendung Aktionuser_new_form
Verwenden Sie für Benutzerprofile Aktionen show_user_profile
,edit_user_profile
Felder registrieren
/**
* 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. Felder speichern
Für Add New User Verwendung Aktionuser_register
Verwenden Sie für Benutzerprofile Aktionen personal_options_update
,edit_user_profile_update
Felder speichern 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'] );
}
}
Vollständiges Code-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 }
}
/**
* 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'] );
}
}