Skip Navigation

[Resolved] Nested Shortcode not working

This support ticket is created 4 years, 10 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 4 replies, has 2 voices.

Last updated by peterv-5 4 years, 10 months ago.

Assisted by: Minesh.

Author
Posts
#1556743
For ToolSet.png

Tell us what you are trying to do?
I'm nesting the ToolSet field in my own shortcode (for format the data), but my shortcode never gets the value.

This shortcode: [types field=”video-length”][/types] displays seconds => 386 WORKS
This shortcode: [s_f] 3666 [/s_f] turns a raw number (3666 seconds) to “Hrs:Min:Sec” ==> 1:1:6 WORKS
Nested shortcodes: [s_f] [types field=”video-length”][/types][/s_f] ==> 0:0:0 NOT WORKING

Is there any documentation that you are following?

I read this:
https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/?utm_source=typesplugin&utm_medium=plugin&utm_campaign=product&utm_content=codesnippets

And followed the instructions to put the shortcode in ToolSet > Settings > Custom Code, but the that didn't work either.

I've tried a default Theme and turning off all non-essential plugins (leaving all ToolSet plugins working) - no change.

What is the link to your site?
hidden link

#1556983

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Can you please share the code you added to the "Custom Code" section? Once I review that I will be able to guide you in the right direction.

#1557009

This is how I called it:

[s_f][types field=”video-length”][/types][/s_f]

This is the shortcode:

function convert_secs_to_formatedv2 ($atts = [], $content = null, $tag = '')
{
$seconds = (int)$content;
$hours = floor($seconds / 3600);
$seconds -= $hours * 3600;
$minutes = floor($seconds / 60);
$seconds -= $minutes * 60;

$formated = "$hours:$minutes:$seconds"; //24:0:1';

return $formated;
}
add_shortcode('s_f', 'convert_secs_to_formatedv2');

#1557011

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please try to use the following code and replace the following code with your existing code.

function convert_secs_to_formatedv2 ($atts = [], $content = null, $tag = ''){

$seconds = (int)  wpv_do_shortcode($content);
$hours = floor($seconds / 3600);
$seconds -= $hours * 3600;
$minutes = floor($seconds / 60);
$seconds -= $minutes * 60;

$formated = "$hours:$minutes:$seconds"; //24:0:1';

return $formated;
}
add_shortcode('s_f', 'convert_secs_to_formatedv2');

As you can see I've changed the line from:

$seconds = (int)$content;

To:

$seconds = (int)  wpv_do_shortcode($content);

Because as you are passing the shortcode to your content, to get the value from shortcode, you need to execute your shortcode and to do that, you need to use wpv_do_shortcode($content);.

If you will pass shortcode as your custom shortcode attribute - the above method will not be applicable.

For that, you need to change the custom shortcode code as given under:

function convert_secs_to_formatedv2 ($atts = [], $content = null, $tag = ''){

$seconds = (int)  $atts['seconds'];
$hours = floor($seconds / 3600);
$seconds -= $hours * 3600;
$minutes = floor($seconds / 60);
$seconds -= $minutes * 60;

$formated = "$hours:$minutes:$seconds"; //24:0:1';

return $formated;
}
add_shortcode('s_f', 'convert_secs_to_formatedv2');

and you can call the shortcode as:

[s_f  seconds="[types field='video-length'][/types]"]

This way the passed shortcode value will be automatically assigned to the seconds attribute as shown above.

You can go with whatever method that suits you the best as per your requirement.

#1557071

My issue is resolved now. Thank you!