Skip Navigation

[Resolved] writting my first conditional snippet under Toolset

This support ticket is created 4 years 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
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: Africa/Casablanca (GMT+01:00)

Tagged: 

This topic contains 1 reply, has 2 voices.

Last updated by Jamal 4 years ago.

Assisted by: Jamal.

Author
Posts
#1924703
toolset 1-30-21 b.png
toolset 1-30-21 c.png

Tell us what you are trying to do?
implement a snippet within a conditional if
Is there any documentation that you are following?
https://toolset.com/documentation/adding-custom-code/how-to-create-a-custom-shortcode/

the "b.png" is the snippet, I followed your example, the output is '1' for testing purposes
the "c.png" is the view code, always testing for '1'. the view works fine if I remove this particular line testing for search() eq '1'. but the condition is never satisfied because the enclosed output never occurs

Also I tested to see that [search] returns '1' (or the real value from the custom field)
I also registered search as a 3rd party shortcode arguments, thinking that might be necessary
I need to implement some quite complex code in the snippet, one I get this simple demo working
Thanks for your help

#1925287

Hello and thank you for contacting the Toolset support.

You have registered a shortcode to be used in the condition, but you actually used a function in the condition:

[wpv-conditional if="(search() eq '1')"]

When you should use the shortcode for it:

[wpv-conditional if="( '[search]' eq '1')"]

If you want to use the function, you will need to register the function, instead of the shortcode, in the Toolset Settings.
https://toolset.com/documentation/programmer-reference/views/using-custom-functions-in-conditions/
You will also need to wrap it in single quotes:

[wpv-conditional if="( 'search()' eq '1')"]

It is also safer if you move the following line before your code like so:

<?php
/**
 * New custom code snippet (replace this with snippet description).
 */

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.

add_shortcode('search', 'search');
// etc...