Is it possible to return content, and then also continue to do other things?

Please see my shortcode script below (which is intended to print the next 15 minute time interval, eg if the current time is 9:01pm, it would print 9:15pm).

I have managed to produce the correct output, however because I am echoing the HTML, the dynamic content is showing up at the top of my WordPress content area, not in the location I type the shortcode.

I understand after reading that I need to return my value to get it to appear in the right place… However, returning ends the function, and so I miss out on including the javascript that will ultimately insert my correct time value (the javascript is necessary to retrieve the user’s local time).

I’m wondering if there is a way to have two functions in the shortcode, or nest them somehow… change my approach, etc?

<?php
add_shortcode( ‘now_plus_15_min’, ‘now_plus_15_min’ );

function now_plus_15_min() {

// get current date & time
$current_date = date(‘d-M-Y g:i:s A’);
$current_time = strtotime($current_date);

// create new date & time that is the nearest next 15 minute interval
$frac = 900;
$r = $current_time % $frac;
$new_time = $current_time + ($frac-$r);
$new_date = date(‘d-M-Y g:i:s A’, $new_time);

// insert HTML which will be later affected by javascript
// this part is the issue! I need to return this, not echo…
// but returning ends the function before javascript can be run…
echo ‘<span id=”webinartime”></span>’;
echo “n”;

// Modify the PHP new date & time to match the user’s local time
// and insert it into above HTML
echo “<script>
var date = new Date(‘” . $new_date . ” UTC’);
var NextWebinarTime = date.toLocaleString();
console.log(NextWebinarTime);
document.getElementById(‘webinartime’).innerHTML = NextWebinarTime;
</script>
“;

}
?>

Read more here:: Is it possible to return content, and then also continue to do other things?

Leave a Reply

Your email address will not be published. Required fields are marked *