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)
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/
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/
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
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
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
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
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
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!