A modular WordPress plugin to manage ad insertion, visibility rules, and global tracking scripts using Gutenberg blocks.
This plugin allows you to:
- Insert AdSense or custom ads via Gutenberg blocks
- Set visibility rules by user role
- Define default ad behavior for blocks
- Inject global tracking scripts (GA4, GTM, etc.)
- Manage all options from a single, accessible admin page (Yoast-style)
- Admin page with 3 tabs: Ads, Block Defaults, Tracking
- Settings API integration with validation and sanitization
- Full SCSS structure compiled via Webpack
- Custom tab navigation styled for clean UX
- Modular architecture
- Script injection via
<head>andwp_body_open - Role-based ad visibility with
has-ads/has-no-adsbody classes - Central visibility control with
sab_should_show_ads() - Translatable (
Text Domain: site-ads-by-bertuuk)
-
Global Ads Configuration
Navigate to Settings → Site Ads and enter:- Your AdSense client ID
- The full
<script>to inject in the<head> - A fallback ad HTML
- User roles that should NOT see ads
-
Block Defaults
In the "Block Defaults" tab, define:- Default Ad Slot
- Default Ad Format (
auto,rectangle,horizontal, etc.) - Responsive behavior (on/off)
-
Tracking Scripts
In the "Tracking" tab, you can:- Enable or disable global script injection
- Paste your GA4, GTM, or other tracking scripts
- Include a
<noscript>GTM fallback after<body>viawp_body_open
npm install
npm run build # one-time build
npm run start # watch modeThe compiled files go to assets/admin.css and assets/admin.js.
- Admin page UI with custom tab navigation
- Global and default block settings via Settings API
- Tracking script management
- Webpack + SCSS setup
- Conditionally load AdSense and tracking scripts
- Inject
<script>tags in<head>based on settings - Inject GTM
<noscript>code withwp_body_open - Role-based visibility logic with
sab_user_can_see_ads() - Add conditional body classes (
has-ads,has-no-ads) - Centralize logic with
sab_should_show_ads()
- Create
adsense-bannerblock with attributes:adSlot,adFormat,responsive,showOnMobile, etc.
- Default values fallback to global settings
- Server-side rendering with PHP
- Compatibility with FSE (Full Site Editing)
Made by Bertuuk with ❤️