[Resolved] Creating user profile view for guest users

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 26 replies, has 2 voices.

Last updated by Nigel 6 hours, 59 minutes ago.

Assigned support staff: Nigel.

Author
Posts
#1289889

Hey Nigel,

Its NULL. I am attaching the screenshot for your reference.

If it would have been added into the database it must show under the post right? Because its not showing anything right now.!!

#1290267

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

I'm not sure why providing the value with $_POST doesn't work, so let's just get the value from the usermeta for the new user and use that to add the postmeta.

You would do so like this, for example:

		// get and post salutation
		$salutation = get_user_meta( $user_id, 'wpcf-salutation', true );
		if (isset($salutation)){
			add_post_meta($post_id, 'wpcf_salutation-i', $salutation, true);
		}
#1290303

Hey Nigel,

This seemed like a good solution, but it is not working all the field in the newly created post is empty.

I have earlier tried using form data to fill post title, and it worked.

But the code is not saving custom fields created on that post type.

#1290397

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

I can't see why it wouldn't work, but I'll need to set up a test site locally using the same code to verify that it does, then we can consider why it might not be working on your site.

I'll do that and then I'll get back to you in a little while.

#1290441

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

OK, I set up a test and spotted a couple of things to get it working.

Let me share the code I used, you'll note the form ID, post type and field slugs are different, otherwise the scenario is the same as yours.

function my_save_data_action($user_id, $form_data)
{
    if ($form_data['id'] == 20) {
 
        $user = get_userdata( $user_id );
 
        $my_post = array(
            'post_title' => $user->data->user_login,
            'post_content' => '',
            'post_status' => 'publish',
            'post_author' => $user_id,
            'post_type' => 'profile',
        );
  
        // Insert the post into the database
        $post_id = wp_insert_post($my_post);

		// get and post custom field
		$field = get_user_meta( $user_id, 'wpcf-capacity', true );

		if (isset($field)){
		    add_post_meta($post_id, 'wpcf-user-capacity', $field, true);
		}
    }
}
add_filter('cred_save_data', 'my_save_data_action', 10, 2);

I noticed that with wp_insert_post the post_content is required, so I included it as an empty string. And the $user object has the user_login inside a data object so I updated how the post title is provided.

Otherwise it worked as expected.

If you make the same changes and it still doesn't work then you'll need to start looking at disabling other plugins and switching theme to see if anything conflicts with the process.

#1290565

Hey Nigel,

I was able to fix this. But there is still one thing that is not going through.

Employee Type is acting very funny. Sometimes it is getting saved other time it isn't. Its a select dropdown option. Here is my code.

/*
 * Create instructor Post with form submission Post Id is User ID and Post Type is Instructor Post Type
 */
add_action('cred_save_data', 'my_save_data_action', 5, 2);

