dragunoff

[WP] wp-signup.php custom profile meta

Mar 15th, 2011
1,082
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.45 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Add additional custom fields to profile page
  5.  */
  6.  
  7. add_action ( 'show_user_profile', 'my_show_extra_profile_fields' );
  8. add_action ( 'edit_user_profile', 'my_show_extra_profile_fields' );
  9.  
  10. function my_show_extra_profile_fields ( $profileuser ) {
  11. ?>
  12.  
  13.     <h3>Extra profile information</h3>
  14.     <table class="form-table">
  15.         <tr>
  16.             <th>E-mail updates</th>
  17.             <td>
  18.                 <label for="e_mail_updates"><input type="checkbox" name="e_mail_updates" id="e_mail_updates" <?php checked('true', $profileuser->e_mail_updates); ?> value="true" /> I would like to receive e-mail updates</label>
  19.             </td>
  20.         </tr>
  21.     </table>
  22.  
  23. <?php
  24. }
  25.  
  26. /**
  27.  * Save data input from custom field on profile page
  28.  */
  29.  
  30. add_action ( 'personal_options_update', 'my_save_extra_profile_fields' );
  31. add_action ( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
  32.  
  33. function my_save_extra_profile_fields( $user_id ) {
  34.  
  35.     if ( !current_user_can( 'edit_user', $user_id ) )
  36.         return false;      
  37.        
  38.     /* Copy and paste this line for additional fields. Make sure to change 'e_mail_updates' to the field ID. */
  39.     update_usermeta( $user_id, 'e_mail_updates', $_POST['e_mail_updates'] );
  40.    
  41. }
  42.  
  43.  
  44. /**
  45.  * Add cutom field to WPMU registration form
  46.  */
  47. add_action('signup_extra_fields','custom_signup_extra_fields', $errors);
  48.  
  49. // this in an example with a text input
  50. function custom_signup_extra_fields($errors) {
  51. ?>
  52.  
  53.     <label for="e_mail_updates"><input type="checkbox" name="e_mail_updates" id="e_mail_updates" <?php if(isset($_POST['e_mail_updates'])) checked('true', $_POST['e_mail_updates']); ?> value="true" /> I would like to receive e-mail updates
  54.     </label>
  55.     <br />
  56.  
  57. <?php
  58. }
  59.  
  60.  
  61. /**
  62.  * Save custom field input to wp_signups table
  63.  */
  64. add_filter( 'add_signup_meta', 'custom_add_signup_meta' );
  65.  
  66. function custom_add_signup_meta ( $meta = array() ) {
  67.    
  68.     // create an array of custom meta fields
  69.     $meta['custom_usermeta'] = array(
  70.         // 'meta_key' => $_POST['input_field'],
  71.         'e_mail_updates' => $_POST['e_mail_updates']
  72.         );
  73.  
  74.     return $meta;
  75.  
  76. }
  77.  
  78.  
  79. /**
  80.  * Set new usermeta upon new user activation
  81.  */
  82. add_action( 'wpmu_activate_user', 'custom_add_new_user_meta', 10, 3 );
  83.  
  84. function custom_add_new_user_meta( $user_id, $email, $meta ) {
  85.  
  86.     if ( $meta['custom_usermeta'] ) {
  87.         // loop through array of custom meta fields
  88.         foreach ( $meta['custom_usermeta'] as $key => $value ) {
  89.             // and set each one as a meta field
  90.             update_user_meta( $user_id, $key, $value );
  91.         }
  92.     }
  93.    
  94. }
Advertisement
Add Comment
Please, Sign In to add comment