Wordpress plugin to show a message saying that the site uses cookies

eu-cookie-consent.php 3.4KB

    <?php /* Plugin Name: eu-cookie-consent Plugin URI: http://magick-source.net/for-wordpress/eu-cookie-consent Description: Show a message warning users that the site uses cookies Version: 0.3.1 Author: theMage Author URI: http://magick-source.net/u/themage Text Domain: eu-cookie-consent License: http://www.gnu.org/licenses/gpl-2.0.en.html */ define( 'EU_COOKIECONSENT_VERSION', '0.3.1'); global $cookie_options_defaults; $cookie_options_defaults = array( 'message' => 'This website uses cookies to ensure ' .'you get the best experience. ', 'dismiss' => 'Got it!', 'learnMore' => 'More info', 'link' => null, 'theme' => 'light-floating', 'expiryDays' => 365, ); ### Create Text Domain For Translations add_action( 'plugins_loaded', 'eu_cookieconsent_textdomain' ); function eu_cookieconsent_textdomain() { load_plugin_textdomain('wp-cookie-consent'); } ### Function: Cookie Consent Option Menu add_action('admin_menu', 'eu_cookieconsent_menu'); function eu_cookieconsent_menu() { if (function_exists('add_options_page')) { add_options_page( __('Cookie Consent', 'eu-cookie-consent'), __('Cookie Consent','eu-cookie-consent'), 'manage_options', 'eu-cookie-consent/cookieconsent-options.php'); } } register_activation_hook( __FILE__, 'eu_cookieconsent_activation'); function eu_cookieconsent_activation( $network_wide ) { global $cookie_options_defaults; $option_name = 'eu_cookieconsent_options'; $option = $cookie_options_defaults; if ( is_multisite() && $network_wide ) { $sites = wp_get_sites(); if (sizeof($sites) > 0 ) { foreach ( $sites as $site ) { switch_to_blog( $site['blog_id'] ); add_option( $option_name, $option ); } } restore_current_blog(); } else { add_option( $option_name, $option ); } } add_action('admin_enqueue_scripts', 'eu_cookieconsent_admin'); function eu_cookieconsent_admin($hook_suffix) { $eu_cookieconsent_admin_pages = array('eu-cookie-consent/cookieconsent-options.php','eu-cookie-consent/eu-cookie-consent.php'); if (in_array($hook_suffix, $eu_cookieconsent_admin_pages) ) { wp_enqueue_style('eu-cookie-consent',plugins_url('eu-cookie-consent/styles.css'), false, EU_COOKIECONSENT_VERSION,'all'); } } function eu_cookieconsent_options_parse( $key ) { return !empty($_POST[$key]) ? $_POST[$key] : null; } add_action('wp_head','eu_cookieconsent_headers'); function eu_cookieconsent_headers() { global $cookie_options_defaults; $cookie_options = get_option('eu_cookieconsent_options'); $cookie_options = array_merge( $cookie_options_defaults, $cookie_options ); $result = "window.cookieconsent_options = {\n"; $cnt = 0; foreach ($cookie_options as $option_name => $option_value ) { if ( $option_value ) { if ( $cnt++ ) { $result = $result .",\n"; } if ($option_name === 'theme') { $theme = 'silktide/styles/'.$option_value.'.css'; $option_value = plugins_url($theme, __FILE__); } $result = $result . "\"$option_name\": \"" . htmlspecialchars(stripslashes( $option_value )) . "\""; } } $result = $result . "\n};\n"; echo "<script type='text/javascript'>\n $result </script>\n"; echo "<script type='text/javascript' src='" . plugins_url('silktide/cookieconsent.js',__FILE__) ."?v=".EU_COOKIECONSENT_VERSION."'></script>"; }