Last active
March 28, 2025 18:41
-
-
Save cmcnulty/7673548ef8b0c338f412b20b66e61c06 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: WP Cookie Blocker | |
* Description: Blocks cookies set by other plugins (specifically WP Dark Mode) | |
* Version: 1.0.0 | |
* Author: Your Name | |
*/ | |
// Exit if accessed directly | |
if (!defined('ABSPATH')) { | |
exit; | |
} | |
class WP_Cookie_Blocker { | |
/** | |
* Initialize the plugin | |
*/ | |
public function __construct() { | |
// Use priority -100 to ensure our script runs before everything else | |
add_action('wp_head', array($this, 'print_cookie_blocker'), -100); | |
} | |
/** | |
* Print the script directly in the head | |
* Using inline script to ensure it loads as early as possible | |
*/ | |
public function print_cookie_blocker() { | |
echo "<!-- WP Cookie Blocker - Start -->\n"; | |
echo "<script id=\"wp-cookie-blocker-inline\">\n"; | |
readfile(plugin_dir_path(__FILE__) . 'js/cookie-blocker.js'); | |
echo "\n</script>\n"; | |
echo "<!-- WP Cookie Blocker - End -->\n"; | |
} | |
} | |
// Initialize the plugin | |
new WP_Cookie_Blocker(); | |
// For extra early loading, add this code to wp-config.php just before the line that says "/* That's all, stop editing! Happy publishing. */" | |
// This will ensure it loads even before WordPress fully initializes | |
// | |
// function wp_cookie_blocker_extra_early() { | |
// if (is_admin() || wp_doing_ajax()) return; | |
// add_action('wp_head', function() { | |
// $script_path = WP_CONTENT_DIR . '/plugins/wp-cookie-blocker/js/cookie-blocker.js'; | |
// if (file_exists($script_path)) { | |
// echo "<!-- Extra Early WP Cookie Blocker -->\n"; | |
// echo "<script>\n"; | |
// readfile($script_path); | |
// echo "\n</script>\n"; | |
// } | |
// }, -9999); | |
// } | |
// add_action('init', 'wp_cookie_blocker_extra_early', -9999); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment