{"id":1933,"date":"2025-07-12T09:55:47","date_gmt":"2025-07-12T08:55:47","guid":{"rendered":"https:\/\/ghanim-solution.de\/?p=1933"},"modified":"2025-07-12T10:34:46","modified_gmt":"2025-07-12T09:34:46","slug":"cookie-hinweis-mit-auswahlmoglichkeiten-in-wordpress-einbauen-ohne-plugin","status":"publish","type":"post","link":"https:\/\/ghanim-solution.de\/index.php\/2025\/07\/12\/cookie-hinweis-mit-auswahlmoglichkeiten-in-wordpress-einbauen-ohne-plugin\/","title":{"rendered":"Cookie-Hinweis mit Auswahlm\u00f6glichkeiten in WordPress einbauen (ohne Plugin)"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u2705 Ziel<br><\/h2>\n\n\n\n<h2 class=\"wp-block-heading\">Ein Cookie-Consent-Banner, bei dem:<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Essenzielle Cookies immer aktiv sind<\/li>\n\n\n\n<li>Statistik, Marketing und externe Medien ausw\u00e4hlbar sind<\/li>\n\n\n\n<li>Die Auswahl im Browser gespeichert wird<\/li>\n\n\n\n<li>Externe Dienste <strong>nur bei Zustimmung<\/strong> geladen werden<\/li>\n\n\n\n<li>HTML, CSS und JS in eigenen Dateien ausgelagert sind<\/li>\n\n\n\n<li>Das Ganze \u00fcber <code>functions.php<\/code> ins Theme eingebunden wird<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1- Verzeichnisstruktur<\/h2>\n\n\n\n<pre><code class=\"language-text\">\n\/wp-content\/themes\/dein-theme\/\n\u2514\u2500\u2500 cookie-consent\/\n    \u251c\u2500\u2500 cookie-banner.html\n    \u251c\u2500\u2500 cookie-banner.css\n    \u251c\u2500\u2500 cookie-banner.js\n<\/code><\/pre>\n\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2- Inhalte der Datei: <code>cookie-banner.html<\/code><\/h2>\n\n\n\n<pre><code class=\"language-html\">\n\n&lt;!--\n  OVERLAY\n  Ein halbtransparenter Hintergrund, der den Rest der Seite abdunkelt,\n  wenn das Cookie-Consent-Banner angezeigt wird.\n  Fokus liegt auf dem Banner, Interaktion mit der Seite wird eingeschr\u00e4nkt.\n--&gt;\n&lt;div id=&quot;cookie-consent-overlay&quot;&gt;&lt;\/div&gt;\n\n&lt;!--\n  BANNER\n  Hauptcontainer f\u00fcr das Cookie-Consent-Banner mit allen Infos und Optionen.\n--&gt;\n&lt;div id=&quot;cookie-consent-banner&quot;&gt;\n\n  &lt;!-- \u00dcberschrift: Datenschutz-Pr\u00e4ferenzen --&gt;\n  &lt;h2 style=&quot;margin-top: 0;&quot;&gt;Datenschutz-Pr\u00e4ferenzen&lt;\/h2&gt;\n\n  &lt;!-- Erkl\u00e4rung, dass Einwilligung n\u00f6tig ist --&gt;\n  &lt;p&gt;Wir ben\u00f6tigen Ihre Einwilligung, bevor Sie unsere Website weiter nutzen k\u00f6nnen.&lt;\/p&gt;\n\n  &lt;!-- Erkl\u00e4rung zur Nutzung von Cookies mit Link zur Datenschutzerkl\u00e4rung --&gt;\n  &lt;p&gt;\n    Wir verwenden Cookies und andere Technologien. Einige davon sind essenziell,\n    w\u00e4hrend andere uns helfen, unsere Website und Ihre Erfahrung zu verbessern.\n    Mehr dazu in unserer\n    &lt;a href=&quot;\/datenschutz&quot; target=&quot;_blank&quot;&gt;Datenschutzerkl\u00e4rung&lt;\/a&gt;.\n  &lt;\/p&gt;\n\n  &lt;!-- Hinweis zur Datenverarbeitung in den USA --&gt;\n  &lt;p style=&quot;font-size: 0.9em; color: #555;&quot;&gt;\n    Einige Dienste verarbeiten Daten in den USA. Mit Ihrer Einwilligung stimmen Sie auch\n    dieser \u00dcbermittlung gem\u00e4\u00df Art. 49 Abs. 1 lit. a DSGVO zu.\n  &lt;\/p&gt;\n\n  &lt;!-- Formular zur Auswahl der Cookie-Pr\u00e4ferenzen --&gt;\n  &lt;form id=&quot;cookie-preferences-form&quot; style=&quot;margin-top: 1rem;&quot;&gt;\n\n    &lt;!-- Essenzielle Cookies: immer aktiv, daher disabled --&gt;\n    &lt;label&gt;\n      &lt;input type=&quot;checkbox&quot; checked disabled&gt;\n      &lt;strong&gt;Essenziell&lt;\/strong&gt; (immer aktiv)\n    &lt;\/label&gt;&lt;br&gt;\n\n    &lt;!-- Statistik-Cookies: w\u00e4hlbar --&gt;\n    &lt;label&gt;\n      &lt;input type=&quot;checkbox&quot; name=&quot;statistics&quot;&gt; Statistik\n    &lt;\/label&gt;&lt;br&gt;\n\n    &lt;!-- Marketing-Cookies: w\u00e4hlbar --&gt;\n    &lt;label&gt;\n      &lt;input type=&quot;checkbox&quot; name=&quot;marketing&quot;&gt; Marketing\n    &lt;\/label&gt;&lt;br&gt;\n\n    &lt;!-- Externe Medien: w\u00e4hlbar --&gt;\n    &lt;label&gt;\n      &lt;input type=&quot;checkbox&quot; name=&quot;external&quot;&gt; Externe Medien (z.\u202fB. YouTube)\n    &lt;\/label&gt;&lt;br&gt;&lt;br&gt;\n\n    &lt;!-- Buttons: Auswahl speichern und Alle akzeptieren --&gt;\n    &lt;div style=&quot;margin-top: 1.5rem;&quot;&gt;\n      &lt;button type=&quot;submit&quot; class=&quot;btn-primary&quot;&gt;Auswahl speichern&lt;\/button&gt;\n      &lt;button type=&quot;button&quot; class=&quot;btn-accept-all&quot; onclick=&quot;acceptAllCookies()&quot;&gt;Alle akzeptieren&lt;\/button&gt;\n    &lt;\/div&gt;\n  &lt;\/form&gt;\n\n  &lt;!-- Trennlinie zur optischen Abgrenzung --&gt;\n  &lt;hr style=&quot;margin: 2rem 0;&quot;&gt;\n\n  &lt;!-- Rechtliche Links: Datenschutzerkl\u00e4rung und Impressum --&gt;\n  &lt;div class=&quot;legal-links&quot;&gt;\n    &lt;a href=&quot;\/datenschutz&quot; target=&quot;_blank&quot;&gt;Datenschutzerkl\u00e4rung&lt;\/a&gt; |\n    &lt;a href=&quot;\/impressum&quot; target=&quot;_blank&quot;&gt;Impressum&lt;\/a&gt;\n  &lt;\/div&gt;\n&lt;\/div&gt;\n<\/code><\/pre>\n\n\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3- Inhalte der  Datei: <code>cookie-banner.css<\/code><\/h2>\n\n\n\n<pre><code class=\"language-css\">\n#cookie-consent-overlay {\n  position: fixed;\n  top: 0; left: 0; right: 0; bottom: 0;\n  background: rgba(0, 0, 0, 0.5);\n  z-index: 9998;\n  display: none;\n}\n\n#cookie-consent-banner {\n  display: none;\n  position: fixed;\n  top: 50%; left: 50%;\n  transform: translate(-50%, -50%);\n  background: #fff;\n  padding: 2rem 2.5rem;\n  box-shadow: 0 0 30px rgba(0,0,0,0.4);\n  z-index: 9999;\n  max-width: 700px;\n  width: 90%;\n  border-radius: 12px;\n  text-align: left;\n  font-family: sans-serif;\n  max-height: 90vh;\n  overflow-y: auto;\n}\n\n#cookie-consent-banner a {\n  color: #0056b3;\n  text-decoration: underline;\n}\n\n#cookie-consent-banner a:hover {\n  color: #003366;\n  text-decoration: underline;\n}\n\n.btn-primary, .btn-accept-all {\n  padding: 0.5rem 1rem;\n  color: white;\n  border: none;\n  border-radius: 5px;\n  cursor: pointer;\n}\n\n.btn-primary {\n  background-color: #0073aa;\n  margin-right: 1rem;\n}\n\n.btn-accept-all {\n  background-color: #00aa55;\n}\n\n.legal-links {\n  font-size: 0.85em;\n  text-align: center;\n}\n\n<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4- Inhalte der Datei: <code>cookie-banner.js<\/code><\/h2>\n\n\n\n<pre><code class=\"language-javascript\">\nfunction setConsent(cats) {\n  localStorage.setItem('cookieConsentCategories', JSON.stringify(cats));\n  document.getElementById('cookie-consent-banner').style.display = 'none';\n  document.getElementById('cookie-consent-overlay').style.display = 'none';\n  document.body.style.overflow = '';\n  if (cats.statistics) loadAnalytics();\n  if (cats.marketing) loadMarketing();\n}\n\nfunction acceptAllCookies() {\n  setConsent({ essential: true, statistics: true, marketing: true, external: true });\n}\n\nfunction loadAnalytics() {\n  \/\/ Beispiel: Google Analytics nachladen\n  console.log(\"Analytics geladen\");\n      (function(w,d,s,l,i){\n      w[l]=w[l]||[];w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});\n      var f=d.getElementsByTagName(s)[0],\n      j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';\n      j.async=true;\n      j.src='https:\/\/www.googletagmanager.com\/gtm.js?id='+i+dl;\n      f.parentNode.insertBefore(j,f);\n    })(window,document,'script','dataLayer','GTM-XXXXXXX'); \/\/ Ersetze GTM-XXXXXXX mit deiner echten Google Analytics ID (z.\u202fB. GTM-ABC123DEF4), aber erst, wenn du wirklich bereit bist, das Tracking zu aktivieren.\n\n    window.dataLayer = window.dataLayer || [];\n    window.dataLayer.push({ event: 'cookieConsentAll' });\n}\n\nfunction loadMarketing() {\n  console.log(\"Marketing geladen\");\n}\n\ndocument.addEventListener('DOMContentLoaded', function () {\n  const consent = JSON.parse(localStorage.getItem('cookieConsentCategories') || 'null');\n  if (!consent) {\n    document.getElementById('cookie-consent-banner').style.display = 'block';\n    document.getElementById('cookie-consent-overlay').style.display = 'block';\n    document.body.style.overflow = 'hidden';\n  } else {\n    if (consent.statistics) loadAnalytics();\n    if (consent.marketing) loadMarketing();\n  }\n});\n\ndocument.getElementById('cookie-preferences-form').addEventListener('submit', function (e) {\n  e.preventDefault();\n  const form = e.target;\n  setConsent({\n    essential: true,\n    statistics: form.statistics.checked,\n    marketing: form.marketing.checked,\n    external: form.external.checked\n  });\n});\n\n<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5-  Integration in WordPress: <code>functions.php<\/code><\/h2>\n\n\n\n<p>F\u00fcge in die <code>functions.php<\/code> deines Themes oder Child-Themes Folgendes ein:<br><\/p>\n\n\n\n<pre><code class=\"language-php\">\n\/\/ HTML-Banner einf\u00fcgen\nfunction mytheme_cookie_consent_output() {\n    $html_path = get_stylesheet_directory() . '\/cookie-consent\/cookie-banner.html';\n    if (file_exists($html_path)) {\n        include $html_path;\n    }\n}\nadd_action('wp_footer', 'mytheme_cookie_consent_output');\n\n\/\/ CSS & JS laden\nfunction mytheme_cookie_consent_assets() {\n    $base_uri = get_stylesheet_directory_uri() . '\/cookie-consent';\n    wp_enqueue_style('cookie-banner-style', $base_uri . '\/cookie-banner.css');\n    wp_enqueue_script('cookie-banner-script', $base_uri . '\/cookie-banner.js', [], null, true);\n}\nadd_action('wp_enqueue_scripts', 'mytheme_cookie_consent_assets');\n\n<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\uddea Testen<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u00d6ffne deine Website im Inkognito-Modus.<\/li>\n\n\n\n<li>Du solltest das Banner sehen.<\/li>\n\n\n\n<li>Triff eine Auswahl oder akzeptiere alles.<\/li>\n\n\n\n<li>Wenn du neu l\u00e4dst, sollte das Banner <strong>nicht mehr erscheinen<\/strong>, und in der Konsole sollten nur die freigegebenen Bereiche geladen werden.<\/li>\n<\/ol>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udcdd Hinweis<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Du kannst sp\u00e4ter in <code>loadStatistics()<\/code> oder <code>loadMarketing()<\/code> echte Tools wie <strong>Matomo<\/strong>, <strong>Plausible<\/strong>, <strong>Google Analytics<\/strong> etc. einf\u00fcgen.<\/li>\n\n\n\n<li>Ebenso kannst du in <code>loadExternalMedia()<\/code> z.\u202fB. YouTube-Videos automatisch freigeben.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u2705 Ziel Ein Cookie-Consent-Banner, bei dem: 1- Verzeichnisstruktur \/wp-content\/themes\/dein-theme\/ \u2514\u2500\u2500 cookie-consent\/ \u251c\u2500\u2500 cookie-banner.html \u251c\u2500\u2500 cookie-banner.css \u251c\u2500\u2500 cookie-banner.js 2- Inhalte der Datei: cookie-banner.html &lt;!&#8211; OVERLAY Ein halbtransparenter Hintergrund, der den Rest der Seite abdunkelt, wenn das Cookie-Consent-Banner angezeigt wird. Fokus liegt auf dem Banner, Interaktion mit der Seite wird eingeschr\u00e4nkt. &#8211;&gt; &lt;div id=&quot;cookie-consent-overlay&quot;&gt;&lt;\/div&gt; &lt;!&#8211; BANNER Hauptcontainer [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1948,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1933","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorised"],"_links":{"self":[{"href":"https:\/\/ghanim-solution.de\/index.php\/wp-json\/wp\/v2\/posts\/1933","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ghanim-solution.de\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ghanim-solution.de\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ghanim-solution.de\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ghanim-solution.de\/index.php\/wp-json\/wp\/v2\/comments?post=1933"}],"version-history":[{"count":13,"href":"https:\/\/ghanim-solution.de\/index.php\/wp-json\/wp\/v2\/posts\/1933\/revisions"}],"predecessor-version":[{"id":1947,"href":"https:\/\/ghanim-solution.de\/index.php\/wp-json\/wp\/v2\/posts\/1933\/revisions\/1947"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ghanim-solution.de\/index.php\/wp-json\/wp\/v2\/media\/1948"}],"wp:attachment":[{"href":"https:\/\/ghanim-solution.de\/index.php\/wp-json\/wp\/v2\/media?parent=1933"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ghanim-solution.de\/index.php\/wp-json\/wp\/v2\/categories?post=1933"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ghanim-solution.de\/index.php\/wp-json\/wp\/v2\/tags?post=1933"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}