I’ve been having a problem where pasting some javascript into an element breaks the editor preview pane, but the site and code work just fine on the front end of the website.
This ticket discusses a similar issue and suggests creating a shortcode for the javascript, which I did.
The shortcode breaks the editor preview as well, but on the front end the shortcode and site work fine.
This is my shortcode: [current_date_time]
And this the JS behind it:
// Custom Display Current Date Time Shortcode
// =============================================================================
add_shortcode("current_date_time", "current_date_time_shortcode");
function current_date_time_shortcode() { ?>
<?php ob_start(); ?>
<script type="text/javascript">
var d = new Date(),
minutes = d.getMinutes().toString().length == 1 ? '0'+d.getMinutes() : d.getMinutes(),
hours = d.getHours().toString().length == 1 ? '0'+d.getHours() : d.getHours(),
ampm = d.getHours() >= 12 ? 'pm' : 'am',
months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],
days = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'],
today = days[d.getDay()]+' '+months[d.getMonth()]+' '+d.getDate()+' '+d.getFullYear()+' '+hours+':'+minutes+ampm,
html = '<h4 style="text-align: center;">As of ' + today + '</h4>';
document.write(html);
</script>
<?php return ob_get_clean(); ?>
<?php }
// =============================================================================
I also tried using several other types of elements but saw the same behavior. Any ideas on how to get the preview editor working?
Also, I’m moving this site from Squarespace to WordPress using X, and the code worked fine on Squarespace.
Site is currently at ma.hartsvilleliving.com. Squarespace site is at mourningamerica.com if you’d like to see what the JS looks like when rendered.