Skip Navigation

[Resolved] Creating user profile view for guest users

This support ticket is created 5 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

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.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 26 replies, has 2 voices.

Last updated by Nigel 5 years, 6 months ago.

Assisted by: Nigel.

Author
Posts
#1287517

Tell us what you are trying to do?
I have user cred form, where I am collecting information about them. I am also mimicing author with post to establish post relationships. Now I want to display the user profile to guests with a link like https:www.mywebsite.com/instructor/username.

Can I achieve this using toolset?

Is there any documentation that you are following?

Is there a similar example that we can see?

What is the link to your site?

#1287519

Nigel
Supporter

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

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

Hi there

Where do you want to display the link, what is the context?

It will be possible to generate such a link, but how will we know for which user the link is being created?

#1287521

Hey, Nigel

Where do you want to display the link, what is the context?
I have a logged in view for instructor where I want to add this link which will allow them to share with someone. If anyone visits that link they can see his profile.

It will be possible to generate such a link, but how will we know for which user the link is being created?
We can use the username(unique) to have unique link and generate the result according to that. As it is normally done in social media websites.

#1287527

Nigel
Supporter

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

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

If it is for the currently logged-in user to share a link to their profile then you can generate the URL using Views shortcodes like so:

[wpv-bloginfo show="url"]/instructor/[wpv-current-user info="login"]

You may want to add it as a link element with the generated URL used for the href attribute and/or the link text itself.

#1287531

Hey, this will generate the link for the profile. But how do I create the page that link would render? How to make that template which would take me to [wpv-bloginfo show="url"]/instructor/[wpv-current-user info="login"]

#1287651

Nigel
Supporter

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

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

Sorry, from your question it sounded like that is what you had already set up and it was just a question of generating the link in that format.

It sounds like you have created a custom post type for instructor profiles. You would store the profile data for the user in custom post fields (rather than user fields). The user would be author of that instructor profile post. Then you create a content template to display the instructor posts as you would any other type of post, including the custom fields as required.

So site.com/instructor/john-smith would display the profile titled "John Smith" (with a slug of john-smith) using the above-mentioned template.

Your users register and then use a post form to create this instructor profile post from the front-end. You could pre-populate the post title with the value of the user's display name or username.

#1287769

Okay great, so rather than saving in user field I should save it in post, and then use post form to edit the same, right.

One more question, how do I get the username field from cred user form,

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    if ($form_data['id']==11)
    {
		$my_post = array(
		  'post_title'    => username,
		  'post_status'   => 'publish',
		  'post_author'   => $post_id,
		  'post_type' => 'instructor',
		);
		// Insert the post into the database
		wp_insert_post( $my_post );
    }
}

I am able to get custom values but I am not able to get username. Can you help me out here.

#1287813

Nigel
Supporter

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

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

You can get it from the global $_POST object, i.e. with

global $_POST;
$username = $_POST['user_login'];
#1289389

Hey Nigel, it is not working as intended. It doesn't throw anything and my post is created with (no name). My Modified code looks like this:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)							 
{
	global $_POST;
	$username = $_POST['user_login'];
    if ($form_data['id']==11)
    {
		$my_post = array(
		   
		  'post_title'    => $username,
		  'post_status'   => 'publish',
		  'post_author'   => $post_id,
		  'wpcf_salutation-i' => $_POST['wpcf-salutation'],
		  'post_type' => 'instructor',
		);
		// Insert the post into the database
		wp_insert_post( $my_post );
    }
}

Could you please also tell me how do I use custom field slug of post "wpcf_salutation-i" to store the value of user data that is again custom user field "wpcf-salutation". I have multiple field type like image select radio. How can I bind them so that it works perfectly. Thank You!

#1289409

UPDATE