function my_save_data_action($user_id, $form_data)
{
    if ($form_data['id'] == 11) {
        $user = get_userdata( $user_id );
        $my_post = array(
            'post_title' => $user->user_login,
			'post_content' => '',
            'post_status' => 'publish',
            'post_author' => $user_id,
            'post_type' => 'instructor',
        );
		
		$email =  $user->data->user_email;
		
		// Insert the post into the database
        $post_id = wp_insert_post($my_post);
		
		$employeetype = get_user_meta( $user_id, 'wpcf-employee-type', true );
		if (isset($employeetype)){
			add_post_meta($post_id, 'wpcf-employee-type-i', $employeetype, true);
		}
		
		add_post_meta($post_id, 'wpcf-email-i',$email , true);
 
		
			// get and post salutation
			$salutation = get_user_meta( $user_id, 'wpcf-salutation', true );
			if (isset($salutation)){
				add_post_meta($post_id, 'wpcf-salutation-i', $salutation, true);
			}
		
			$firstname = get_user_meta( $user_id, 'wpcf-first-name', true );
			if (isset($firstname)){
				add_post_meta($post_id, 'wpcf-first-name-i', $firstname, true);
			}
		
			$middlename = get_user_meta( $user_id, 'wpcf-middle-name', true );
			if (isset($middlename)){
				add_post_meta($post_id, 'wpcf-middle-name-i', $middlename, true);
			}
		
			$lastname = get_user_meta( $user_id, 'wpcf-last-name', true );
			if (isset($lastname)){
				add_post_meta($post_id, 'wpcf-last-name-i', $lastname, true);
			}
		
			$dob = get_user_meta( $user_id, 'wpcf-date-of-birth', true );
			if (isset($dob)){
				add_post_meta($post_id, 'wpcf-date-of-birth-i', $dob, true);
			}
		
		$blood_group = get_user_meta( $user_id, 'wpcf-blood-group', true );
			if (isset($blood_group)){
				add_post_meta($post_id, 'wpcf-blood-group-i', $blood_group, true);
			}
		
		$mothersname = get_user_meta( $user_id, 'wpcf-mothers-name', true );
			if (isset($mothersname)){
				add_post_meta($post_id, 'wpcf-mothers-name-i', $mothersname, true);
			}
			
				
		$fathersname = get_user_meta( $user_id, 'wpcf-fathers-name', true );
			if (isset($fathersname)){
				add_post_meta($post_id, 'wpcf-fathers-name-i', $fathersname, true);
			}
		
		$gender = get_user_meta( $user_id, 'wpcf-gender', true );
			if (isset($gender)){
				add_post_meta($post_id, 'wpcf-gender-i', $gender, true);
			}
		
		$mobile = get_user_meta( $user_id, 'wpcf-mobile', true );
			if (isset($mobile)){
				add_post_meta($post_id, 'wpcf-mobile-number-i', $mobile, true);
			}
		
		$nationality = get_user_meta( $user_id, 'wpcf-nationality', true );
			if (isset($nationality)){
				add_post_meta($post_id, 'wpcf-nationality-i', $nationality, true);
			}
		
		$religion = get_user_meta( $user_id, 'wpcf-religion', true );
			if (isset($religion)){
				add_post_meta($post_id, 'wpcf-religion-i', $religion, true);
			}
		
		$marital = get_user_meta( $user_id, 'wpcf-marital-status', true );
			if (isset($marital)){
				add_post_meta($post_id, 'wpcf-marital-status-i', $marital, true);
			}
		
		$marriage = get_user_meta( $user_id, 'wpcf-date-of-marriage', true );
			if (isset($marriage)){
				add_post_meta($post_id, 'wpcf-date-of-marriage-i', $marriage, true);
			}
		
		
		$spousename = get_user_meta( $user_id, 'wpcf-spouse-name', true );
			if (isset($spousename)){
				add_post_meta($post_id, 'wpcf-spouse-name-i', $spousename, true);
			}
		
		
		$spousedateofbirth = get_user_meta( $user_id, 'wpcf-spouse-date-of-birth', true);
			if (isset($spousedateofbirth)){
				add_post_meta($post_id, 'wpcf-spouse-date-of-birth-i', $spousedateofbirth , true);
			}
		
		
		$marriage = get_user_meta( $user_id, 'wpcf-date-of-marriage', true );
			if (isset($marriage)){
				add_post_meta($post_id, 'wpcf-date-of-marriage-i', $marriage, true);
			}
		
		
		$aboutme = get_user_meta( $user_id, 'wpcf-about-me', true );
			if (isset($aboutme)){
				add_post_meta($post_id, 'wpcf-about-me-i', $aboutme, true);
			}
		
		
		$dateofjoining = get_user_meta( $user_id, 'wpcf-date-of-joining', true );
			if (isset($dateofjoining)){
				add_post_meta($post_id, 'wpcf-date-of-joining-i', $dateofjoining, true);
			}
		
		
		$employeeimage = get_user_meta( $user_id, 'wpcf-employee-image', true );
			if (isset($employeeimage)){
				add_post_meta($post_id, 'wpcf-employee-image-i', $employeeimage, true);
			}
		
		$levels = get_user_meta( $user_id, 'wpcf-levels', true );
			if (isset($levels)){
				add_post_meta($post_id, 'wpcf-levels-i', $levels, true);
			}
		
		$reportingmanager = get_user_meta( $user_id, 'wpcf-reporting-manager', true );
			if (isset($reportingmanager)){
				add_post_meta($post_id, 'wpcf-reporting-manager-i', $reportingmanager , true);
			}
		
		$dateofjoining = get_user_meta( $user_id, 'wpcf-date-of-joining', true );
			if (isset($dateofjoining)){
				add_post_meta($post_id, 'wpcf-date-of-joining-i', $dateofjoining, true);
			}
		
		$pastaccrediation = get_user_meta( $user_id, 'wpcf-past-accreditations', true );
			if (isset($pastaccrediation)){
				add_post_meta($post_id, 'wpcf-past-accreditations-i', $pastaccrediation, true);
			}
		
		$pastexpnum = get_user_meta( $user_id, 'wpcf-past-experience-number', true );
			if (isset($pastexpnum)){
				add_post_meta($post_id, 'wpcf-past-experience-time-i', $pastexpnum, true);
			}
		
		$pastexpunit = get_user_meta( $user_id, 'wpcf-past-experience-select', true );
			if (isset($pastexpunit)){
				add_post_meta($post_id, 'wpcf-past-experience-unit-i', $pastexpunit, true);
			}
		
		$category = get_user_meta( $user_id, 'wpcf-category', true );
			if (isset($category)){
				add_post_meta($post_id, 'wpcf-category-i', $category, true);
			}
		
		$addressline = get_user_meta( $user_id, 'wpcf-address-line', true );
			if (isset($addressline)){
				add_post_meta($post_id, 'wpcf-address-line-i', $addressline, true);
			}
		
		$city = get_user_meta( $user_id, 'wpcf-city', true );
			if (isset($city)){
				add_post_meta($post_id, 'wpcf-city-i', $city, true);
			}
		
		$state = get_user_meta( $user_id, 'wpcf-state', true );
			if (isset($state)){
				add_post_meta($post_id, 'wpcf-state-i', $state, true);
			}
		
		$country = get_user_meta( $user_id, 'wpcf-country', true );
			if (isset($country)){
				add_post_meta($post_id, 'wpcf-country-i', $country , true);
			}
		
		$currentaddressline = get_user_meta( $user_id, 'wpcf-current-address-line', true );
			if (isset($currentaddressline)){
				add_post_meta($post_id, 'wpcf-current-address-line-i', $currentaddressline , true);
			}
		
		$currentcity = get_user_meta( $user_id, 'wpcf-current-city', true );
			if (isset($currentcity)){
				add_post_meta($post_id, 'wpcf-current-city-i', $currentcity, true);
			}
		
		$currentstate = get_user_meta( $user_id, 'wpcf-current-state', true );
			if (isset($currentstate)){
				add_post_meta($post_id, 'wpcf-current-state-i', $currentstate, true);
			}
		
		$currentcountry = get_user_meta( $user_id, 'wpcf-current-country', true );
			if (isset($currentcountry)){
				add_post_meta($post_id, 'wpcf-current-country-i', $currentcountry, true);
			}
		
		$permanentaddressline = get_user_meta( $user_id, 'wpcf-permanent-address-line', true );
			if (isset($permanentaddressline)){
				add_post_meta($post_id, 'wpcf-permanent-address-line-i', $permanentaddressline, true);
			}
		
		$permanentcity = get_user_meta( $user_id, 'wpcf-permanent-city', true );
			if (isset($permanentcity)){
				add_post_meta($post_id, 'wpcf-permanent-city-i', $permanentcity, true);
			}
		$permanentstate = get_user_meta( $user_id, 'wpcf-permanent-state', true );
			if (isset($permanentstate)){
				add_post_meta($post_id, 'wpcf-permanent-state-i', $permanentstate, true);
			}

		$permanentcountry = get_user_meta( $user_id, 'wpcf-permanent-country', true );
			if (isset($permanentcountry)){
				add_post_meta($post_id, 'wpcf-permanent-country-i', $permanentcountry, true);
			}
		
		  
						
	}
}

I am very unsure how to fix it. Because I know my slug is correct because it records at some tries and stops recording at other.
Please help me with this one field.

#1290575

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Then I would output the employeetype to the debug log to check whether it is available using get_user_meta.

Maybe the user form is sometimes submitted without an employeetype selected?

$employeetype = get_user_meta( $user_id, 'wpcf-employee-type', true );

error_log( "employeetype : " . print_r( $employeetype, true ) );
#1290631

I will check it and let you know.

Maybe the user form is sometimes submitted without an employeetype selected?

Can't happen. It is a required field. And I can see it being recorded every time inside the user view.

#1292327

My issue is resolved now. Thank you!

#1292333

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Did you identify the problem? It would be good to know why it wasn't working as expected.

#1292371

Hey Nigel,

I was not able to identify why it wasn't working.
But I solved it by changing the slug name for the custom post fields to something else.

Thanks!

#1292761

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

OK, well thanks for sharing.