Title: Adding code in footer.php
Last modified: February 26, 2018

---

# Adding code in footer.php

 *  Resolved [albertx7](https://wordpress.org/support/users/albertx7/)
 * (@albertx7)
 * [8 years, 3 months ago](https://wordpress.org/support/topic/adding-code-in-footer-php/)
 * Hi,
    I try to add this code in footer.php for to generate cookie bar. I add it
   before the tag </body>
 *     ```
       <!-- Codice per accettazione cookie - Inizio -->
       <script type='text/javascript'>
       //<![CDATA[
       (function(window) {
         if (!!window.cookieChoices) {
           return window.cookieChoices;
         }
         var document = window.document; 
         var supportsTextContent = 'textContent' in document.body;
         var cookieChoices = (function() {
           var cookieName = 'displayCookieConsent';
           var cookieConsentId = 'cookieChoiceInfo';
           var dismissLinkId = 'cookieChoiceDismiss';
           function _createHeaderElement(cookieText, dismissText, linkText, linkHref) {
             var butterBarStyles = 'position:fixed;width:100%;background-color:#eee;' +
                 'margin:0; left:0; bottom:0;padding:4px;z-index:1000;text-align:center;';
             var cookieConsentElement = document.createElement('div');
             cookieConsentElement.id = cookieConsentId;
             cookieConsentElement.style.cssText = butterBarStyles;
             cookieConsentElement.appendChild(_createConsentText(cookieText));
             if (!!linkText && !!linkHref) {
               cookieConsentElement.appendChild(_createInformationLink(linkText, linkHref));
             }
             cookieConsentElement.appendChild(_createDismissLink(dismissText));
             return cookieConsentElement;
           }
           function _createDialogElement(cookieText, dismissText, linkText, linkHref) {
             var glassStyle = 'position:fixed;width:100%;height:100%;z-index:999;' +
                 'bottom:0;left:0;opacity:0.5;filter:alpha(opacity=50);' +
                 'background-color:#ccc;';
             var dialogStyle = 'z-index:1000;position:fixed;left:50%;top:50%';
             var contentStyle = 'position:relative;left:-50%;margin-top:-25%;' +
                 'background-color:#fff;padding:20px;box-shadow:4px 4px 25px #888;';
             var cookieConsentElement = document.createElement('div');
             cookieConsentElement.id = cookieConsentId;
             var glassPanel = document.createElement('div');
             glassPanel.style.cssText = glassStyle;
             var content = document.createElement('div');
             content.style.cssText = contentStyle;
             var dialog = document.createElement('div');
             dialog.style.cssText = dialogStyle;
             var dismissLink = _createDismissLink(dismissText);
             dismissLink.style.display = 'block';
             dismissLink.style.textAlign = 'right';
             dismissLink.style.marginTop = '8px';
             content.appendChild(_createConsentText(cookieText));
             if (!!linkText && !!linkHref) {
               content.appendChild(_createInformationLink(linkText, linkHref));
             }
             content.appendChild(dismissLink);
             dialog.appendChild(content);
             cookieConsentElement.appendChild(glassPanel);
             cookieConsentElement.appendChild(dialog);
             return cookieConsentElement;
           }
           function _setElementText(element, text) {
             if (supportsTextContent) {
               element.textContent = text;
             } else {
               element.innerText = text;
             }
           }
           function _createConsentText(cookieText) {
             var consentText = document.createElement('span');
             _setElementText(consentText, cookieText);
             return consentText;
           }
           function _createDismissLink(dismissText) {
             var dismissLink = document.createElement('a');
             _setElementText(dismissLink, dismissText);
             dismissLink.id = dismissLinkId;
             dismissLink.href = '#';
             dismissLink.style.marginLeft = '24px';
             return dismissLink;
           }
           function _createInformationLink(linkText, linkHref) {
             var infoLink = document.createElement('a');
             _setElementText(infoLink, linkText);
             infoLink.href = linkHref;
             infoLink.target = '_blank';
             infoLink.style.marginLeft = '8px';
             return infoLink;
           }
           function _dismissLinkClick() {
             _saveUserPreference();
             _removeCookieConsent();
             return false;
           }
           function _showCookieConsent(cookieText, dismissText, linkText, linkHref, isDialog) {
             if (_shouldDisplayConsent()) {
               _removeCookieConsent();
               var consentElement = (isDialog) ?
                   _createDialogElement(cookieText, dismissText, linkText, linkHref) :
                   _createHeaderElement(cookieText, dismissText, linkText, linkHref);
               var fragment = document.createDocumentFragment();
               fragment.appendChild(consentElement);
               document.body.appendChild(fragment.cloneNode(true));
               document.getElementById(dismissLinkId).onclick = _dismissLinkClick;
             }
           }
           function showCookieConsentBar(cookieText, dismissText, linkText, linkHref) {
             _showCookieConsent(cookieText, dismissText, linkText, linkHref, false);
           }
           function showCookieConsentDialog(cookieText, dismissText, linkText, linkHref) {
             _showCookieConsent(cookieText, dismissText, linkText, linkHref, true);
           }
           function _removeCookieConsent() {
             var cookieChoiceElement = document.getElementById(cookieConsentId);
             if (cookieChoiceElement != null) {
               cookieChoiceElement.parentNode.removeChild(cookieChoiceElement);
             }
           }
           function _saveUserPreference() {
       // Durata del cookie di un anno
             var expiryDate = new Date();
             expiryDate.setFullYear(expiryDate.getFullYear() + 1);
             document.cookie = cookieName + '=y; expires=' + expiryDate.toGMTString();
           }
           function _shouldDisplayConsent() {
             // Per mostrare il banner solo in mancanza del cookie
             return !document.cookie.match(new RegExp(cookieName + '=([^;]+)'));
           }
           var exports = {};
           exports.showCookieConsentBar = showCookieConsentBar;
           exports.showCookieConsentDialog = showCookieConsentDialog;
           return exports;
         })();
         window.cookieChoices = cookieChoices;
         return cookieChoices;
       })(this);
       document.addEventListener('DOMContentLoaded', function(event) {
           cookieChoices.showCookieConsentBar('Questo sito utilizza i cookie per migliorare servizi ed esperienza dei lettori. Se continui la navigazione consideriamo che accetti il loro uso.',
               'OK', '+Info', 'http://www.salutealtop.com/policy');
         });
       //]]>
       </script>
       <!-- Codice per accettazione cookie - Fine -->
       ```
   
 * But wordpress says: _There was an error. Your changes may not have been saved.
   Try again. There is a possibility that you have to manually resolve the problem
   and upload the file via FTP._
 * How can I add it?
 * Thank you!
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fadding-code-in-footer-php%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

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

 *  Theme Author [Tom](https://wordpress.org/support/users/edge22/)
 * (@edge22)
 * [8 years, 3 months ago](https://wordpress.org/support/topic/adding-code-in-footer-php/#post-10018355)
 * Hi there,
 * Try adding this as a function:
 *     ```
       add_action( 'wp_footer', 'tu_add_custom_script' );
       function tu_add_custom_script() { 
       ?>
           <!-- Codice per accettazione cookie - Inizio -->
       	<script type='text/javascript'>
       	//<![CDATA[
       	(function(window) {
       	  if (!!window.cookieChoices) {
       	    return window.cookieChoices;
       	  }
       	  var document = window.document; 
       	  var supportsTextContent = 'textContent' in document.body;
       	  var cookieChoices = (function() {
       	    var cookieName = 'displayCookieConsent';
       	    var cookieConsentId = 'cookieChoiceInfo';
       	    var dismissLinkId = 'cookieChoiceDismiss';
       	    function _createHeaderElement(cookieText, dismissText, linkText, linkHref) {
       	      var butterBarStyles = 'position:fixed;width:100%;background-color:#eee;' +
       	          'margin:0; left:0; bottom:0;padding:4px;z-index:1000;text-align:center;';
       	      var cookieConsentElement = document.createElement('div');
       	      cookieConsentElement.id = cookieConsentId;
       	      cookieConsentElement.style.cssText = butterBarStyles;
       	      cookieConsentElement.appendChild(_createConsentText(cookieText));
       	      if (!!linkText && !!linkHref) {
       	        cookieConsentElement.appendChild(_createInformationLink(linkText, linkHref));
       	      }
       	      cookieConsentElement.appendChild(_createDismissLink(dismissText));
       	      return cookieConsentElement;
       	    }
       	    function _createDialogElement(cookieText, dismissText, linkText, linkHref) {
       	      var glassStyle = 'position:fixed;width:100%;height:100%;z-index:999;' +
       	          'bottom:0;left:0;opacity:0.5;filter:alpha(opacity=50);' +
       	          'background-color:#ccc;';
       	      var dialogStyle = 'z-index:1000;position:fixed;left:50%;top:50%';
       	      var contentStyle = 'position:relative;left:-50%;margin-top:-25%;' +
       	          'background-color:#fff;padding:20px;box-shadow:4px 4px 25px #888;';
       	      var cookieConsentElement = document.createElement('div');
       	      cookieConsentElement.id = cookieConsentId;
       	      var glassPanel = document.createElement('div');
       	      glassPanel.style.cssText = glassStyle;
       	      var content = document.createElement('div');
       	      content.style.cssText = contentStyle;
       	      var dialog = document.createElement('div');
       	      dialog.style.cssText = dialogStyle;
       	      var dismissLink = _createDismissLink(dismissText);
       	      dismissLink.style.display = 'block';
       	      dismissLink.style.textAlign = 'right';
       	      dismissLink.style.marginTop = '8px';
       	      content.appendChild(_createConsentText(cookieText));
       	      if (!!linkText && !!linkHref) {
       	        content.appendChild(_createInformationLink(linkText, linkHref));
       	      }
       	      content.appendChild(dismissLink);
       	      dialog.appendChild(content);
       	      cookieConsentElement.appendChild(glassPanel);
       	      cookieConsentElement.appendChild(dialog);
       	      return cookieConsentElement;
       	    }
       	    function _setElementText(element, text) {
       	      if (supportsTextContent) {
       	        element.textContent = text;
       	      } else {
       	        element.innerText = text;
       	      }
       	    }
       	    function _createConsentText(cookieText) {
       	      var consentText = document.createElement('span');
       	      _setElementText(consentText, cookieText);
       	      return consentText;
       	    }
       	    function _createDismissLink(dismissText) {
       	      var dismissLink = document.createElement('a');
       	      _setElementText(dismissLink, dismissText);
       	      dismissLink.id = dismissLinkId;
       	      dismissLink.href = '#';
       	      dismissLink.style.marginLeft = '24px';
       	      return dismissLink;
       	    }
       	    function _createInformationLink(linkText, linkHref) {
       	      var infoLink = document.createElement('a');
       	      _setElementText(infoLink, linkText);
       	      infoLink.href = linkHref;
       	      infoLink.target = '_blank';
       	      infoLink.style.marginLeft = '8px';
       	      return infoLink;
       	    }
       	    function _dismissLinkClick() {
       	      _saveUserPreference();
       	      _removeCookieConsent();
       	      return false;
       	    }
       	    function _showCookieConsent(cookieText, dismissText, linkText, linkHref, isDialog) {
       	      if (_shouldDisplayConsent()) {
       	        _removeCookieConsent();
       	        var consentElement = (isDialog) ?
       	            _createDialogElement(cookieText, dismissText, linkText, linkHref) :
       	            _createHeaderElement(cookieText, dismissText, linkText, linkHref);
       	        var fragment = document.createDocumentFragment();
       	        fragment.appendChild(consentElement);
       	        document.body.appendChild(fragment.cloneNode(true));
       	        document.getElementById(dismissLinkId).onclick = _dismissLinkClick;
       	      }
       	    }
       	    function showCookieConsentBar(cookieText, dismissText, linkText, linkHref) {
       	      _showCookieConsent(cookieText, dismissText, linkText, linkHref, false);
       	    }
       	    function showCookieConsentDialog(cookieText, dismissText, linkText, linkHref) {
       	      _showCookieConsent(cookieText, dismissText, linkText, linkHref, true);
       	    }
       	    function _removeCookieConsent() {
       	      var cookieChoiceElement = document.getElementById(cookieConsentId);
       	      if (cookieChoiceElement != null) {
       	        cookieChoiceElement.parentNode.removeChild(cookieChoiceElement);
       	      }
       	    }
       	    function _saveUserPreference() {
       	// Durata del cookie di un anno
       	      var expiryDate = new Date();
       	      expiryDate.setFullYear(expiryDate.getFullYear() + 1);
       	      document.cookie = cookieName + '=y; expires=' + expiryDate.toGMTString();
       	    }
       	    function _shouldDisplayConsent() {
       	      // Per mostrare il banner solo in mancanza del cookie
       	      return !document.cookie.match(new RegExp(cookieName + '=([^;]+)'));
       	    }
       	    var exports = {};
       	    exports.showCookieConsentBar = showCookieConsentBar;
       	    exports.showCookieConsentDialog = showCookieConsentDialog;
       	    return exports;
       	  })();
       	  window.cookieChoices = cookieChoices;
       	  return cookieChoices;
       	})(this);
       	document.addEventListener('DOMContentLoaded', function(event) {
       	    cookieChoices.showCookieConsentBar('Questo sito utilizza i cookie per migliorare servizi ed esperienza dei lettori. Se continui la navigazione consideriamo che accetti il loro uso.',
       	        'OK', '+Info', 'http://www.salutealtop.com/policy');
       	  });
       	//]]>
       	</script>
       	<!-- Codice per accettazione cookie - Fine -->
       <?php 
       }
       ```
   
 * You should add it using one of these methods: [https://docs.generatepress.com/article/adding-php/](https://docs.generatepress.com/article/adding-php/)
 *  Thread Starter [albertx7](https://wordpress.org/support/users/albertx7/)
 * (@albertx7)
 * [8 years, 3 months ago](https://wordpress.org/support/topic/adding-code-in-footer-php/#post-10024829)
 * With _Code Snippets plugin_, that’s the error:
    [https://s13.postimg.org/u4n7mke87/error.png](https://s13.postimg.org/u4n7mke87/error.png)
 * 🙁
 * I don’t understand how to add this code.
 * Thank you again Tom
 *  Theme Author [Tom](https://wordpress.org/support/users/edge22/)
 * (@edge22)
 * [8 years, 3 months ago](https://wordpress.org/support/topic/adding-code-in-footer-php/#post-10024949)
 * It looks like you copied the code from within your email.
 * Can you try copying it directly from this page?
 *  Thread Starter [albertx7](https://wordpress.org/support/users/albertx7/)
 * (@albertx7)
 * [8 years, 3 months ago](https://wordpress.org/support/topic/adding-code-in-footer-php/#post-10024984)
 * I selected “Only run on site front-end” from plugin’s options.
    IT WORKS. Thank
   you.
 * But, why I can’t add it directly in footer.php?
 *  Theme Author [Tom](https://wordpress.org/support/users/edge22/)
 * (@edge22)
 * [8 years, 3 months ago](https://wordpress.org/support/topic/adding-code-in-footer-php/#post-10025002)
 * You can, you’d just need to make you had the appropriate opening and closing 
   PHP tags.
 * You’d also need to use a child theme and overwrite the entire footer.php file,
   which is likely overkill for something like this.

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

The topic ‘Adding code in footer.php’ is closed to new replies.

 * ![](https://i0.wp.com/themes.svn.wordpress.org/generatepress/3.6.1/screenshot.
   png)
 * GeneratePress
 * [Support Threads](https://wordpress.org/support/theme/generatepress/)
 * [Active Topics](https://wordpress.org/support/theme/generatepress/active/)
 * [Unresolved Topics](https://wordpress.org/support/theme/generatepress/unresolved/)
 * [Reviews](https://wordpress.org/support/theme/generatepress/reviews/)

 * 5 replies
 * 2 participants
 * Last reply from: [Tom](https://wordpress.org/support/users/edge22/)
 * Last activity: [8 years, 3 months ago](https://wordpress.org/support/topic/adding-code-in-footer-php/#post-10025002)
 * Status: resolved