Creating a Custom Notification Banner in WordPress
Basic Implementation Approach
The most efficient way to add a notification banner is through a combination of PHP functions hooked into WordPress and cleanly organized CSS/JS. We'll create a solution that's both lightweight and maintainable.
Method 1: Direct Theme Implementation
Add this code to your theme's functions.php to create the banner structure:
function add_notification_banner() {
$banner_text = get_theme_mod('notification_text', '');
if (!empty($banner_text)) {
echo '<div id="notification-banner">';
echo wp_kses_post($banner_text);
echo '<button class="close-banner">×</button>';
echo '</div>';
}
}
add_action('wp_body_open', 'add_notification_banner');
Add the required styles to your theme:
function add_banner_styles() {
wp_enqueue_style('notification-banner', get_template_directory_uri() . '/css/notification-banner.css');
wp_enqueue_script('notification-banner', get_template_directory_uri() . '/js/notification-banner.js', array('jquery'), '1.0', true);
}
add_action('wp_enqueue_scripts', 'add_banner_styles');
Create this CSS (save as notification-banner.css):
#notification-banner {
background: #f8f9fa;
color: #333;
padding: 10px 20px;
text-align: center;
position: relative;
width: 100%;
z-index: 1000;
}
.close-banner {
position: absolute;
right: 10px;
top: 50%;
transform: translateY(-50%);
background: none;
border: none;
cursor: pointer;
}
Add this JavaScript for banner functionality (save as notification-banner.js):
jQuery(document).ready(function($) {
$('.close-banner').click(function() {
$('#notification-banner').slideUp();
localStorage.setItem('bannerClosed', 'true');
});
if (localStorage.getItem('bannerClosed') !== 'true') {
$('#notification-banner').show();
}
});
Method 2: Using the Customizer
Add these settings to your theme's customizer:
function add_banner_customizer_settings($wp_customize) {
$wp_customize->add_section('notification_banner', array(
'title' => 'Notification Banner',
'priority' => 30,
));
$wp_customize->add_setting('notification_text', array(
'default' => '',
'sanitize_callback' => 'wp_kses_post',
));
$wp_customize->add_control('notification_text', array(
'label' => 'Banner Message',
'section' => 'notification_banner',
'type' => 'textarea',
));
}
add_action('customize_register', 'add_banner_customizer_settings');
Security Considerations
- Always sanitize output using wp_kses_post()
- Validate and escape user input
- Use nonces for any form submissions
- Properly enqueue scripts and styles
- Use WordPress core functions instead of raw HTML output
Common Pitfalls to Avoid
- Don't hardcode styles inline
- Avoid using !important in CSS
- Don't forget mobile responsiveness
- Consider performance impact of JavaScript
- Remember to handle different theme compatibility
Plugin Solutions
If you prefer a ready-made solution, consider these plugins:
-
Hello Bar (hellobar.com)
- Easy to customize
- A/B testing capabilities
- Free and premium versions
-
WP Notification Bar (wordpress.org/plugins/wp-notification-bar/)
- Lightweight
- Multiple position options
- Basic but effective
-
Simple Notice Bar (wordpress.org/plugins/simple-notice-bar/)
- Minimalist approach
- Easy configuration
- Free version available
Best Practices
- Load scripts in footer when possible
- Use WordPress hooks properly
- Make banner dismissible
- Store user preferences (using localStorage or cookies)
- Ensure accessibility standards
- Test across different browsers and devices
- Consider caching implications
Remember to test thoroughly before deploying to production and ensure your banner doesn't interfere with other site elements or affect loading speed significantly.