Skip Navigation

[Resolved] Conditional ShortCode with lots of nested attributes and shortcodes

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

Problem:
At a certain point HTML Conditionals start to break - this means they will not parse/execute anymore because they have too many ShortCodes with in turn attributes populated by ShortCodes.
What can one do in such cases where too many shortcode attributes are required to pass the adequate data to the HTML conditional?

Solution:
In those cases Custom Code is suggested, you can see an example here
https://toolset.com/forums/topic/conditional-output-using-user-fields-repost/#post-1216771

This support ticket is created 5 years, 10 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Tagged: 

This topic contains 4 replies, has 2 voices.

Last updated by Pat 5 years, 10 months ago.

Assisted by: Beda.

Author
Posts
#1214798

Pat

Hello,

I'm reopening this ticket : https://toolset.com/forums/topic/conditional-output-using-user-fields/

I started first with the [wpv-conditional if="( '[wpv-post-author format="meta" meta="user_level"]' eq '2' )"] and it's working fine.

Then I tried to add a second condition :
[wpv-conditional if="( '[wpv-post-author format="meta" meta="user_level"]' eq '2' ) AND ( '[types usermeta="type-de-membre" id="[wpv-post-author format='meta' meta='ID']"' eq 'ELEVEUR' )"] and nothing was displayed.

If I put the shortcodes [wpv-post-author format='meta' meta='ID'] in the loop, I'm getting the post author ID and if I place [types usermeta="type-de-membre" id="[wpv-post-author format='meta' meta='ID'] also in the loop, then I'm getting the 'ELEVEUR' result

So it seems all shortcodes are working fine separately but are not working if I place them in the condition.

Another point is that I tried with this condition :[wpv-conditional if="( '[wpv-post-author format="meta" meta="user_level"]' eq '2' ) AND ( '[types usermeta="type-de-membre" id="[wpv-post-author format='meta' meta='ID']"' eq 'ELEVEUR' )"] (OR instead of AND) and nothing was displayed which is strange as the first condition should always be taken into consideration !!!

Can you find any idea on why the condition is not running well?

Regards
Pat

#1214891

The syntax is broken of this shortcode, and it might be because there are too many nested attributes.
Then, " and ' will not be alternated anymore and WordPress itself will break this.
On top a siongle ended shortcode is used ([types] without [/types])

In fact the version even if using closing Types Shortcode as you use it will show on the front end:

' eq '2' ) AND ( '"' eq 'ELEVEUR' )"]

This because of the escaped apostrophes, and that happens due to WordPress.

This, works:

[wpv-conditional if="( '[wpv-post-author format="meta" meta="ID"]' eq '1' ) AND ( $(wpcf-type-de-membre) eq '1' )"]This[/wpv-conditional]

User Fields are not available in the conditional GUI, but this still would work:

[wpv-conditional if="( '[wpv-post-author format="meta" meta="ID"]' eq '1' ) AND ( '[types usermeta='type-de-membre' user_id='1'][/types]' eq '1' )"]this[/wpv-conditional]

Actually, even this works, but it's totally over nested and not something we can grant to work (due to WordPress' escaping)

[wpv-conditional if="( '[wpv-post-author format="meta" meta="ID"]' eq '1' ) AND ( '[types usermeta='type-de-membre' user_id='[wpv-post-author format="meta" meta="ID"]'][/types]' eq '1' )"]this[/wpv-conditional]
#1214903

Pat

Hi Beda,

It seems these nested attributes issue will be difficult to solve in the short term.

What about creating a shortcode to make the test like :

[test-condition]This[/test-condition]

Normally, the function should not bring any issue and should help to retrieve all needed info from the current user (meta and custom user fields)?

Regards
Pat

#1216771

Let's say, the nested attributes problem will never be solved. It's due to how WordPress wants us to use ShortCodes.
The usage as it's now in Toolset is already only possible due to "custom magic" we apply to the do_shortcode (we use wpv_do_shortcode due to this).
We cannot (unfortunately) change the intentions of WordPress in this scenario.
If it would be by the "core idea", ShortCodes could not even be used as attributes in HTML but should produce the HTML directly, making ShortCodes a quite un-dynamic thing.

Now, [test-condition]This[/test-condition] is surely a possible solution, where you completely neglect the usage of Toolset ShortCode or API and simply create the custom condition in PHP using WordPress API, then return that in a ShortCode and wrap the content with it.

I cannot really craft this kind of code for you due to our Support Policy https://toolset.com/toolset-support-policy/ but I can give an example.

function custom_condition($atts, $content = null) {
	
   	extract(shortcode_atts(array(
   		'user_id_to_check' => "",
      	'meta_slug' => "",
      	'meta_value' => "",
   	), $atts));
   	$author_ID = get_the_author_meta( 'ID' );
	$author_meta = get_user_meta($author_ID, 'wpcf-'.$meta_slug, true);

	if ($user_id_to_check == $author_ID && $author_meta == $meta_value){
		return $content;
	}
}

add_shortcode('custom-condition', 'custom_condition');

Usage in the Loop of Views:

[custom-condition user_id_to_check="1" meta_slug="afds" meta_value="1"]this is post by author and meta[/custom-condition]

Of course, this code needs adaption to your case but it should give an idea of how you can craft it.
I used this API DOC:
https://codex.wordpress.org/Function_Reference/get_the_author
https://developer.wordpress.org/reference/functions/get_the_author_meta/
https://codex.wordpress.org/Function_Reference/get_user_meta
And this original example:

#1219408

Pat

Hi Beda,

Thanks for your support.
Regards
Pat