The Problem: Inconsistent VIP Banner Display 🤔
In our previous article (How to Build a VIP Membership Site on WordPress & WooCommerce), we implemented a membership system using WordPress, WooCommerce, and subscription models. However, we identified a user experience issue:
The VIP promotion banner appeared inconsistently across different user scenarios:
Displayed on regular products for non-members ✅
Still visible on VIP product pages for members ❌
Appeared for upgraded VIP members viewing any products ❌
This created confusion and diminished the premium experience for paying VIP members.
The Solution: Conditional Banner Display Logic 🎯
After thorough testing, we developed a PHP-based solution that controls banner visibility based on two critical factors:
Product type (VIP vs. regular products)
User membership status (logged-in VIP members vs. non-members)
Implementation Code Snippet
function display_vip_banner_based_on_role() {
// 默认显示横幅
$show_banner = true;
// 获取当前产品
global $product;
// 检查当前产品URL或名称
if ($product) {
$product_url = get_permalink($product->get_id());
$product_name = $product->get_name();
// 如果当前产品是VIP购买页面或名称为"vip会员购买",则不显示横幅
if ($product_url === 'https://www.wolzq.com/product/wordpress-plugins-vip-subscription' ||
$product_name === 'vip会员购买') {
$show_banner = false;
}
}
// 如果上面的条件未满足,继续检查用户会员状态
if ($show_banner && is_user_logged_in()) {
$user_id = get_current_user_id();
// 检查用户是否有任何活跃会员资格
if (function_exists('yith_wcmbs_user_has_membership') && yith_wcmbs_user_has_membership($user_id)) {
$show_banner = false;
}
}
// 如果用户不是会员(或未登录)且不在VIP产品页,则显示横幅
if ($show_banner) {
$banner_html = '
<div style="background-color: #fff3cd; border-left: 4px solid #ffc107; padding: 10px 15px; margin: 0px 0; border-radius: 4px;">
<p style="margin: 0; font-weight: bold; color: #856404;">🚀 SMARTER DEAL: <a style="color: #d10000; font-weight: bold;" href="https://www.wolzq.com/product/wordpress-plugins-vip-subscription">Get UNLIMITED Access with VIP Subscription →</a></p>
</div>';
echo $banner_html;
}
}
add_action('woocommerce_product_meta_start', 'display_vip_banner_based_on_role', 25);
Implementation Steps for WordPress WooCommerce Sites 📋
Step 1: Install Code Snippets Plugin
First, install and activate the Code Snippets plugin, which allows safe PHP code implementation without modifying theme files.
Step 2: Create New Snippet
Navigate to: Snippets → Add New
Paste the provided code
Set execution location: Run snippet everywhere
Activate the snippet
Step 3: Woodmart Theme Integration (Optional)
For Woodmart theme users:
Navigate to: Woodmart Theme Settings → Single Product → Custom Content
Insert shortcode or custom HTML to trigger the code snippet
Save changes
Press enter or click to view image in full size
Expected Behavior After Implementation ✅
Scenario 1: Non-Logged-In Users
Regular products: VIP banner visible
VIP products: No banner displayed
Press enter or click to view image in full size
Press enter or click to view image in full size
Scenario 2: Logged-In Non-Members
Regular products: VIP banner visible
VIP products: No banner displayed
Scenario 3: VIP Members
All products: No banner displayed (premium experience maintained)
Press enter or click to view image in full size
Best Practices for WordPress Membership Sites 🔧
Use reliable membership plugins: YITH Membership, MemberPress, or WooCommerce Memberships
Test user roles thoroughly: Always verify functionality across all user types
Implement caching considerations: Ensure membership checks work with caching systems
Maintain code documentation: Comment your customizations for future maintenance
Conclusion 🏁
This implementation provides a sophisticated solution for controlling promotional content visibility based on user membership status — an essential feature for any WordPress membership site or WooCommerce subscription business.
The custom code approach offers flexibility beyond what most themes and plugins provide natively, allowing for precise control over the user experience for both regular customers and premium VIP members.
Pro Tip: Always test custom code implementations on a staging site before deploying to production, and consider adding error logging to handle any unexpected behaviors.
Original link: https://www.wolzq.com/blog/10847
Top comments (0)