Skip Navigation

[Résolu] wpv-conditional shortcode using custom user field not working

This support ticket is created Il y a 6 années et 3 mois. 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.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

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

Supporter timezone: America/Jamaica (GMT-05:00)

Ce sujet contient 20 réponses, a 2 voix.

Dernière mise à jour par julieP Il y a 6 années et 3 mois.

Assisté par: Shane.

Auteur
Publications
#1089491

I've tried using this conditional shortcode but nothing is being returned:-

[wpv-conditional if="('[types usermeta='custom-field' user_current='true' output='raw'][/types]' eq '999' )"]

When I add the debug attribute to the shortcode, I get this:-

####################
wpv-conditional attributes
####################
Array
(
    [if] => ('' = '999' )
    [debug] => true
)

####################
Debug information
####################
--------------------
Original expression: ('' = '999' )
--------------------
After replacing 1 general variables and comparing strings: ('' = '999' )
	Comparing  to 999

Has something changed (it has worked for me in the past and I've seen multiple tickets showing this as a solution) or is there a known issue (can't find it in errata)?

(I've double-checked that the custom field exists for the user and that it's populated with the expected value. I've also double checked that I'm testing as the right user!!)

Thanks

#1089544

Shane
Supporter

Les langues: Anglais (English )

Fuseau horaire: America/Jamaica (GMT-05:00)

Hi Juilie,

Yes I can see in the debug as it should work.

Does the custom field itself return a value ? What happens when you add it to a page.
[types usermeta='custom-field' user_current='true' output='raw'][/types]

Please let me know.
Thanks,
Shane

#1090111

Hi Shane

No output even with the types shortcode outside the conditional.

I've tested this again this morning on a minimal test site with the same results. The site's locked down so I can't provide you with access. Please either test this yourself and if you can replicate, please escalate. Alternatively, please enable private fields so I can provide a site dump.

Thanks

#1090271

Shane
Supporter

Les langues: Anglais (English )

Fuseau horaire: America/Jamaica (GMT-05:00)

Hi Juilie,

This works fine on my end.

I've enabled the private fields so that you can provide me with a duplicator package for the site.
https://toolset.com/faq/provide-supporters-copy-site/

Thanks,
Shane

#1091097

