Let a user edit his existing WordPress Profile directly from the FrontEnd through a CPT Edit/Create CRED form.
Note that this function assumes a setup with the correct CPT’s, custom fields, user CF’s.
The Process of this example CRED Form will create a CPT “user Profile” entry , which can be used for front-end displaying user profiles (with a content template as example).
During this, it will get the CPT field values and update the USER fields with it.
This example is also useful to see how checkboxes (multi-options) are different then “normal” CF’s.
function user_save_data_action($post_id, $form_data) {
if ($form_data['id']==ID) {
//get curretn user's ID
$user_id = get_current_user_id();
//get Skype post_meta field value he submits in post
//note: any other SINGLE option Field applies to below "rule")
$skype = get_post_meta( $post_id, 'wpcf-skype', true );
//update the user_meta field with this value
update_user_meta($user_id, 'wpcf-skype', $skype);
//repeats as above
//again, ANY single value Field can be used (email, URL, etc)
$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);
//now the tricky part, we are handling with CheckBoxes, multiple Options!
//AGAIN, THANK YOU +Ana Couto !!!!
//Check if actually the post_meta Checkboxes field has value(s)
if ( isset($_POST['wpcf-languages'])){
//if yes, get all the user meta fields and custom fields information (of user + post)
$langs_user = get_option( 'wpcf-usermeta' );//users fields
$langs_fields = get_option( 'wpcf-fields' );//posts fields
//refine this, we only want specific fields, actually the "languages" and "support-languages" fields
//being those the checkboxes fields (with options), once for user, once for post
$langs_user_options = $langs_user['support-languages']['data']['options'];//user field
$langs_fields_options = $langs_fields['languages']['data']['options'];//post field
//check the field's values against each other (user_meta vs post_meta field values)
$langs_selected = $_POST['wpcf-languages'];//get values from post field (all of them)
//define the data with which we are going to update user_meta field
$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_meta field
update_user_meta($user_id, 'wpcf-support-languages', $user_langs);
}
//Now it get's easy again :)
//Update post_name (slug) with user_name (author)
//we don't want author to enter a title for his profile, this should be done automatically
$custom_title = wp_get_current_user();//get user, define where to get info from for new title
$new_title = $custom_title->user_login;//get login name from user
//collect data and define new title
$my_post = array(
'ID' => $post_id,
'post_name' => $new_title,
'post_title' => $new_title,//we NEED to do that, otherwise, AUTO DRAFT will be post name
);
// Update the post into the database
wp_update_post( $my_post );
}
}
add_action('cred_save_data', 'user_save_data_action',10,2);