Halloween Sale 15% off Ends in See the plans
PHP

"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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.