I used this code which I saw here : https://toolset.com/forums/topic/cred-save-data-hook-not-updating-custom-field/#post-552876
My new code looks like this. (still no luck):

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)							 
{
	global $_POST;
	$username = $_POST['user_login'];
    if ($form_data['id']==11)
    {
		$my_post = array(
		  'post_title'    => $username,
		  'post_status'   => 'publish',
		  'post_author'   => $post_id,
		  'post_type' => 'instructor',
			
		);
		if (isset($_POST['wpcf-salutation']))
			{
				// 'wpcf_salutation-i' is the meta key that the value of 'wpcf_salutation' will be added to
				add_post_meta($post_id, 'wpcf_salutation-i', $_POST['wpcf_salutation'], true);
			}
			if (isset($_POST['wpcf-first-name']))
			{
				// 'wpcf_first-name-i' is the meta key that the value of 'wpcf_first-name' will be added to
				add_post_meta($post_id, 'wpcf_first-name-i', $_POST['wpcf_first-name'], true);
			} 
		// Insert the post into the database
		wp_insert_post( $my_post );
    }
}

#1289445

Nigel
Supporter

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

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

What exactly is it that works, and what doesn't?

I can see one obvious problem with your final code sample, namely mixing up the $post_id, as well as adding post meta to a post that has not been inserted yet.

First, let's clear something up about the filter arguments.

In your example we have two arguments, $post_id, and $form_data.

Now these variable names could be anything, we just need to be consistent when using them throughout the rest of the code.

For post forms the first argument is the id of the newly created post.

For user forms the first argument is actually the id of the newly registered user. The code works if we call it $post_id (we could equally call it $banana), but as it is really the user ID let's use $user_id instead.

Because when you insert a new instructor post, you will need the id of that post to be able to add post meta to it (which you are currently doing in the wrong order).

So, the updated code looks like this:

add_action('cred_save_data', 'my_save_data_action', 10, 2);
function my_save_data_action($user_id, $form_data)
{
    global $_POST;

    // for testing, delete when done
    error_log('_POST: ' . print_r($_POST, true));

    if ($form_data['id'] == 11) {
        $my_post = array(
            'post_title' => $_POST['user_login'],
            'post_status' => 'publish',
            'post_author' => $user_id,
            'post_type' => 'instructor',
        );

        // Insert the post into the database
        $post_id = wp_insert_post($my_post);

        if (isset($_POST['wpcf-salutation'])) {
            // 'wpcf_salutation-i' is the meta key that the value of 'wpcf_salutation' will be added to
            add_post_meta($post_id, 'wpcf_salutation-i', $_POST['wpcf_salutation'], true);
        }

        if (isset($_POST['wpcf-first-name'])) {
            // 'wpcf_first-name-i' is the meta key that the value of 'wpcf_first-name' will be added to
            add_post_meta($post_id, 'wpcf_first-name-i', $_POST['wpcf_first-name'], true);
        }
    }
}

Note that I've included a line for debugging which you can later delete when everything works as expected.

It simply sends the global $_POST object to the debug log so that you can inspect it and make sure it contains everything you need/expect and the format. (You could find the value of the fields such as wpcf-salutation are buried inside an array, for example.)

If you haven't already, turn on the debug log by editing your wp-config.php file and change the line with WP_DEBUG like so:

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
define('WP_DISABLE_FATAL_ERROR_HANDLER',true);

That will create a debug.log file in your /wp-content/ directory which you can examine in any text editor.

#1289623

Hi Nigel,

Thanks for the descriptive explanation.
I am still unable to get the the required field inside the post.

Errors experiencing :
1) 'post_title' => $_POST['user_login'], This is still NULL. I am not getting the username of the profile created.
2) The custom fields of the post wpcf_salutation-i, wpcf_first-name-i is not being saving the data from the form..

I have used all the configurations suggested by you. And I got the following dump:

