I was playing around in establishing a simple cookie consent.
<?php
/**
* This function is used to dequeue and deregister WordPress scripts and styles based on user consent.
* It checks for the presence of a 'consent' cookie. If the cookie is not set, the function de-registers
* and de-queues certain scripts and styles specified in $script_handles and $style_handles arrays.
* This function is hooked to the 'wp_enqueue_scripts' action, so it is called when WordPress enqueues
* scripts and styles.
*/
function dequeue_scripts_and_styles_by_consent() {
// An array of script handles to be dequeued if consent cookie isn't set
$script_handles = array();
// PASTE SCRIPT EXCLUSIONS HERE
// An array of style handles to be dequeued if consent cookie isn't set
$style_handles = array();
// PASTE STYLE SHEET EXCLUSIONS HERE
// Check if consent cookie is set
if( !isset( $_COOKIE['consent'] ) ) {
// Dequeue scripts
foreach( $script_handles as $handle ) {
wp_dequeue_script( $handle );
wp_deregister_script( $handle );
}
// Dequeue styles
foreach( $style_handles as $handle ) {
wp_dequeue_style( $handle );
wp_deregister_style( $handle );
}
}
}
add_action( 'wp_enqueue_scripts', 'dequeue_scripts_and_styles_by_consent', 9998 );
This function is only a helper function to determine the handles on a page (output in console)
/**
* This function is primarily a helper function used to print handles of all the queued scripts and styles in WordPress to the JavaScript console.
* It is hooked to the 'wp_print_scripts' action, meaning it runs when WordPress prints the scripts.
* The main purpose of this function is to assist in generating a list of all enqueued scripts and styles which can then be used in the dequeue_scripts_and_styles_by_consent function.
* It outputs these handles as ready-to-copy-and-paste lines of PHP code, specifically formatted to be directly used in the $script_handles and $style_handles arrays of the dequeue_scripts_and_styles_by_consent function.
* This function can be disabled once the list is generated and the necessary scripts and styles have been identified and pasted into the dequeue_scripts_and_styles_by_consent function.
* The function is useful for debugging and checking the sequence and dependencies of the enqueued scripts and styles.
*/
function print_scripts_and_styles() {
global $wp_scripts;
$script_handles = $wp_scripts->queue;
$script_handles_list = '';
foreach( $script_handles as $handle ) {
$script_handles_list .= '$script_handles[] = \'' . $handle . '\';\n';
}
if ($script_handles_list) echo '<script>console.log("' . $script_handles_list . '")</script>' . "\n";
global $wp_styles;
$style_handles = $wp_styles->queue;
$style_handles_list = '';
foreach( $style_handles as $handle ) {
$style_handles_list .= '$style_handles[] = \'' . $handle . '\';\n';
}
if ($style_handles_list) echo '<script>console.log("' . $style_handles_list . '")</script>' . "\n";
}
add_action( 'wp_print_scripts', 'print_scripts_and_styles', 9999 );
Just sharing my WIP… if anybody wants to add to it or has some ideas… you are welcome.
Regards
PS: Is missing a cookie consent setter… this can be done by adding the cookie $_COOKIE['consent']
via Bricks. Also, tracking the IP and consent in the WP Database is missing. So, probably not GDPR ready, but it actually does dequeue all scripts and styles that have not been consented to.