I sorted out a clone site for you but then, after playing around a bit more, I discovered that the contents of the custom user field are only displaying if that field was included in the original user registration form (if the custom user field and its value are added to the database any other way, the contents don't show).

Is this behaviour expected?

There is still an issue however for multi site installations. Where a custom user field is populated via a user form on the main site (where users register), the types shortcode for that user field (inside a conditional or on its own) is returning a value when used on the main site but not when used on any other sub site. Views shortcodes (e.g. [wpv-user field="user_login"]) successfully return a value on sub sites so I would expect a Types shortcode to do the same. Please advise.

If you need to see this on a cloned site, please re-enable private field. Thanks

#1091275

Shane
Supporter

Les langues: Anglais (English )

Fuseau horaire: America/Jamaica (GMT-05:00)

Hi Juilie,

What do you mean added to the database any other way ?

Do you mean by manually going to the database and adding or just updating it on the backend? Also if you add it directly to the database, does it show up on the user's profile on the backend?

Please let me know.

Thanks
Shane

#1091839

By 'any other way' I mean either via a cred hook or via SQL query in phpMyAdmin.

I've checked and can see that only custom user fields added via a registration form can be seen in the User's profile in the backend. Is this expected behaviour?

#1093054

Shane
Supporter

Les langues: Anglais (English )

Fuseau horaire: America/Jamaica (GMT-05:00)

Hi Juilie,

Actually no this is not the expected behaviour. Could you send me an example of the hook that you are using ? If the custom field was created in Types and you used the hook to populate it then it should work.

Could you let me know what kind of field is it ?

Thanks,
Shane

#1093689

I haven't used a hook. To test this thoroughly, I created a user custom field ('user-field') and included it in the registration form and I also added custom user field directly to the usermeta table in phpMyAdmin so I have meta_key is wpcf-my-custom-field, meta_value is 999 and user_id is the ID of my test user with Contributor role. So my test user has two custom user fields.

Whilst waiting for your response I created my own shortcode to return the value of wpcf-my-custom-field and if I use this shortcode inside a wpv-conditional shortcode, I get the required results on the main and sub site. The Types shortcode that should do the same thing isn't working.

#1093757

Shane
Supporter

Les langues: Anglais (English )

Fuseau horaire: America/Jamaica (GMT-05:00)

Hi Juilie,

Yes as you indicated this should work fine, however I know that some fields are stored differently. If this is a single line field I see no reason why it should work. However for other fields such as Checkboxes or Select fields I know that these have a special for format when saved to the database.

If it is one of those fields then I know that unless the format is the same then it may not show up at all. Could you let me know what kind of custom field is it ?

Thanks,
Shane

#1093807

It's a single line field

#1094088

Shane
Supporter

Les langues: Anglais (English )

Fuseau horaire: America/Jamaica (GMT-05:00)

Hi Juilie,

I'm not able to replicate the issue as i made a manual entry to the database on my end and it works fine. The value shows up in the backend and I was able to retrieve it with no issues at all.

In your case you say that the value doesn't show up in the backend at all. Can you try testing this again and let me know if the correct user ID is being added as well as if its a manual entry there must be a wpcf- prefix added before the slug of the custom field, example wpcf-my-single-line

Please let me know if these items check out.

Thanks,
Shane

#1094605
user profile.JPG
wp_usermeta.JPG
wp_users.JPG
page output.JPG

If I was testing as the wrong user or if I hadn't added the second custom field correctly to the database, my own custom short code wouldn't work!

Please look at the images I've uploaded and the following snippets of code I've used to demonstrate that using the type shortcode inside the conditional isn't working (this is on a minimal multi site install on site #1)

User
Image 'wp_users' is screenshot from database showing user id is 3 which concurs with the frontend output (see image 'page output')

Custom User Fields

Image 'wp_usermeta' shows the two custom user fields in the database

User Profile

Image 'user profile' shows only the custom field that was added via the registration form

My Custom Shortcode

These are the shortcodes I've created to return the values of the two custom user fields:-

add_shortcode( 'custom-field-1' , 'ts_user_custom_field_1' );
function ts_user_custom_field_1(){
    
    $blog_id = get_current_blog_id();

	$user_id = get_current_user_id();
	$custom_field_1 = get_user_meta($user_id , 'wpcf-custom-field-1', true);

    return $custom_field_1;
}
add_shortcode( 'custom-field-2' , 'ts_user_custom_field_2' );
function ts_user_custom_field_2(){
    
    $blog_id = get_current_blog_id();

	$user_id = get_current_user_id();
	$custom_field_2 = get_user_meta($user_id , 'wpcf-custom-field-2', true);

    return $custom_field_2;
}

Page Code

[toolset_access role="Contributor" operator="allow"]
<h4>User Credentials</h4>
Hello my username is [wpv-user field="user_login"] and my user ID is [wpv-user field="ID"]
<h4>Conditional Shortcode Based on Types Shortcode</h4>
<ul>
<li>
[wpv-conditional if="('[types usermeta='custom-field-1' user_current='true' output='raw'][/types]' eq '111' )"]I was created via User form 'Register' and my value is 111[/wpv-conditional]
</li>
<li>
[wpv-conditional if="('[types usermeta='custom-field-2' user_current='true' output='raw'][/types]' eq '222' )"]I was added manually to the database in phpMyAdmin and my value is 222[/wpv-conditional]
</li>
</ul>
<h4>Conditional Shortcode Based on My Custom Shortcode</h4>
<ul>
<li>
[wpv-conditional if="('[custom-field-1]' eq '111' )"]I was created via User form 'Register' and my value is 111[/wpv-conditional]
</li>
<li>
[wpv-conditional if="('[custom-field-2]' eq '222' )"]I was added manually to the database in phpMyAdmin and my value is 222[/wpv-conditional]
</li>
</ul>
[/toolset_access]

See image 'page output' for frontend content

#1095155

Shane
Supporter

Les langues: Anglais (English )

Fuseau horaire: America/Jamaica (GMT-05:00)

Hi Juilie,

I see yes as you mentioned this should work. Thanks for clarifying my concerns.

I tried to download the duplicator package that you provided previously but was not able to download it. I need to have this copy and test so that I can provide our team with an example to play around with.

This is because i'm not able to replicate your specific issue on a fresh install.

Thanks,
Shane

#1095661

The clone I had created previously didn't include a registration form with custom user field so it became redundant.

I've created another one now but you didn't re-enable a private field so I've updated the contents of private thread #1091062 so you should be able to download the duplicator package now.

Thanks

PS: My name contains one i, not two