Hooks & Filters
Bannerize provides WordPress filters that allow developers to customize banner behavior and output. These hooks follow standard WordPress conventions and can be used in your theme’s functions.php or in a custom plugin.
Available Filters
wp_bannerize_classes
Modify the CSS classes applied to each banner’s container element.
Signature:
apply_filters('wp_bannerize_classes', array $classes, WP_Post $banner): arrayParameters:
| Parameter | Type | Description |
|---|---|---|
$classes | array | Array of CSS class strings applied to the banner container |
$banner | WP_Post | The WordPress post object for the current banner |
Return: An array of CSS class strings.
Example: Add a Class to All Banners
add_filter('wp_bannerize_classes', function ($classes, $banner) {
$classes[] = 'my-site-banner';
return $classes;
}, 10, 2);Example: Add Classes Based on Banner Type
add_filter('wp_bannerize_classes', function ($classes, $banner) {
$type = get_post_meta($banner->ID, 'banner_type', true);
switch ($type) {
case 'local':
$classes[] = 'banner-local-image';
break;
case 'remote':
$classes[] = 'banner-remote-image';
break;
default:
$classes[] = 'banner-html-content';
break;
}
return $classes;
}, 10, 2);Example: Add Campaign Slug as a CSS Class
add_filter('wp_bannerize_classes', function ($classes, $banner) {
$terms = wp_get_post_terms($banner->ID, 'wp_bannerize_tax', [
'fields' => 'slugs',
]);
if (!is_wp_error($terms)) {
foreach ($terms as $slug) {
$classes[] = 'campaign-' . sanitize_html_class($slug);
}
}
return $classes;
}, 10, 2);This adds classes like campaign-sidebar-ads and campaign-holiday-sale to each banner, allowing you to target campaign-specific styles in CSS:
.campaign-holiday-sale {
border: 2px solid #c41e3a;
border-radius: 8px;
}Example: Add Size-Based Classes
add_filter('wp_bannerize_classes', function ($classes, $banner) {
$width = (int) get_post_meta($banner->ID, 'banner_width', true);
if ($width >= 728) {
$classes[] = 'banner-large';
} elseif ($width >= 300) {
$classes[] = 'banner-medium';
} else {
$classes[] = 'banner-small';
}
return $classes;
}, 10, 2);Example: Remove a Default Class
add_filter('wp_bannerize_classes', function ($classes, $banner) {
// Remove a specific class
$classes = array_filter($classes, function ($class) {
return $class !== 'unwanted-class';
});
return array_values($classes);
}, 10, 2);wp_bannerize_banners_rank_seed
Customize the seed value used for randomizing banner order when orderby is set to random.
Signature:
apply_filters('wp_bannerize_banners_rank_seed', int $seed): intParameters:
| Parameter | Type | Description |
|---|---|---|
$seed | int | The current randomization seed value |
Return: An integer seed value.
This filter is only called when no explicit rank_seed is provided via shortcode attributes, widget settings, or PHP function arguments. If a rank_seed is explicitly set, it takes precedence over this filter.
Example: Change Random Order Every Hour
add_filter('wp_bannerize_banners_rank_seed', function ($seed) {
// Same seed for the entire hour, changes on the hour
return (int) date('YmdH');
});This means all visitors within the same hour see the same “random” banner order. The order changes at the top of each hour.
Example: Change Random Order Daily
add_filter('wp_bannerize_banners_rank_seed', function ($seed) {
// Same seed for the entire day
return (int) date('Ymd');
});Example: Per-User Random Order
add_filter('wp_bannerize_banners_rank_seed', function ($seed) {
if (is_user_logged_in()) {
// Each user sees their own consistent random order
return get_current_user_id();
}
// Anonymous visitors get a different order each page load
return $seed;
});Example: Per-Page Random Order
add_filter('wp_bannerize_banners_rank_seed', function ($seed) {
// Same random order on each specific page, different across pages
return crc32(get_permalink());
});Example: A/B Testing with Two Groups
add_filter('wp_bannerize_banners_rank_seed', function ($seed) {
// Split visitors into two groups based on IP
$ip = $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0';
$group = crc32($ip) % 2; // Returns 0 or 1
return $group === 0 ? 1001 : 1002;
});Group A (seed 1001) and Group B (seed 1002) will each see a different banner arrangement, but the arrangement is consistent within each group.
Using Filters in Your Theme
Add filter code to your theme’s functions.php file or create a custom plugin:
// In functions.php
add_filter('wp_bannerize_classes', 'my_bannerize_classes', 10, 2);
function my_bannerize_classes($classes, $banner) {
$classes[] = 'my-theme-banner';
return $classes;
}If you add filters in your theme’s functions.php, they will be lost when you switch themes. For permanent customizations, consider creating a small custom plugin instead.
Filter Priority
Both filters support WordPress priority levels. A lower number means the filter runs earlier:
// Runs first (priority 5)
add_filter('wp_bannerize_classes', 'early_filter', 5, 2);
// Runs second (priority 10, default)
add_filter('wp_bannerize_classes', 'normal_filter', 10, 2);
// Runs last (priority 20)
add_filter('wp_bannerize_classes', 'late_filter', 20, 2);The filter with the highest priority number has the final say on the output.