Advertisement
bedas

User Profile CRED

Jun 18th, 2015
756
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.54 KB | None | 0 0
  1.  /**
  2.   *Update a User Profile and create a Custom Post "Profile" with one CRED form
  3.   */
  4. function user_save_data_action($post_id, $form_data) {
  5.  
  6. //change your CRED form ID accordingly
  7. if ($form_data['id']==210) {
  8.  
  9. //get the current user's ID
  10. $user_id = get_current_user_id();
  11.  
  12. //get skype field value the user submits in post type "profile" CRED form
  13. $skype = get_post_meta( $post_id, 'wpcf-skype', true );
  14.  
  15. //udate the user field with this value
  16. update_user_meta($user_id, 'wpcf-skype', $skype);
  17.  
  18. //repeats as above for each Custom Field/User Field
  19. $phone = get_post_meta( $post_id, 'wpcf-phone-number', true );
  20. update_user_meta($user_id, 'wpcf-phone', $phone);
  21. $pic = get_post_meta( $post_id, 'wpcf-profile-photo', true );
  22. update_user_meta($user_id, 'wpcf-profile-image', $pic);
  23.  
  24.  
  25. //Below is a case of Checkboxes. Checkboxes can have more then one value, and it is not as easy as for single value fields
  26. //Custom field is wpcf-languages
  27. //check if the field has values
  28. if ( isset($_POST['wpcf-languages'])){
  29. //get user meta from database
  30. $langs_user = get_option( 'wpcf-usermeta' );
  31. //define which field from user meta to get
  32. $langs_fields = get_option( 'wpcf-fields' );
  33. $langs_user_options = $langs_user['support-languages']['data']['options'];
  34. $langs_fields_options = $langs_fields['languages']['data']['options'];
  35. //define pendant the Post Custom Field
  36. $langs_selected = $_POST['wpcf-languages'];
  37.  
  38.  
  39. $user_langs = array();
  40. foreach ($langs_selected as $fields_option_key) {
  41. if (isset($langs_fields_options[$fields_option_key])) {
  42. $value = $langs_fields_options[$fields_option_key]['set_value'];
  43. foreach ($langs_user_options as $user_option_key => $user_option) {
  44. if ($user_option['set_value'] == $value) {
  45. $user_langs[$user_option_key] = array($value);
  46. break;
  47. }
  48. }
  49. }
  50. }
  51. //Update the User Field with value form Post Custom field
  52. update_user_meta($user_id, 'wpcf-support-languages', $user_langs);
  53. }
  54.  
  55. //below another example for Custom Field with multiple values, custom field is wpcf-badges
  56. //Again, the same Custom User Field with same values needs to be set up as well  
  57.     if ( isset($_POST['wpcf-badges'])){
  58.         $langs_user = get_option( 'wpcf-usermeta' );
  59.         $langs_fields = get_option( 'wpcf-fields' );
  60.         $langs_user_options = $langs_user['badges-user']['data']['options'];
  61.         $langs_fields_options = $langs_fields['badges']['data']['options'];
  62.         $langs_selected = $_POST['wpcf-badges'];
  63.  
  64.         $user_langs = array();
  65.         foreach ($langs_selected as $fields_option_key) {
  66.             if (isset($langs_fields_options[$fields_option_key])) {
  67.                 $value = $langs_fields_options[$fields_option_key]['set_value'];
  68.                 foreach ($langs_user_options as $user_option_key => $user_option) {
  69.                     if ($user_option['set_value'] == $value) {
  70.                         $user_langs[$user_option_key] = array($value);
  71.                         break;
  72.                     }
  73.                 }
  74.             }
  75.         }
  76.         update_user_meta($user_id, 'wpcf-badges-user', $user_langs);
  77.     }
  78.  
  79. //Now, we want the user profile Post to have the slug (name) and title of the user who created it
  80. //Update slug with user_name
  81. $custom_title = wp_get_current_user();
  82. $new_title = $custom_title->user_login;
  83.  
  84. //collect data and define new title
  85. $my_post = array(
  86. 'ID'               => $post_id,
  87. 'post_name' => $new_title,
  88. 'post_title'=> $new_title,
  89.  
  90. );
  91.  
  92. // Update the post into the database
  93. wp_update_post( $my_post );
  94.  
  95. }
  96. }
  97. add_action('cred_save_data', 'user_save_data_action',10,2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement