Custom shortcode not firing in template

WordPress
2 May, 2021 01:58:30
gci
Topics: 3
Messages: 5
Hi,

I have a custom shortcode that pulls ACF repeater fields and does timezone conversion, then prints in a list.
It works perfectly on the page, but does not fire in the pdf template.

I have tried in textarea and html object.

I have attached a screenshot of what the screenshot outputs.

Shortcode:
[dateau1]

Code:
function displayDateau1(){
if( have_rows('1_dates_&_times') ):

// Loop through rows.
while( have_rows('1_dates_&_times') ) : the_row();
// Load sub field value.
$sub_value = get_sub_field('date_&_time');
$utc_date = DateTime::createFromFormat('d/m/Y g:i a', $sub_value, new DateTimeZone('Australia/Sydney'));

$acst_date = clone $utc_date; // we don't want PHP's default pass object by reference here
$acst_date->setTimeZone(new DateTimeZone('America/New_York'));

echo '' . $utc_date->format('j M, Y g: i a') . '<br/>'; // UTC: 2011-04-27 2:45 AM
// echo '' . $acst_date->format('j M, Y g: i a'); // ACST: 2011-04-27 12:15 PM
endwhile;

// No value.
else :
// Do something...
endif;
}
add_shortcode('dateau1', 'displayDateau1');
2 May, 2021 08:53:29
E2Pdf
Support
Topics: 7
Messages: 3335
Hi,

There are 2 issues why shortcode not working.

1. E2Pdf Template has no global $post object so your shortcode can't get correctly data.
2. You are using "echo" in shortcode instead of "return".

To make it work:

1. We currently released an update which will allow using [id] as shortcode attribute so you can detect the Post ID for which it must be generated content so please update plugin to latest version (1.13.40 (Stable) or 1.14.13 (RC)).

2. Modify your PHP shortcode function to: https://codeshare.io/2KApqz. It has modification to detect "attribute" from where we will get the Post ID and replaced function which will output data via "return" and not with "echo". It must still work inside pages with same way without modification shortcode however it will be better to recheck.

3. Modify your shortcode inside E2Pdf Template to [dateau1 post_id="[id]"]

We remain at your service.
We would really appreciate your feedback at WordPress.org!