Skip Navigation

[Resolved] "Author Page" depending on the role of the user

This support ticket is created 7 years, 5 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 21 replies, has 2 voices.

Last updated by Bochnacki 7 years, 4 months ago.

Assisted by: Minesh.

Author
Posts
#538212
user-profile-error.jpg
user-type.jpg

I have divided users into two groups/role - "customer" and "dealer".
I have created the "author page" and two views - one for the "customer", the other for the "dealer".
I want the "author page" to show the view depending on the role of the user presented on it.
Unfortunately, the link to the "author page" in both cases looks the same:

<a href="<em><u>hidden link</u></em> format="meta" meta='ID']">[wpv-post-author format="meta" meta="nickname"] </a>

There are two forms for registering an account: one for the "customer" and other for the "dealer".
I thought to do a user field "user-type", give it to the form as a hidden field (screen "user-type.jpg"), and then use the shortcode [wpv-conditional] to select the appropriate view:

[wpv-conditional if="( $(user-type) eq '1' )"] View for "customer" [/wpv-conditional]
[wpv-conditional if="( $(user-type) eq '2' )"] View for "dealer" [/wpv-conditional]

The hidden field is saved in the correct form correctly:

<input type="hidden" id="cred_user_form_37_1-hidden-1-1497786325" name="user-type" value="1" class="wpt-form-hidden form-hidden" data-wpt-id="cred_user_form_37_1_cred_user_form_37_1-hidden-1-1497786325" data-wpt-name="user-type" />

However, it is not saved in user settings - screen "user-profile-error.jpg".
Also my shortcode probably is not well done ...

Please help me.

#538421

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - I think you do not need user type field as you can identify the current role.

You can identify current role by two ways.

First option:
- you can use [toolset_access] shortcode. For example:

[toolset_access role="customer" operator="allow"]
    display view for customer
[/toolset_access]

[toolset_access role="dealer" operator="allow"]
    display view for dealer
[/toolset_access]

Second option:
- You can use View's [wpv-current-user] shortcode to get current logged in user information:
More info:
https://toolset.com/documentation/views-shortcodes/#wpv-current-user

Now, if you want to get current user role, you should use following code:

[wpv-current-user info="role"]

As you want to display custom fields based on specific role, you can use [wpv-confitional] shortcode.

For example - to check 'customer" role:

[wpv–conditional if="( '[wpv-current-user info='role']' eq 'customer' )"]
 
Display your custom fields for administrator role
 
[/wpv–conditional]

More info:
https://toolset.com/documentation/views-shortcodes/#wpv-conditional
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

I hope this will help you to resolve your issue. Please feel free to get in touch with me with your queries if any.

#538494

Minesh, thanks for the help, but that's not the point.
The displayed view does not depend on the currently logged-in user, only from the role of the user on the given "author page".
In other words: I can be an admin, a customer or a dealer, or not logged on at all -> I open the "author page" which has the "customer" role and displays a view for "custom", or opens the "author page" It has a "dealer" role and displays a view for "dealer".
In this is a problem - how to do it?

#538499

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

how you build author page - using PHP template file? if yes - could you please share your template file?

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#539598

Forgive the delay on my part.
At the moment I do not want to give access to my website because there are other works and many things are changing.

I'm wondering why the hidden field in the CRED form does not save information in user profile. There is no such possibility, or did I write it wrong?
My code:

[cred_generic_field field='user-type' type='hidden' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":"1"
}
[/cred_generic_field]

Screens in https://toolset.com/forums/topic/author-page-depending-on-the-role-of-the-user/#post-538212

Is my initial idea bad? Can not achieve what I want with shortcodes: [wpv-conditional if="( $(user-type) eq '1' )"] View for "customer" [/wpv-conditional] ?

#539674

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Have you added any CRED hook that will save the value of the generic field? If no - the generic field value will not saved automatically.

You need to use cred_save_data hook to save the generic field value.

More info:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

#539731

Unfortunately I do not know PHP and I guess I did not use it:

