Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *Update a User Profile and create a Custom Post "Profile" with one CRED form
- */
- function user_save_data_action($post_id, $form_data) {
- //change your CRED form ID accordingly
- if ($form_data['id']==210) {
- //get the current user's ID
- $user_id = get_current_user_id();
- //get skype field value the user submits in post type "profile" CRED form
- $skype = get_post_meta( $post_id, 'wpcf-skype', true );
- //udate the user field with this value
- update_user_meta($user_id, 'wpcf-skype', $skype);
- //repeats as above for each Custom Field/User Field
- $phone = get_post_meta( $post_id, 'wpcf-phone-number', true );
- update_user_meta($user_id, 'wpcf-phone', $phone);
- $pic = get_post_meta( $post_id, 'wpcf-profile-photo', true );
- update_user_meta($user_id, 'wpcf-profile-image', $pic);
- //Below is a case of Checkboxes. Checkboxes can have more then one value, and it is not as easy as for single value fields
- //Custom field is wpcf-languages
- //check if the field has values
- if ( isset($_POST['wpcf-languages'])){
- //get user meta from database
- $langs_user = get_option( 'wpcf-usermeta' );
- //define which field from user meta to get
- $langs_fields = get_option( 'wpcf-fields' );
- $langs_user_options = $langs_user['support-languages']['data']['options'];
- $langs_fields_options = $langs_fields['languages']['data']['options'];
- //define pendant the Post Custom Field
- $langs_selected = $_POST['wpcf-languages'];
- $user_langs = array();
- foreach ($langs_selected as $fields_option_key) {
- if (isset($langs_fields_options[$fields_option_key])) {
- $value = $langs_fields_options[$fields_option_key]['set_value'];
- foreach ($langs_user_options as $user_option_key => $user_option) {
- if ($user_option['set_value'] == $value) {
- $user_langs[$user_option_key] = array($value);
- break;
- }
- }
- }
- }
- //Update the User Field with value form Post Custom field
- update_user_meta($user_id, 'wpcf-support-languages', $user_langs);
- }
- //below another example for Custom Field with multiple values, custom field is wpcf-badges
- //Again, the same Custom User Field with same values needs to be set up as well
- if ( isset($_POST['wpcf-badges'])){
- $langs_user = get_option( 'wpcf-usermeta' );
- $langs_fields = get_option( 'wpcf-fields' );
- $langs_user_options = $langs_user['badges-user']['data']['options'];
- $langs_fields_options = $langs_fields['badges']['data']['options'];
- $langs_selected = $_POST['wpcf-badges'];
- $user_langs = array();
- foreach ($langs_selected as $fields_option_key) {
- if (isset($langs_fields_options[$fields_option_key])) {
- $value = $langs_fields_options[$fields_option_key]['set_value'];
- foreach ($langs_user_options as $user_option_key => $user_option) {
- if ($user_option['set_value'] == $value) {
- $user_langs[$user_option_key] = array($value);
- break;
- }
- }
- }
- }
- update_user_meta($user_id, 'wpcf-badges-user', $user_langs);
- }
- //Now, we want the user profile Post to have the slug (name) and title of the user who created it
- //Update slug with user_name
- $custom_title = wp_get_current_user();
- $new_title = $custom_title->user_login;
- //collect data and define new title
- $my_post = array(
- 'ID' => $post_id,
- 'post_name' => $new_title,
- 'post_title'=> $new_title,
- );
- // Update the post into the database
- wp_update_post( $my_post );
- }
- }
- add_action('cred_save_data', 'user_save_data_action',10,2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement