Really simple Cookie Consent (DIY)

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.

1 Like

I’ve compiled the following simple step-by-step guide on how to apply these functions to your WordPress site:

  1. Copy the provided code: Copy the given PHP code, which includes two functions: dequeue_scripts_and_styles_by_consent() and print_scripts_and_styles().
  2. Paste the code in the functions.php file: Navigate to your WordPress theme’s functions.php file, found under wp-content/themes/your-theme/functions.php and paste the copied code at the bottom.
  3. Identify scripts and styles for dequeuing: Identify the scripts and styles you’d like to be dequeued when the user has not provided consent.
  • To do this, load your website in a web browser and open the developer console (F12 on most browsers). Refresh your page and take note of the handles that have been printed out in the console. These are the handles for all the scripts and styles currently enqueued in your site.
  • Copy these handles and paste them into the appropriate arrays in the dequeue_scripts_and_styles_by_consent() function. The handles for scripts go in the $script_handles array, and the handles for styles go in the $style_handles array.
  1. Set the cookie consent: You will need to implement a mechanism to set the ‘consent’ cookie when users agree to your terms. This could be done using a plugin like Bricks or another cookie consent plugin, or by manually creating a JavaScript function that sets the cookie when the user interacts with a consent form or pop-up.
  2. Disable the print_scripts_and_stylesfunction: Once you’ve identified all the scripts and styles you need and added them to the dequeue_scripts_and_styles_by_consent function, you can disable the print_scripts_and_styles function. Comment out or delete the add_action line for this function to prevent it from running.
  3. Additional considerations: As mentioned, tracking user IP and consent is missing in the current script, and it is something required by GDPR, so consider adding athis feature. Ensure you have a robust consent management system in place to comply with privacy laws
1 Like

Thanks for sharing, I really learned a lot from this post!