[11-Jul-2019 12:09:46 UTC] _POST: Array
(
    [wpcf-salutation] => Ms.
    [wpcf-first-name] => bhan
    [wpcf-middle-name] => hgjhj
    [wpcf-last-name] => ljkljlj
    [wpcf-date-of-birth] => Array
        (
            [display-only] => July 17, 2019
            [datepicker] => 1563321600
            [datetime] => 2019-07-17
            [hour] => 00
            [minute] => 00
            [timestamp] => 1563321600
        )

    [wpcf-blood-group] => A negative
    [wpcf-fathers-name] => hjkhkh
    [wpcf-mothers-name] => 
    [wpcf-gender] => Male
    [user_email] => sakdlsakj@krenovate.com
    [wpcf-mobile] => 46546+54
    [wpcf-address-line] => jhvghj
    [wpcf-city] => hgk
    [wpcf-state] => Delhi
    [wpcf-country] => India
    [wpcf-current-address-line] => jkgjkb
    [wpcf-current-city] => hjk
    [wpcf-current-state] => Delhi
    [wpcf-current-country] => India
    [wpcf-permanent-address-line] => 
    [wpcf-permanent-city] => 
    [wpcf-permanent-state] => 
    [wpcf-permanent-country] => 
    [wpcf-religion] => kjhjkH
    [wpcf-nationality] => Indian
    [wpcf-marital-status] => no
    [wpcf-about-me] => JKNJHJKH
    [wpcf-date-of-joining] => Array
        (
            [display-only] => July 11, 2019
            [datepicker] => 1562803200
            [datetime] => 2019-07-11
            [hour] => 00
            [minute] => 00
            [timestamp] => 1562803200
        )

    [wpcf-employee-type] => Fresher
    [wpcf-employee-image] => Cambridge-School-Noida-1.png
    [wpcf-levels] => Level 1
    [wpcf-reporting-manager] => HJGKH
    [wpcf-past-accreditations] => KJHJKH
    [wpcf-past-experience-number] => 3
    [wpcf-past-experience-select] => Months
    [wpcf-category] => Crush Club
    [_cred_cred_wpnonce_cred_user_form_11] => 3d54a978b6
    [_cred_cred_prefix_post_id] => -1
    [_cred_cred_prefix_cred_container_id] => 16
    [_cred_cred_prefix_form_id] => 11
    [_cred_cred_prefix_form_count] => 1
    [form_submit] => true
    [action] => cred_submit_form
    [wpnonce] => c74fa75431
)

This shows that the form data is being captured but is not being passed through add_post_meta.

#1289725

Nigel
Supporter

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

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

That $_POST object is missing the user_login.

This is a user registration form, right?

If the user_login is missing, I guess your form settings are to generate the username automatically, which is why it's not present.

So, as an alternative, we know the id of the user just registered, so we can get the username.

You'd want to do this:

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_status' => 'publish',
            'post_author' => $user_id,
            'post_type' => 'instructor',
        );
 
        // Insert the post into the database
        $post_id = wp_insert_post($my_post);
 
        if (isset($_POST['wpcf-salutation'])) {
            // 'wpcf_salutation-i' is the meta key that the value of 'wpcf_salutation' will be added to
            add_post_meta($post_id, 'wpcf_salutation-i', $_POST['wpcf_salutation'], true);
        }
 
        if (isset($_POST['wpcf-first-name'])) {
            // 'wpcf_first-name-i' is the meta key that the value of 'wpcf_first-name' will be added to
            add_post_meta($post_id, 'wpcf_first-name-i', $_POST['wpcf_first-name'], true);
        }
    }
}

Try that?

#1289763

Hey Nigel,

Thanks, I was able to fetch the username as the title of the post. I was indeed using automatically generate username.

I still have no idea why I am unable to use this piece of code:
if (isset($_POST['wpcf-salutation'])) {
// 'wpcf_salutation-i' is the meta key that the value of 'wpcf_salutation' will be added to
add_post_meta($post_id, 'wpcf_salutation-i', $_POST['wpcf_salutation'], true);
}

I have set wpcf_salutation-i as the field slug for that custom post type and also have wpcf_salutation as the user form field.
Is this not the way I should be writing this?

#1289767

Nigel
Supporter

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

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

Double-check in the database directly whether an entry is being made for the newly published post for wpcf-salutation-i etc.

Use phpMyAdmin or similar to check wp_postmeta table for the corresponding post_id and see if there are entries added by your add_post_meta calls.

What do you find?