ad
目次
導入
こんにちは。Webサイトの運営をしていると避けて通れないのが、昨今のプライバシー対応。特にEU圏(EEA)からのアクセスがある場合、Cookie使用の同意バナーを出す必要がありますよね。
でも、プラグインを入れるとサイトが重くなるし、日本語ページでは出したくない…。 さらに言えば、ただ機械的に「同意してください」と出すのではなく、「サイト運営の裏側」を正直に伝えて、納得した上で応援してもらいたい。
今回は、Polylangで構築したサイトの「英語ページだけ」に、透明性を重視した同意バナーをスクラッチで実装したお話を紹介します。
1. なぜプラグインを使わなかったのか?
最初は便利なプラグインを探しましたが、以下の点が気になりました。
- 全ページに出てしまう: 日本語ページではまだそこまで厳格にしたくない場合も、一律で表示されてしまう。
- デザインが浮く: サイトの雰囲気に合わせた微調整が意外と面倒。
- 重くなる: 1つの機能のためにプラグインを増やすのは避けたい。
そこで、WordPressの functions.php に少しコードを書き足すだけの「スクラッチ実装」を選びました。
2. 「透明性」を重視したメッセージの力
今回こだわったのは、バナーに載せるメッセージです。 単なる「Cookieを使います」という通知ではなく、**「このサイトは広告収入で維持されており、Cookieをオンにしてもらえると直接的な支援になります」**という意図を正直に盛り込みました。
これ、実は海外の個人ブロガーの間では「透明性重視型」として信頼を得ている手法です。
選択肢によって次のような変化があります。
許可した時の動作:Cookieを用いたパーソナライズされたGoogle広告が表示される。
拒否した時の動作:非パーソナライズ広告が表示される。(影響としては閲覧者にささらない広告でクリック率、収入が下がる)
メッセージにはその旨が表示されています。
To keep this site free and high-quality, we use cookies for advertising. While you can opt-out, keeping them On directly supports our revenue and helps us continue writing. We value your privacy and thank you for your support! If you decline, you will still see ads, but they will not be personalized because there will be no cookies.

3. 実装コード(コピペOK)
PolylangでURLを /en/ のように分けている場合、以下のコードを functions.php に貼るだけで動きます。
コード内にある下記の部分はご自身のAdsensenIDに差し替えてください。
script.src = 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-xxxxxxxxxxxxxxxxx'; // ご自身のAdsense IDに変更してください
function add_transparent_cookie_banner() {
if (strpos($_SERVER['REQUEST_URI'], '/en/') !== false) {
?>
<div id="custom-cookie-banner" style="display:none; position:fixed; bottom:30px; left:50%; transform:translateX(-50%); width:90%; max-width:500px; background:#ffffff; color:#333; padding:25px; z-index:999999; border-radius:15px; box-shadow: 0 10px 30px rgba(0,0,0,0.2); border: 1px solid #eee; line-height: 1.6;">
<div style="text-align:center; margin-bottom: 15px;">
<span style="font-size: 24px;">🍪</span>
<h3 style="margin: 10px 0; font-size: 18px;">Support Our Content</h3>
</div>
<p style="font-size: 14px; margin-bottom: 20px;">
To keep this site free and high-quality, we use cookies for advertising. While you can opt-out, keeping them On directly supports our revenue and helps us continue writing. We value your privacy and thank you for your support! If you decline, you will still see ads, but they will not be personalized because there will be no cookies.
</p>
<div style="display:flex; gap:10px;">
<button id="cookie-accept-btn" style="flex:1; background:#007cba; color:#fff; border:none; padding:12px; border-radius:8px; cursor:pointer; font-weight:bold;">I'll Support (Accept)</button>
<button id="cookie-decline-btn" style="flex:1; background:#f0f0f0; color:#666; border:none; padding:12px; border-radius:8px; cursor:pointer;">Opt-out</button>
</div>
</div>
<script>
(function() {
var consentKey = 'user_cookie_consent';
var banner = document.getElementById('custom-cookie-banner');
if (!localStorage.getItem(consentKey)) {
banner.style.display = 'block';
}
document.getElementById('cookie-accept-btn').onclick = function() {
localStorage.setItem(consentKey, 'accepted');
banner.style.display = 'none';
location.reload();
};
document.getElementById('cookie-decline-btn').onclick = function() {
localStorage.setItem(consentKey, 'declined');
banner.style.display = 'none';
// 既存のCookieを削除
document.cookie.split(";").forEach(function(c) {
document.cookie = c.replace(/^ +/, "").replace(/=.*/, "=;expires=" + new Date().toUTCString() + ";path=/");
});
location.reload();
};
})();
</script>
<?php
}
}
add_action('wp_footer', 'add_transparent_cookie_banner');
// 広告スクリプトを条件付きで読み込む
function load_ads_conditionally() {
if (strpos($_SERVER['REQUEST_URI'], '/en/') !== false) {
?>
<script>
(function() {
var consent = localStorage.getItem('user_cookie_consent');
if (!consent) return; // 未選択なら何もしない(バナーが出る)
window.adsbygoogle = window.adsbygoogle || [];
if (consent === 'declined') {
// 拒否された場合:非パーソナライズ広告をリクエスト
window.adsbygoogle.requestNonPersonalizedAds = 1;
console.log('AdSense: Non-Personalized Mode active');
}
// AdSenseの本体スクリプトを読み込む
var script = document.createElement('script');
script.src = 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-xxxxxxxxxxxxxxxxx'; // ご自身のAdsense IDに変更してください
script.async = true;
document.head.appendChild(script);
})();
</script>
<?php
}
}
add_action('wp_head', 'load_ads_conditionally');4. 実際にやってみて気づいたこと
実は、最近Google AdSenseの「インテント広告」からサポート詐欺のような怪しいサイトが表示される経験があり、一部の広告機能を停止しました。
ユーザーを守るために怪しい広告は排除する。その代わり、サイトを維持するために必要なクッキー(広告配信)については正直にお願いして、理解してもらう。 この「誠実さのバランス」こそが、これからの個人メディア運営に必要なんじゃないかなと感じています。
結び
「ルールだから仕方なくやる」のではなく、読者に対して「こういう理由で協力してほしい」と伝える。 スクラッチなら、そんな血の通ったバナーが作れます。Polylangで多言語サイトを運営している方は、ぜひ試してみてください。
ad




