"Do Not Sell or Share My Personal Information" Link
Adds a [do_not_sell_link] shortcode for the opt-out link US state privacy laws such as the CCPA require; clicking it records the visitor's opt-out in a cookie and tells marketing scripts held back by consent snippets not to run.
Opens WPIDE on
Code
add_shortcode( 'do_not_sell_link', function ( $atts ) {
static $script = false;
$atts = shortcode_atts( array( 'text' => 'Do Not Sell or Share My Personal Information' ), $atts, 'do_not_sell_link' );
$html = '<button type="button" class="wpide-optout" style="background:none;border:0;padding:0;color:inherit;text-decoration:underline;cursor:pointer;font:inherit;">' . esc_html( $atts['text'] ) . '</button><span class="wpide-optout-status" role="status" style="margin-left:.5em;"></span>';
if ( ! $script ) {
$script = true;
$html .= '<script>(function(){function out(){return /(?:^|; )wpide_optout=1/.test(document.cookie)||navigator.globalPrivacyControl===true;}function mark(){document.querySelectorAll(".wpide-optout-status").forEach(function(s){s.textContent=out()?"You have opted out.":"";});}document.addEventListener("click",function(e){if(!e.target.closest(".wpide-optout")){return;}document.cookie="wpide_optout=1;path=/;max-age="+(365*86400)+";SameSite=Lax"+(location.protocol==="https:"?";Secure":"");document.cookie="wpide_consent=denied;path=/;max-age="+(365*86400)+";SameSite=Lax";document.dispatchEvent(new CustomEvent("wpide:consent",{detail:"denied"}));mark();});document.addEventListener("DOMContentLoaded",mark);})();</script>';
}
return $html;
} );
Notes
Visitors who send the Global Privacy Control signal are treated as opted out automatically; your marketing tools still need to respect the wpide_optout cookie or the consent helpers.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.