WordPress function to list site cookies

Watch out! This tutorial is over 4 years old. Please keep this in mind as some code snippets provided may no longer work or need modification to work on current systems.
Tutorial Difficulty Level    

The EU Cookie Directive (as it is popularly known) is a 2009 amendment to the E-Privacy Directive. It requires companies to obtain your consent to place and access data (like cookies) on your digital device (like your laptop). While many plugins exist to control cookies on your WordPress website, there is a shocking lack of them to simply LIST the cookies used.. so let’s write our own!

Add the code below to the functions.php file in your theme folder.

function get_cookies( $paras = '', $content = '' ) {

    if ( strtolower( $paras[ 0 ] ) == 'novalue' ) { $novalue = true; } else { $novalue = false; }

    if ( $content == '' ) { $seperator = ' : '; } else { $seperator = $content; }

    $cookie = $_COOKIE;
    ksort( $cookie );

    $content = "<ul>";
    foreach ( $cookie as $key => $val ) {
        $content .= '<li>' . $key;
        if ( !$novalue ) { $content .= $seperator . $val; }
        $content .= "</li>";
    }
    $content .= "</ul>";

    return do_shortcode( $content );
}
add_shortcode( 'cookies', 'get_cookies' );

Now use the following shortcode in a post or page…

[cookies]

Simple, eh? This will list all the site’s cookies along with their values. Two further options are available…

[cookies novalue]

This will now output the cookie values.

[cookies] ==> [/cookies]

Now by providing a value between opening and closing shortcodes you can override the text that is used to separate the cookie name and the value!

Here’s how it looks on this very website (without the values):

wfwaf-authcookie-984153ea447fc496443ff94a1f9626bf
wordpress_logged_in_c04b9c66a59f8837e9fa1ae02bdefb0b
wordpress_test_cookie
wp-settings-1
wp-settings-time-1