Anda perlu melakukan 2 hal.
- Daftarkan bidang
- Simpan bidang
Catatan: Contoh di bawah ini hanya berfungsi untuk administrator
peran pengguna.
1. Daftarkan bidang
Untuk tindakan Tambahkan Pengguna Baru gunakanuser_new_form
Untuk tindakan penggunaan Profil Pengguna show_user_profile
,edit_user_profile
Daftarkan bidang Cuplikan:
/**
* 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. Simpan bidang
Untuk tindakan Tambahkan Pengguna Baru gunakanuser_register
Untuk tindakan penggunaan Profil Pengguna personal_options_update
,edit_user_profile_update
Simpan cuplikan bidang:
/**
* 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'] );
}
}
Cuplikan Kode Lengkap:
/**
* 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'] );
}
}