Skip Navigation

[Resolved] Automatically registering functions inside conditionals

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

Problem: I would like to automatically register functions for use inside conditionals, by adding those functions with code.

Solution: It turns out there is an undocumented filter wpv_filter_wpv_custom_conditional_functions that you can use to add your own custom functions to the allowed function name array. That part works pretty well right now, you can register your own functions and use them as needed in conditionals. There are a couple of minor issues:
- The functions you add with this hook will not appear in the list of function names in Toolset > Settings > Frontend Content > Functions inside conditional evaluations
- The functions you add here will not appear in the list of available custom functions in the conditional builder popup (see the screenshot here)

Those minor issues aside, it works!

Example

1. Register function

function custom_function($type, $object) {
    $return = 0;
    return $return;
}

2. Add Conditional Support for function:

add_filter( 'wpv_filter_wpv_custom_conditional_functions', 'ts_custom_stuff', 10, 1 );
 
function ts_custom_stuff( $allowed_functions ) {
    if ( ! in_array( 'custom_function', $allowed_functions ) ) {
        $allowed_functions[] = 'custom_function';
    }
    return $allowed_functions;
}

3. Insert function in HTML condition. The syntax is tricky, so pay close attention to the quotes and parentheses as shown in the documentation link below.

[wpv-conditional if="( 'custom_function()' eq 0 )"]
  Custom function equals zero!
[/wpv-conditional]

Relevant Documentation:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-custom-functions-in-conditions/

This support ticket is created 5 years, 2 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Tagged: 

This topic contains 3 replies, has 2 voices.

Last updated by josephC-5 5 years, 2 months ago.

Assisted by: Christian Cox.

Author
Posts
#1367693

Hi there,

I know it's possible to automatically register shortcodes using the 'wpv_custom_inner_shortcodes' filter in PHP. Can I also automatically register shortcodes that are used inside conditionals?

Thanks!

Saul

#1367841

Hello, I'm not aware of a public API for this but I can ask my 2nd tier team if one exists. Stand by and I will update you when I have additional information to share.

#1369095
Screen Shot 2019-10-24 at 10.50.55 AM.png

Okay I have some feedback from the team. It turns out there is an undocumented filter wpv_filter_wpv_custom_conditional_functions that you can use to add your own custom functions to the allowed function name array. That part works pretty well right now, you can register your own functions and use them as needed in conditionals. There are a couple of minor issues:
- The functions you add with this hook will not appear in the list of function names in Toolset > Settings > Frontend Content > Functions inside conditional evaluations
- The functions you add here will not appear in the list of available custom functions in the conditional builder popup (see the screenshot here)

Those minor issues aside, it works!

Example

1. Register function

function custom_function($type, $object) {
    $return = 0;
    return $return;
}

2. Add Conditional Support for function:

add_filter( 'wpv_filter_wpv_custom_conditional_functions', 'ts_custom_stuff', 10, 1 );

function ts_custom_stuff( $allowed_functions ) {
	if ( ! in_array( 'custom_function', $allowed_functions ) ) {
		$allowed_functions[] = 'custom_function';
	}
	return $allowed_functions;
}

3. Insert function in HTML condition. The syntax is tricky, so pay close attention to the quotes and parentheses as shown in the documentation link below.

[wpv-conditional if="( 'custom_function()' eq 0 )"]
  Custom function equals zero!
[/wpv-conditional]

Documentation for this syntax:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-custom-functions-in-conditions/

#1369175

Christian,

This is super cool! I hope you guys update the official documentation and include this useful bit of magic in it.

Thanks!

Saul