Skip Navigation

[Resolved] Testing view output vs. custom field content in conditional block

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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Karachi (GMT+05:00)

This topic contains 3 replies, has 2 voices.

Last updated by Waqar 7 months, 1 week ago.

Assisted by: Waqar.

Author
Posts
#2692633

Hi all,
In a PROFILE CT (blocks) I need to place a conditional block to test the output of a view against the content of a profile custom field (a string in a single line field). If the field content is the same of the view output I show some content (blocks) otherwise not. The view itself works perfectly and returns the desired output. But the conditional block doesn't accept the following syntax:
[wpv-view name="get-secret-code" user="[wpv-current-user info='id']" cached="off"]' eq $(wpcf-secret-code-profile)
Could you please help me with the right syntax or let me know how to do this? If I need to register a view shortcode how ?
thanks
Regards
Nicola

#2692748

Hi Nicola,

Thank you for contacting us and I'd be happy to assist.

Your observation is correct and the processing of the nested shortcodes in the conditional blocks becomes complicated.

It is better to register a custom shortcode that can return 'yes' or 'no', whether the output from the view and the custom field are the same or not.

For example:


add_shortcode('check-view-output-with-field', 'check_view_output_with_field_func');
function check_view_output_with_field_func() {
	// get current user ID
	$current_user_id = do_shortcode("[wpv-current-user info='id']");
	// get view's output
	$view_output = do_shortcode('[wpv-view name="get-secret-code" user="'.$current_user_id.'" cached="off"]');
	// get field value
	$field_value = types_render_field("secret-code-profile", array("output" => "raw"));
	// if view output and the field out are the same, return 'yes' and if not, return 'no'
	if($view_output == $field_value) {
		return 'yes';
	} else {
		return 'no';
	}
}

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

Next, please add "check-view-output-with-field" in the "Third-party shortcode arguments" section, at WP Admin -> Toolset -> Settings -> Front-end Content.

After these steps, you'll be able to compare this shortcode's output in the conditional block.

Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#2692784
aaa.jpg

Hello Waqar,
the snippet works, just I don't understand why when $view_output == $field_value it goes to else (no) instead of yes ! I've checked that $view_output and $field_value are actually the same, see picture. Any clue ?
thanks

#2692822

I'll recommend opening the page's source code ( ref: hidden link ) where you're printing these variable values and checking if any empty spaces or hidden characters are getting inserted at the start or the beginning.

This would explain the difference in the two values which would cause the condition to fail and else part getting executed.

Tip: Once confirmed, you can use the 'trim' function in the shortcode with the two variables before comparing them in the equivalence condition.
( ref: hidden link )

#2692832

Hello Waqar,
It works now ! I knew about the trim, but I didn't know how to apply it in PHP. Great support as usual, I learned a lot from this ticket. Kind regards
Nicola

#2692833

Hello Waqar,
It works now ! I knew about the trim, but I didn't know how to apply it in PHP. Great support as usual, I learned a lot from this ticket. Kind regards
Nicola