Forum Replies Created

Viewing 5 replies - 1 through 5 (of 5 total)
  • Keep in mind sometimes you cannot simply put all non-functional cookies to block in there: think about youtube embedded video, social share, map which load later as body content or after mouse events.

    Sometimes you can call external APIs without cookies (new maps ones, youtube-nocookies.com, etc), but sometimes you can’t.

    Using an ajax call means you need to unlock all these cookies on the success event. Hard enough.
    Reloading the page, on the other side, lets you handle every cookie policy by working on specific elements once it’s loaded (for example by overriding shortcodes of js composer in yout theme).

    Just a thought

    @blackster000, when you disable “reload page” in options, them you accept cookies on frontend, the plugin set the cookie_notice_accepted cookie to true by itself.
    On your wesite it doesn’t happen, something’s going wrong: check your log console error.

    @christian70, if it save cookies, it means somewhere the held code is printed, so you should see this line before the end of body closing tag:

    <!– Global site tag (gtag.js) – Google Analytics –>

    By the way, I think sometimes cache messes things up a little bit.

    Another step, in ajax handler you might replace hard-coded javascript with the one you put in cookie-notice admin option, so it becomes:

    function get_ajax_cookies_release_scripts(){
    check_ajax_referer( ‘get-ajax-cookies-nonce’, ‘nonce’ );
    ob_start();
    print_blocked_scripts(); /* this is new*/
    $data = ob_get_clean();
    wp_send_json_success( $data );
    }
    add_action( ‘wp_ajax_get_ajax_cookies_release_scripts’, ‘get_ajax_cookies_release_scripts’ );
    add_action( ‘wp_ajax_nopriv_get_ajax_cookies_release_scripts’, ‘get_ajax_cookies_release_scripts’ );

    /* this is new*/

    function print_blocked_scripts(){

    $options = get_option( ‘cookie_notice_options’, false );

    $allowed_html = apply_filters( ‘cn_refuse_code_allowed_html’, array_merge( wp_kses_allowed_html( ‘post’ ), array(
    ‘script’ => array(
    ‘type’ => array(),
    ‘src’ => array(),
    ‘charset’ => array(),
    ‘async’ => array()
    ),
    ‘noscript’ => array()
    )));

    echo apply_filters( ‘cn_refuse_code_scripts_html’, html_entity_decode( trim( wp_kses( $options[‘refuse_code’], $allowed_html ) ) ) );
    }

    It’ a standard wordpress/ajax workflow to load content without page reload, but you need to kwon what theme functions.php is and how ajax works in both php and javascript

    1. First you need to enqueue in your theme the javascript where you detect click event on button.
    2. then you localize that script so you can exchange data and vars between php and javascript in wordpress context.
    3. you create ajax handlers to respond with html (blocked stuff) when ajax call is placed.
    4. you bind click event to call an ajax request and print out the response from handlers.

    This is one of the first tutorial I found sometime ago:

    https://premium.wpmudev.org/blog/load-posts-ajax/?utm_expid=3606929-97.J2zL7V7mQbSNQDPrXwvBgQ.0&utm_referrer=https%3A%2F%2Fwww.google.it%2F

    christian70 I agree with you. Also a “change cookies preference” that resets the user choice would be appreciated. Something like this:

    $(‘#reset-cookies’).click(function(e){
    e.preventDefault();
    document.cookie = ‘cookie_notice_accepted=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;’;
    window.location.reload();
    });

    This is a quick fix that worked for me, but it reloads the page.
    It coul be mae in ajax as wll.

    The main problem with bloking scripts, or cookies, is that some contents load them later than page load, like embedded video, iframe, ect. so there’s no one solution for all.

    Hi, this might be helpful, I’ve just finished coding it for my website after a bit of struggle.
    It uses wordpress standard ajax and seems to work, though I need to test it better.
    Please take it as it is, just a hint.

    in functions.php :

    /* ENQUEUE AND LOCALIZE */

    function child_theme_enqueue_scripts() {

    wp_enqueue_script(‘my_modules’, get_stylesheet_directory_uri().’/assets/js/’, array(‘jquery’), false, true);

    /* AJAX LOCALIZE */
    $args = array(
    ‘nonce’ => wp_create_nonce( ‘get-ajax-cookies-nonce’ ),
    ‘url’ => admin_url( ‘admin-ajax.php’ )
    );
    wp_localize_script( ‘my_modules’, ‘ajaxcookies’, $args );

    }
    add_action( ‘wp_enqueue_scripts’, ‘child_theme_enqueue_scripts’ );

    /* AJAX CALL OUTPUT */

    function get_ajax_cookies_release_scripts(){

    check_ajax_referer( ‘get-ajax-cookies-nonce’, ‘nonce’ );
    ob_start();
    ?>
    <!– Global site tag (gtag.js) – Google Analytics –>
    <script src=”https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXXX”></script&gt;
    <script>
    console.log(‘COOKIE SCRIPTS RELEASED’);
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag(‘js’, new Date());

    gtag(‘config’, ‘UA-XXXXXXXXX’);
    </script>
    <?php
    $data = ob_get_clean();
    wp_send_json_success( $data );

    }
    add_action( ‘wp_ajax_get_ajax_cookies_release_scripts’, ‘get_ajax_cookies_release_scripts’ );
    add_action( ‘wp_ajax_nopriv_get_ajax_cookies_release_scripts’, ‘get_ajax_cookies_release_scripts’ );

    In your modules.js (inside ready function):

    if($(‘#cookie-notice’).length){

    $(‘#cn-accept-cookie’).click(function(){

    var data = {
    action: ‘get_ajax_cookies_release_scripts’,
    nonce: ajaxcookies.nonce
    };
    $.post(ajaxcookies.url, data, function(res) {
    if( res.success && res.data != ”) {
    console.log(‘ajaxcookies success’);
    $(‘body’).append( res.data );
    } else {
    console.log(‘ajaxcookies no success’);
    console.log(res);
    }
    }).fail(function(xhr, textStatus, e) {
    console.log(‘ajaxcookies call fail’);
    console.log(xhr.responseText);
    });

    });

    });

    I hope it helps, you don’t need to modify the plugin code.
    Improvements are welcome!

Viewing 5 replies - 1 through 5 (of 5 total)