Showing posts with label personal_options_update. Show all posts
Showing posts with label personal_options_update. Show all posts

Friday, November 28, 2014

Wordpress add custom field to Add New User Page

Unfortunally i could not find a plugin for this purpose and could not write a new one because there is not a hook or filter method in Wordpress.  Altough you can easily write a new one for personel options update and user profile update as shown below:

add_action ( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action ( 'edit_user_profile_update', 'my_save_extra_profile_fields' );

Finally, to be able to add a new field to Add New User  page, i decided to modify user-new.php file.

Before table end tag on line 428  i added the code below

 <tr class="form-field form-required">  
  <th scope="row"><label for="foundation"><?php _e('Kurum'); ?> <span class="description"><?php _e('(required)'); ?></span></label></th>  
  <td>  
   <select id="organizationSelection" name="organizationSelection">  
   <?php  
   $organizations = $wpdb->get_results("SELECT id, organization_name FROM organization ORDER BY organization_name ASC");  
   foreach ($organizations as &$organization) {?>  
    <option value="<?php echo $organization->id;?>"><?php echo $organization->organization_name;?></option>  
   <?php }  
   ?>  
   </select>  
  </td>  
  </tr>  

Then i changed the code as the following on line 94

 elseif ( isset($_REQUEST['action']) && 'createuser' == $_REQUEST['action'] ) {  
  check_admin_referer( 'create-user', '_wpnonce_create-user' );  
  if ( ! current_user_can('create_users') )  
  wp_die(__('Cheatin&#8217; uh?'));  
  if ( ! is_multisite() ) {  
  $user_id = edit_user();  
   if ( is_wp_error( $user_id ) ) {  
   $add_user_errors = $user_id;  
  } else {  
   if ( current_user_can( 'list_users' ) ){  
   //@info following line added by ozayd  
   update_user_meta($user_id, 'organization_id', $_POST['organizationSelection']);  
   $redirect = 'users.php?update=add&id=' . $user_id;}  
   else{  
   //@info following line added by ozayd  
   update_user_meta($user_id, 'organization_id', $_POST['organizationSelection']);  
   $redirect = add_query_arg( 'update', 'add', 'user-new.php' );  
   }  
   wp_redirect( $redirect );  
   die();  
  }  
  } else {  
..