add_action('cred_save_data_37', 'my_save_data_user_type',10,2);
function my_save_data_user_type($post_id, $form_data)
{
    if ($form_data['id']==37)
    {
        if (isset($_POST['user_type']))
        {
            add_post_meta($post_id, '__user_type', $_POST['user_type'], true);
        }
    }
}

Can you help me save this well?
"37" is the ID of the CRED form, but there is another form ID = 112 with the field "user-type". How to save it?

#539998

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - if you can share temporary access details and I fix it on your behalf.

Please don't forget to share page URL where you've added CRED form.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#540063

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok, I review the fields - it's hard to manage the checkboxes values on user profile page as it stores the serialize arrays.

What if we convert the "User type" checkboxes field options to dropdown box - is this OK for you?

With User Type dropdown we will display two options Customer or Dealer.

#540069

I do not know why the "user-type" field is supposed to be a drop-down box. Generally, this field is to be invisible in the CRED form, it should be marked automatically depending on which form the user chooses.

Unless you have a better idea how to make different pages for users 🙂
Another for "customer' and another for "dealer".

#540075

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I do not know why the "user-type" field is supposed to be a drop-down box. Generally, this field is to be invisible in the CRED form, it should be marked automatically depending on which form the user chooses.
==> In CRED form the field will remain as it is - there will be no change.

What I'm talking about here is in on user profile page in backend where you added/displaying user-type filed as checkboxes.

In backend - we need to change the field to drop-down - is that OK? If yes - could you please add user-type field as dropdown select in admin on your user profile page and let me know.

#540089

I made the "user-type" field as a drop down list.
In the CRED form, I inserted this field (example for "customer"):

[cred_generic_field field='user-type' type='hidden' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":"1"
}
[/cred_generic_field]

Unfortunately, this field remains unchecked in the user profile.
On the page where they are supposed to show views with the corresponding profile of course now there is nothing. I used this code:

[wpv-conditional if="( '[user-type]' eq '1' )"][wpv-view name="profil-customer"][/wpv-conditional]
[wpv-conditional if="( '[user-type]' eq '2' )"][wpv-view name="profil-dealera"][/wpv-conditional]
#540125

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Great - thanks for updating the user-type field to dropdown select box in admin.

Now - I've added the following code that is responsible to save the CRED user-type field hidden field value:

add_action('cred_save_data_37', 'my_save_data_user_for_customer',10,2);
function my_save_data_user_for_customer($post_id, $form_data){
    // if a specific form
    if ($form_data['id']==37){
            update_user_meta($post_id, 'wpcf-user-type', $_POST['user-type']);
    }
}

add_action('cred_save_data_112', 'my_save_data_user_for_dealer',10,2);
function my_save_data_user_for_dealer($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==112){
            update_user_meta($post_id, 'wpcf-user-type', $_POST['user-type']);
    }
}

Now - if you will create customer it will save with user-type=1 and if you create dealer it will save user-type=2.

Please create new users to test it.

Now - To access the user filed you can use shortcode: [wpv-user]

For example - you should use following code to check user-type field:

[wpv-conditional if="( '[wpv-user field="wpcf-user-type"]' eq '1' )"]
[wpv-view name="profil-customer"]
[/wpv-conditional]

[wpv-conditional if="( '[wpv-user field="wpcf-user-type"]' eq '2' )"]
[wpv-view name="profil-dealera"]
[/wpv-conditional]

More info:
=> https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-user

I'm glad to help you and please let me know if works find for you.

#540135

Your code works. The new customer in the "User type" field is marked "customer" and the dealer has a "dealer" marked.
However, the shortcode to insert the appropriate view does not work.
Interestingly, the Firefox page with the user view is empty, while Chrome is old, so "customer" and "dealer" have a view for "dealer".

#540137

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Glad to know that the user-type field save issue is resolved now.

Now - Could you please share problem URL where above conditional shortcode not working.

Also - I want to confirm that if person logged in as customer user it will display customer view and if person logged in as a dealer then it will display dealer view - correct?