Skip Navigation

[Resolved] I'm not able to pass a custom field as param of a custom short code

This support ticket is created 3 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 – 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)

This topic contains 8 replies, has 3 voices.

Last updated by patrickF-7 3 years ago.

Assisted by: Shane.

Author
Posts
#2277375

Tell us what you are trying to do? Within a view loop I need to call a custom short code with a custom field of the currently selected item

Is there any documentation that you are following? I checked all the docs and related issues on the toolset site

Is there a similar example that we can see? The output of the first line works as expected, the second one (call to the custom short-code) doesn't work (the short code receives well the 1st param but not the second one) :

[types field="address"][/types]
[snc_set_global name='current_property_address' value='[types field="address"][/types]']

What is the link to your site? hidden link (but a login is needed to access the page - tell me if needed)

#2277391

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

WordPress doesn't support shortcodes as arguments for other shortcodes.

It works specifically for Toolset shortcodes because they have special code to handle that.

Instead of trying to pass the value as a shortcode attribute, try passing it as the shortcode content, e.g.

[snc_set_global name='current_property_address'][types field="address"][/types][/snc_set_global]

Check the shortcode documentation for handling shortcode content: https://developer.wordpress.org/plugins/shortcodes/

#2277445

Hi Nigel, thanks for your suggestion. However even by doing as suggested, the field is not "expanded" in the short code call: here is a log I take at the entry of the short code:

In snc_set_global_shortcode name=current_property_address content=###SPACE###[types field="address"][/types]

Do you know what could be the reason ?

BTW: I spent some type trying to use short codes within short code as different posts were mentioning that option as possible, as this one for instance: https://toolset.com/documentation/legacy-features/views-plugin/shortcodes-within-shortcodes/

#2277667

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Patrick,

Are you using the nested shortcode in a Toolset Content template?

If not then this could be why you're having an issue with the Types shortcode not rendering when nested inside your first shortcode.

This is due to a change in the wordpress shortcode API to disallow the nesting of the shortcodes or shortcodes as parameters.

If you're not using a content template to display your content then I would recommend doing this and see if the shortcode works as intended.

Thanks,
Shane

#2277845

Hi Shane,

as pasted above, I'm using a Toolset shortcode ([types field ...) as parameter of a custom (mine) short code (snc_set_global_shortcode). If that is no longer possible could you please suggest me a solution to pass a custome field in a view loop to a custom function ? I need to save the content of the field in a global variable. Thanks for your help.

Cheers
Patrick

#2277901

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Patrik,

From your previous response I gather that you're actually using this custom shortcode inside a view correct?

Can you paste the exact shortcode php code so that I can test it on my end to see if the issue is replicable?

Thanks,
Shane

#2277909

Hi Shane, yes I use it in a Toolset view loop. Here is the short code PHP:

add_shortcode( 'snc_set_global', 'snc_set_global_shortcode' );
function snc_set_global_shortcode( $atts, $content = null ) {
error_log(json_encode($atts));
error_log($content);
$params = shortcode_atts(
array(
'name' => ''
), $atts );

$varName = $params['name'];
$value = $content;
$GLOBALS['sncvars'][$varName] = $value;
return '';
}

And I use it in the loop to capture the address of an item with the following line:

[snc_set_global name='current_property_address'] [types field="address"][/types] [/snc_set_global]

Thanks

#2277953

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Patrick,

I found the issue to be simply that the shortcode isn't being processed as it normally would if you were to render your custom function onto the page, so no internal processing is being on on the nested shortcode.

Using the do_shortcode() function on the $content variable should resolve the issue. So your code should be.

add_shortcode( 'snc_set_global', 'snc_set_global_shortcode' );
function snc_set_global_shortcode( $atts, $content = null ) {
error_log(json_encode($atts));
error_log($content);
$params = shortcode_atts(
array(
'name' => ''
), $atts );

$varName = $params['name'];
$value = do_shortcode($content);
$GLOBALS['sncvars'][$varName] = $value;
return '';
}

Please let me know if this helps.
Thanks,
Shane

#2278393

Brilliant, thank you so much Shane. I spent 4 days searching on the Toolset documentation and forums to search for a solution with no success. Your proposal works great ! My issue is resolved now. Thank you!