Vous devez faire 2 choses.
- Enregistrer les champs
- Enregistrer les champs
Remarque: l' exemple ci - dessous ne fonctionne que pour administrator
le rôle d'utilisateur.
1. Enregistrer les champs
Pour Ajouter un nouvel utilisateur action utilisationuser_new_form
Pour les actions d' utilisation du profil utilisateur show_user_profile
,edit_user_profile
Enregistrer les extraits de champs:
/**
* 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. Enregistrer les champs
Pour Ajouter un nouvel utilisateur action utilisationuser_register
Pour les actions d' utilisation du profil utilisateur personal_options_update
,edit_user_profile_update
Enregistrer les extraits de champs:
/**
* 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'] );
}
}
Extrait de code complet:
/**
* 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'] );
}
}