Skip Navigation

[Resolved] How to display conditional content based on custom user field

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to display conditional content based on custom user field

Solution:
In this specific case, we use custom shortcode with [wpv-conditional] shortcode to compare the user custom field value.

You can find proposed solution, in this case, with the following reply:
=> https://toolset.com/forums/topic/combining-apostrophe-and-quotes/#post-691169

Relevant Documentation:

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

Tagged: 

This topic contains 12 replies, has 2 voices.

Last updated by Tiit Sau 5 years, 11 months ago.

Assisted by: Minesh.

Author
Posts
#656003

How to get user id work inside this conditional?

[wpv-conditional if="( '[types usermeta='representing-a-company' user_id="[wpv-search-term param='user_id']" output='raw'][/types]' eq '1' )"]
#656511

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Could you please tell me what value you want to check conditionally?

As I understand from your code that you've URL param which is assigned as user_id but what you want to check conditionally and whats not working for you.

#656527
syntax error.png

Looks like syntax problem with usage of quotes. Take a look of editor error colors on the picture.
Editor does not like thouse apostrophe inside quotes. Not printing debug information.
I want to check usermeta 'representing-a-company' and it works, if to set user id like this:

[wpv-conditional if="( '[types usermeta='representing-a-company' user_id='1463' output='raw'][/types]' eq '1' )" ]
#656782

Minesh
Supporter

Languages: English (English )

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

Well - I just tried same kind of shortcode on my local test install and I see that its working fine:

[wpv-conditional if="( '[types usermeta='user-tmp-phone' user_id="[wpv-search-term param='user_id']" output='raw'][/types]' eq '1' )" debug="true"]

working 

[/wpv-conditional]

If your concern is regarding the red line - it maybe due to nested shortcodes and the words that do not understand by editor. So there is no harm using the way you are using now.

#656846

Does not work for me. By the way shortcode is inside CRED user editing form.

#656850

Minesh
Supporter

Languages: English (English )

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

That was the difference I check with post editor but again I checked with CRED edit form and for me its working. Maybe its issue or nested shortcode you are using.

Can I have problem URL and access details so I can check whats going wrong there?

#657948

Minesh
Supporter

Languages: English (English )

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

Ok - the thing is that I need to check the same code on CRED edit user form and I will try to reproduce the issue. I will check this on next week.

The thing is that if I assign static user_id="1463" - the code works but if you use nested shortcode with [wpv-search-term] and even I tried to use custom shortcode:

function func_get_url_user_id() {
	if(isset($_GET['user_id'])){
		return $_GET['user_id'];
	}
	return 0;
}
add_shortcode('get_url_user_id', 'func_get_url_user_id');

For example:

[wpv-conditional if="( '[types usermeta='representing-a-company' user_id='[get_url_user_id]' output='raw'][/types]' eq '1' )" debug="true"]

But there is no success. I will try to debug this issue further and update you.

In the mean time - is it possible for you to share the duplicator copy?

#691169

Minesh
Supporter

Languages: English (English )

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

I just checked with user edit form on my localhost and tried to reproduce the issue but I could not able to reproduce it.

So, I have find a workaround for you. I've added following code to your functions.php file:

function func_get_url_user_id() {
	if(isset($_GET['user_id']) and is_numeric($_GET['user_id'])){
		$is_company = get_user_meta($_GET['user_id'],'wpcf-representing-a-company',true);
		
		if($is_company == 1){
			return 1;
		}else{
			return 0;	
		}
	}
	return 0;
}
add_shortcode('get_url_user_id', 'func_get_url_user_id');

And I've added the condition to CRED form as given under:

[wpv-conditional if="( '[get_url_user_id]' eq '1' )"]
  you are in
  [/wpv-conditional]

And I can see the conditional code is working just fine now.

#691194

Are you still editing or just logged in.
I can't access CRED forms to edit.

#691195

Minesh
Supporter

Languages: English (English )

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

I was just logged in - I just logged out now.

#691846

Your PHP code works well and thanks for that!
But the present case is just small one and I do not want you writing me php on a daily basis.
Did you find out why this nested shortcode works on your server and not on mine?
Main question - is it server based or my software setup issue?

#691848

Minesh
Supporter

Languages: English (English )

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

Well - I dont able to reproduce the issue on my clean test install and nested shortcode used to work fine for me. I dont know why its not working on your install.

However - in this case it's not a nested shortcode but you are passing shortcode as attribute. So the solution I shared is better option.

#691986

Thanks again!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.