Advertisement
bedas

Update User Meta Fields with CRED from front end

Jun 4th, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.62 KB | None | 0 0
  1. function user_save_data_action($post_id, $form_data) {
  2.        
  3.         if ($form_data['id']==ID) {
  4.            
  5.             //get curretn user's ID
  6.             $user_id = get_current_user_id();
  7.  
  8.             //get Skype post_meta field value he submits in post
  9.             //note: any other SINGLE option Field applies to below "rule")
  10.             $skype = get_post_meta( $post_id, 'wpcf-skype', true );
  11.             //update the user_meta field with this value
  12.             update_user_meta($user_id, 'wpcf-skype', $skype);
  13.  
  14.             //repeats as above
  15.             //again, ANY single value Field can be used (email, URL, etc)
  16.             $phone = get_post_meta( $post_id, 'wpcf-phone-number', true );
  17.             update_user_meta($user_id, 'wpcf-phone', $phone);
  18.             $pic = get_post_meta( $post_id, 'wpcf-profile-photo', true );
  19.             update_user_meta($user_id, 'wpcf-profile-image', $pic);
  20.            
  21.         //now the tricky part, we are handling with CheckBoxes, multiple Options!
  22.  
  23.         //Check if actually the post_meta Checkboxes field has value(s)
  24.             if ( isset($_POST['wpcf-languages'])){
  25.                 //if yes, get all the user meta fields and custom fields information (of user + post)
  26.                 $langs_user = get_option( 'wpcf-usermeta' );//users fields
  27.                 $langs_fields = get_option( 'wpcf-fields' );//posts fields
  28.         //refine this, we only want specific fields, actually the "languages" and "support-languages" fields
  29.         //being those the checkboxes fields (with options), once for user, once for post
  30.                 $langs_user_options = $langs_user['support-languages']['data']['options'];//user field
  31.                 $langs_fields_options = $langs_fields['languages']['data']['options'];//post field
  32.  
  33.         //check the field's values against each other (user_meta vs post_meta field values)
  34.                 $langs_selected = $_POST['wpcf-languages'];//get values from post field (all of them)
  35.  
  36.         //define the data with which we are going to update user_meta field
  37.                 $user_langs = array();
  38.                     foreach ($langs_selected as $fields_option_key) {
  39.                         if (isset($langs_fields_options[$fields_option_key])) {
  40.                             $value = $langs_fields_options[$fields_option_key]['set_value'];
  41.                             foreach ($langs_user_options as $user_option_key => $user_option) {
  42.                                 if ($user_option['set_value'] == $value) {
  43.                                     $user_langs[$user_option_key] = array($value);
  44.                                     break;
  45.                                 }
  46.                             }
  47.                         }
  48.                     }
  49.  
  50.         //update the user_meta field
  51.                 update_user_meta($user_id, 'wpcf-support-languages', $user_langs);
  52.             }
  53.            
  54.             //Update post_name (slug) with user_name (author)
  55.         //we don't want author to enter a title for his profile, this should be done automatically
  56.             $custom_title = wp_get_current_user();//get user, define where to get info from for new title
  57.             $new_title = $custom_title->user_login;//get login name from user
  58.  
  59.             //collect data and define new title
  60.             $my_post = array(
  61.                     'ID'        => $post_id,
  62.                     'post_name'     => $new_title,
  63.                     'post_title'    => $new_title,//we NEED to do that, otherwise, AUTO DRAFT will be post name
  64.  
  65.             );
  66.  
  67.             // Update the post into the database
  68.             wp_update_post( $my_post );
  69.  
  70.         }
  71.     }
  72. add_action('cred_save_data', 'user_save_data_action',10,2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement