Tagged: cookie
-
AuthorPosts
-
June 4, 2021 at 9:24 am #1303962
in the enfold theme how can I move the js script avia_cookie_check_sessionStorage () in the footer? and remove it from the meta tags?
<meta charset=”UTF-8″ />
<!– mobile setting –>
<meta name=”viewport” content=”width=device-width, initial-scale=1″><!– Scripts/CSS and wp_head hook –>
<meta name=’robots’ content=’index, follow, max-image-preview:large, max-snippet:-1, max-video-preview:-1′ /><script type=’text/javascript’>
function avia_cookie_check_sessionStorage()
{
// FF throws error when all cookies blocked !!
var sessionBlocked = false;
try
{
var test = sessionStorage.getItem( ‘aviaCookieRefused’ ) != null;
}
catch(e)
{
sessionBlocked = true;
}var aviaCookieRefused = ! sessionBlocked ? sessionStorage.getItem( ‘aviaCookieRefused’ ) : null;
var html = document.getElementsByTagName(‘html’)[0];
/**
* Set a class to avoid calls to sessionStorage
*/
if( sessionBlocked || aviaCookieRefused )
{
if( html.className.indexOf(‘av-cookies-session-refused’) < 0 )
{
html.className += ‘ av-cookies-session-refused’;
}
}if( sessionBlocked || aviaCookieRefused || document.cookie.match(/aviaCookieConsent/) )
{
if( html.className.indexOf(‘av-cookies-user-silent-accept’) >= 0 )
{
html.className = html.className.replace(/\bav-cookies-user-silent-accept\b/g, ”);
}
}
}avia_cookie_check_sessionStorage();
</script>
June 7, 2021 at 5:01 pm #1304444Hey Demia87,
Thank you for the inquiry.
We can nullify the original script by adding this script in the functions.php file.
// a custom script // disable avia_cookie_check_sessionStorage function ava_custom_script_mod_cookie_check() { if ( wp_script_is( 'avia-default', 'registered' ) ) { wp_add_inline_script( 'avia-default', ' window.avia_cookie_check_sessionStorage = null; '); } } add_action( 'wp_enqueue_scripts', 'ava_custom_script_mod_cookie_check', 9999);
Then use this code to add the script back to the footer. Please note that the original function will still exist but it should not work because it’s already nulled.
/** * Add a script that removes the class av-cookies-user-silent-accept if local browser session has the cookie set * that user refused cookies. This is a fallback for enfold\js\avia-snippet-cookieconsent.js * * As FF throws an error when cookies are disabled we have to add this workaround. * * @since 4.6.4 */ function avia_cookie_check_session_storage_mod() { $privacy = av_privacy_helper(); $option = $privacy->get_cookie_consent_message_bar_option(); if( 'disabled' == $option ) { return; } $output = ''; $output .= " <script type='text/javascript'> function avia_cookie_check_sessionStorage_mod() { // FF throws error when all cookies blocked !! var sessionBlocked = false; try { var test = sessionStorage.getItem( 'aviaCookieRefused' ) != null; } catch(e) { sessionBlocked = true; } var aviaCookieRefused = ! sessionBlocked ? sessionStorage.getItem( 'aviaCookieRefused' ) : null; var html = document.getElementsByTagName('html')[0]; /** * Set a class to avoid calls to sessionStorage */ if( sessionBlocked || aviaCookieRefused ) { if( html.className.indexOf('av-cookies-session-refused') < 0 ) { html.className += ' av-cookies-session-refused'; } } if( sessionBlocked || aviaCookieRefused || document.cookie.match(/aviaCookieConsent/) ) { if( html.className.indexOf('av-cookies-user-silent-accept') >= 0 ) { html.className = html.className.replace(/\bav-cookies-user-silent-accept\b/g, ''); } } } avia_cookie_check_sessionStorage_mod(); </script> "; echo $output; } add_action("init", function() { add_action( 'wp_footer', 'avia_cookie_check_session_storage_mod', 10 ); }, 25);
Best regards,
IsmaelJune 8, 2021 at 3:06 pm #1304659hi Ishmael, I inserted your code in the enfold child theme in function.php file, but no effect, I still see the avia_cookie_check_sessionStorage function between the meta tags
June 11, 2021 at 4:45 am #1305176Hi,
Thank you for the update.
Yes, my bad. I just realized how pointless the solution above is. Please edit the enfold/includes/helper-privacy.php file and look for this code around line 131.
add_action( 'wp_head', array( $this, 'handler_wp_head_script' ), 1 );
Comment it out or remove it completely, then add this code below.
add_action( 'wp_footer', array( $this, 'handler_wp_head_script' ), 1 );
Best regards,
IsmaelJune 11, 2021 at 10:11 am #1305234thanks Ismael for your support, this time it worked, when the theme is updated that file is overwritten?
June 11, 2021 at 1:14 pm #1305264Hi,
Yes, unfortunately, this will get overwritten on update. Unfortunately, we cannot find a way to override the current action hook that is attached to the privacy class in the functions.php file. This is also why we recommended the first solution.
Why do you need the script in the footer anyway? It should work fine in the head tag and should not incur any performance overhead.
Best regards,
IsmaelJune 11, 2021 at 2:55 pm #1305278actually it is not for the overhead, i think it could have a negative impact on seo, i like clean code, with fewer scripts possible, or at least transfer the js in the footer
June 11, 2021 at 4:34 pm #1305302I also preferred the first solution, but unfortunately it doesn’t work, I inserted your code in the function.php file of the child theme but to no avail, maybe if you can find another way to make that code stable even when updates are made it would be a lot better.
June 14, 2021 at 7:41 am #1305535Hi,
i think it could have a negative impact on seo
Why would it have a negative impact on SEO? It is a very a small script which should not affect the actual content of the site.
Best regards,
IsmaelJune 14, 2021 at 11:26 am #1305567because I believe that the structure of an html site should be clean especially the part that concerns the meta tags with as few scripts as possible, and not grafted between the meta with raw code, for me they should all be inserted in the footer I don’t say it I have it read in textbooks. In any case it is not a criticism, on the contrary I really like the enfold theme I know that it is one of the best themes around, even if in my opinion some things could still be improved.
June 16, 2021 at 1:55 pm #1306002Hi,
Crawlers should be smart enough to ignore scripts that are not directly related to the content or at least assign priority on how they should be assessed, so having this small script within the head tag will not affect how search engines crawl the site.
You may need to keep the modification for now if you really need to move it to the footer.
Best regards,
IsmaelJune 19, 2021 at 3:56 pm #1306511ok tanks
June 20, 2021 at 9:47 pm #1306611Hi,
Did you need additional help with this topic or shall we close?
Best regards,
Jordan ShannonJune 21, 2021 at 4:07 pm #1306765yes for now it’s okay thanks.
June 22, 2021 at 10:14 pm #1307025Hi,
If you need additional help, please let us know here in the forums.
Best regards,
Jordan Shannon -
AuthorPosts
- The topic ‘how can i move the js script avia_cookie_check_sessionStorage () in the footer?’ is closed to new replies.