Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lusareal/3c6c2f7f1e8fdd133dbece2256d93635 to your computer and use it in GitHub Desktop.
Save lusareal/3c6c2f7f1e8fdd133dbece2256d93635 to your computer and use it in GitHub Desktop.
Check if adblockers like Ghostery block Popup Maker REST API calls
<?php // Ignore this first line when copying to your child theme's functions.php file.
add_action( 'wp_footer', function () { ?>
<script>
(function () {
function testRestApi(url, cb) {
jQuery.ajax({
url: url,
dataType: "text",
type: "GET",
complete: function (xhr) {
if (typeof cb === "function") cb.apply(this, [xhr.status]);
},
error: function () {
console.log("[PUM] AJAX ERROR");
},
});
}
/**
* 1. Change twenty-twentyone.local to your website's domain
* 2. Add a div with id="ajax-error" to the page with your popup(s).
*/
testRestApi(
"https://twenty-twentyone.local/wp-json/pum/v1/analytics/?event=open&pid=118",
function (status) {
console.log(`[PUM] testRestApi status: ${status} `);
switch (status) {
case 204:
console.log("[PUM] 204: That URL works!");
break;
case 404:
console.log("[PUM] 404: Cannot find that URL.");
break;
case 0:
console.log("[PUM] BLOCKED: Cannot reach that URL.");
const ajaxMessage = document.querySelector("#ajax-error");
if (ajaxMessage) {
ajaxMessage.append(
(document.createElement("p").textContent =
"Ooops! Can you turn off Ghostery so you can see all of our great content? Thank you ;-)")
);
}
break;
default:
console.log("[PUM] UNKNOWN: Cannot reach that URL.");
}
}
);
})();
</script>
<?php } );
/**
* You can add the PHP code snippet to your child theme's functions.php file
* or with third-party plugins such as My Custom Functions and Code Snippets.
*
* Learn more:
* - https://docs.wppopupmaker.com/article/84-getting-started-with-custom-js
* - https://docs.wppopupmaker.com/article/552-getting-started-with-custom-php
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment