Skip Navigation

[Resolved] Snippet returns different output if in CT instead of page

This support ticket is created 2 years, 4 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
- 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 18 replies, has 2 voices.

Last updated by Shane 2 years, 4 months ago.

Assisted by: Shane.

Author
Posts
#2416115
bug.jpg

Hello,
I have implemented a snippet suggested in one of your support threads (I can't find the link anymore, I cleared my cache recently, sorry!) that works fine. I just have simplified the output to get 0 or 1 if one date is <= another date.
I have a test page called "aaa" where the shortcode returns correct results. But when I use the same shortcode in the content template I am creating it returns strange results.
I have a conditional block to test the function output for: "if function output=0 write something, if function output= 1 write something else" (see picture). The conditional fails so I have displayed the shortcode output in a paragraph to visualize the shortcode output and it always returns 0"] (0 + last 2 characters of the shortcode). The shortcode is
[show_day_difference date="[types field='application-date' output='raw'][/types]"]
exactly the same of the "aaa" page, just copied, so it should return the same output, but it doesn't. Any clue ?
Thanks
Regards
Nicola

#2416175

Shane
Supporter

Languages: English (English )

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

Hi Nicola,

Thank you for getting in touch.

The problem here is that the Default Shortcode block that wordpress provides doesn't support nested shortcodes.

To work around this you will need to use the Classic block or the Fields and Text block and then add your shortcode to that.

Please let me know if this helps.
Thanks,
Shane

#2416635
bug2.jpg

Hi Shane,
thanks for replying. I added the same shortcode to a Fields and text block but as you can see the result is exactly the same. Most importantly, the conditional fails and it fails also if I replace
The result of [dates-difference date=[types field='application-date' output='raw']] is equal to 1
with
The result of [dates-difference date=[types field='application-date' output='raw']] is equal to 0"]
Can it be a shortcode bug ?
thanks

#2416645

Same result with the classic block

#2416913

Shane
Supporter

Languages: English (English )

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

Hi Nicola,

This isn't a shortcode bug. The problem is wordpress core itself removed support for nesting of shortcodes. However this should work when used in our Content Template.

There is a way you can workaround this by only including the custom field slug as the parameter and then retrieve the custom field value inside your shortcode using the function below.

types_render_field("my-date", array("style" => "calendar"))

So the my-date value will be replaced with the slug that your provide into your shortcode.
Thanks,
Shane

#2417435

Hi Shane
following your suggestion I changed the shortcode into this:
[show_day_difference date="types_render_field("application-date", array("style" => "calendar"))"]
and now it returns 0 instead of 0"]. Issue is that it ALWAYS returns 0 even when the function on the test page returns 1.
I suppose that this 0 isn't the one returned by the function but some kind of error message ... don't know ....
I tried also to change the quotation marks like this, no change
[show_day_difference date="types_render_field('application-date', array('style' => 'calendar'))"]
Any clue ?

#2417567

Shane
Supporter

Languages: English (English )

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

Hi Nicola,

This is not what i'm suggesting. I'm assuming here that the show_date_difference is a shortcode that you've created.

In such a case then you can modify the shortcode to use the types_render_field() function inside the function for the shortcode.

So what you will be passing into the shortcode is the slug of the custom field. Can you send me the function for your custom shortcode so that I can make the modifications.

Thanks,
Shane

#2417583

ok thanks, please find the code below. FYI I've found the original code here:
https://toolset.com/forums/topic/display-countdown-using-date-field/

<?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("show_day_difference", "show_day_difference_func");
function show_day_difference_func($atts) {
 
    $a = shortcode_atts( array(
        'date' => ''
    ), $atts );
 
    // today's date
    $current_date = date( 'U' );
 
    // date to compare
    $compare_date = $a['date'];
     
    // difference in dates
    $datediff = $compare_date - $current_date;
 
    // difference in number of days
    $datediffdays = round($datediff / (60 * 60 * 24));
 
    // conditional display of difference based on number of days 
    if( ($datediffdays >= 0)) {
        $output = '1';
    }
    elseif ($datediffdays < 0) {
        $output = '0';
    }
 
    return $output;
}
#2417587

Shane
Supporter

Languages: English (English )

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

Hi Nicola,

Here is the modified code.


add_shortcode("show_day_difference", "show_day_difference_func");
function show_day_difference_func($atts) {
  
    $a = shortcode_atts( array(
        'date' => ''
    ), $atts );
  
    // today's date
    $current_date = date( 'U' );
  
    // date to compare
    $compare_date = types_render_field("".$a['date']."", array("output" => "raw"));
      
    // difference in dates
    $datediff = $compare_date - $current_date;
  
    // difference in number of days
    $datediffdays = round($datediff / (60 * 60 * 24));
  
    // conditional display of difference based on number of days 
    if( ($datediffdays >= 0)) {
        $output = '1';
    }
    elseif ($datediffdays < 0) {
        $output = '0';
    }
  
    return $output;
}

Please try the shortcode now like this [show_day_difference date='application-date']

Thanks,
Shane

#2417637

My issue is resolved now. Thank you!

#2418071

Hi Shane
I apologise to bother you again on this, but the function now works and the correct output is displayed on the page. Although the conditional fails. It is a very simple conditional:
( ( '[date-lag date='application-date']' eq '1' ) ) ... or eq '0', one of the two...
It drives me mad why such a simple test doesn't work ! Any clue?
thanks
Regards
Nicola

#2418241

Shane
Supporter

Languages: English (English )

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

Hi Nicola,

Can you add debug='true' as a shortcode attribute for the conditional so that I can see the debug data that is being displayed on the frontend ?

Thanks,
Shane

#2418247
debug.jpg

Hi Shane
debug IS on already (see picture) but nothing is displayed in frontend ....
Same with this
( ( '[date-lag date='application-date' debug='true']' ne '0' ) )

#2418259

Shane
Supporter

Languages: English (English )

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

Hi Nicola,

Would you mind allowing me to have admin access to the website so that I can have a more detailed look at this for you ?

Please where applicable please provide me with a link to an example page where I can see the issue.

I've enabled the private fields for your next response.

Thanks,
Shane

#2418293

Shane
Supporter

Languages: English (English )

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

Hi Nicola,

The problem was that you were using a shortcode called date-lag.

When I checked you don't have a shortcode declared with this name. The correct name that should be used is "show_day_difference"

Thanks,
Shane