Website script for WordPress
WordPress script setup for single post pages, including conditional CSS/JS loading, jQuery fallback, button styling, and analytics integration.
Updated Sep 15, 20261 min read
When integrating with WordPress sites, copy the script below to ensure correct behaviour of elements such as galleries.
HTML
<script>
if (
document.body.classList.contains("single-post") &&
document.querySelector(".single-post .uptodate-blogpost")
) {
function loadCSS(url) {
var link = document.createElement("link");
link.href = url;
link.rel = "stylesheet";
link.type = "text/css";
document.head.appendChild(link);
}
var style = document.createElement("style");
document.head.appendChild(style);
loadCSS(
"https://uptodatewebdesign.s3.eu-west-3.amazonaws.com/cdn/css/utd-blogger-styles.css",
);
}
document.addEventListener("DOMContentLoaded", function () {
function loadJS(url, callback) {
var script = document.createElement("script");
script.src = url;
script.onload = callback;
document.body.appendChild(script);
}
function ensureJQuery(callback) {
if (typeof jQuery == "undefined") {
loadJS("https://code.jquery.com/jquery-3.6.0.min.js", function () {
if (typeof $ === "undefined") {
$ = jQuery;
}
callback();
});
} else {
if (typeof $ === "undefined") {
$ = jQuery;
}
callback();
}
}
if (
document.body.classList.contains("single-post") &&
document.querySelector(".single-post .uptodate-blogpost")
) {
ensureJQuery(function () {
loadJS(
"https://uptodatewebdesign.s3.eu-west-3.amazonaws.com/cdn/js/plugins.js",
);
});
}
});
</script>
<script>
document
.querySelectorAll('.uptodate-blogpost table a[style*="background-color"]')
.forEach((el) => {
el.classList.add("btn");
});
</script>
Did this answer your question?