A strong inc/core/sidebars.php for the RX theme with many widget areas, safe registration helpers, conditional output helpers, and WooCommerce/AMP/block-widget support where available.
Below is an advanced sidebars.php file for your RX Theme.
Place it here:
rx-theme/inc/core/sidebars.php
Then make sure your functions.php loads it:
require_once get_template_directory() . '/inc/core/sidebars.php';
<?php
/**
* RX Theme Sidebars & Widget Areas
*
* File: inc/core/sidebars.php
*
* This file registers all possible widget/sidebar areas for the RX Theme:
* - Main blog sidebar
* - Homepage sections
* - Header widget areas
* - Footer widget columns
* - WooCommerce sidebars
* - bbPress sidebars
* - BuddyPress sidebars
* - LMS sidebars
* - Ad/banner widget areas
* - Mobile/off-canvas sidebar
* - Sticky sidebar
* - Medical article widget areas
* - Author/profile widgets
* - Before/after content widget zones
* - Helper functions for safely displaying widgets
*
* @package RX_Theme
*/
defined( 'ABSPATH' ) || exit;
if ( ! defined( 'RX_THEME_VERSION' ) ) {
define( 'RX_THEME_VERSION', '1.0.0' );
}
/**
* Prevent duplicate registration.
*/
if ( ! defined( 'RX_SIDEBARS_LOADED' ) ) {
define( 'RX_SIDEBARS_LOADED', true );
}
/**
* Main sidebar registration hook.
*/
add_action( 'widgets_init', 'rx_theme_register_sidebars' );
if ( ! function_exists( 'rx_theme_register_sidebars' ) ) :
/**
* Register all RX Theme sidebars.
*
* @return void
*/
function rx_theme_register_sidebars() {
/**
* Default widget wrappers.
*/
$default_before_widget = '<section id="%1$s" class="widget rx-widget %2$s">';
$default_after_widget = '</section>';
$default_before_title = '<h2 class="widget-title rx-widget-title">';
$default_after_title = '</h2>';
/**
* Register sidebar helper.
*/
$register = function( $args ) use (
$default_before_widget,
$default_after_widget,
$default_before_title,
$default_after_title
) {
$defaults = array(
'name' => '',
'id' => '',
'description' => '',
'before_widget' => $default_before_widget,
'after_widget' => $default_after_widget,
'before_title' => $default_before_title,
'after_title' => $default_after_title,
);
$args = wp_parse_args( $args, $defaults );
if ( empty( $args['id'] ) || empty( $args['name'] ) ) {
return;
}
register_sidebar( $args );
};
/**
* Primary sidebars.
*/
$register(
array(
'name' => esc_html__( 'Primary Sidebar', 'rx-theme' ),
'id' => 'rx-primary-sidebar',
'description' => esc_html__( 'Main sidebar shown beside blog posts, archives, and pages.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'Secondary Sidebar', 'rx-theme' ),
'id' => 'rx-secondary-sidebar',
'description' => esc_html__( 'Optional second sidebar for magazine or multi-column layouts.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'Sticky Sidebar', 'rx-theme' ),
'id' => 'rx-sticky-sidebar',
'description' => esc_html__( 'Sticky sidebar area for popular posts, ads, newsletter, or CTA widgets.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'Mobile Off Canvas Sidebar', 'rx-theme' ),
'id' => 'rx-mobile-offcanvas-sidebar',
'description' => esc_html__( 'Widget area for mobile menu/off-canvas panel.', 'rx-theme' ),
)
);
/**
* Header widget areas.
*/
$register(
array(
'name' => esc_html__( 'Header Top Bar Left', 'rx-theme' ),
'id' => 'rx-header-top-left',
'description' => esc_html__( 'Top header left widget area for phone, notice, social links, or text.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'Header Top Bar Right', 'rx-theme' ),
'id' => 'rx-header-top-right',
'description' => esc_html__( 'Top header right widget area for links, social icons, language switcher, or login.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'Header Before Logo', 'rx-theme' ),
'id' => 'rx-header-before-logo',
'description' => esc_html__( 'Widget area before site logo.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'Header After Logo', 'rx-theme' ),
'id' => 'rx-header-after-logo',
'description' => esc_html__( 'Widget area after site logo.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'Header Advertisement', 'rx-theme' ),
'id' => 'rx-header-ad',
'description' => esc_html__( 'Header banner advertisement area.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'Header Search Area', 'rx-theme' ),
'id' => 'rx-header-search',
'description' => esc_html__( 'Header search, product search, or custom search widget area.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'Below Navigation Bar', 'rx-theme' ),
'id' => 'rx-below-navbar',
'description' => esc_html__( 'Widget area below the main navigation menu.', 'rx-theme' ),
)
);
/**
* Homepage widget areas.
*/
$register(
array(
'name' => esc_html__( 'Homepage Hero Area', 'rx-theme' ),
'id' => 'rx-home-hero',
'description' => esc_html__( 'Homepage hero/banner widget area.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'Homepage Featured Area', 'rx-theme' ),
'id' => 'rx-home-featured',
'description' => esc_html__( 'Featured posts, services, or category widgets on homepage.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'Homepage Before Content', 'rx-theme' ),
'id' => 'rx-home-before-content',
'description' => esc_html__( 'Widget area before homepage main content.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'Homepage After Content', 'rx-theme' ),
'id' => 'rx-home-after-content',
'description' => esc_html__( 'Widget area after homepage main content.', 'rx-theme' ),
)
);
for ( $i = 1; $i <= 12; $i++ ) {
$register(
array(
'name' => sprintf(
/* translators: %d: homepage section number */
esc_html__( 'Homepage Section %d', 'rx-theme' ),
$i
),
'id' => 'rx-home-section-' . $i,
'description' => sprintf(
/* translators: %d: homepage section number */
esc_html__( 'Custom homepage widget section %d.', 'rx-theme' ),
$i
),
)
);
}
/**
* Blog/archive widget areas.
*/
$register(
array(
'name' => esc_html__( 'Blog Before Posts', 'rx-theme' ),
'id' => 'rx-blog-before-posts',
'description' => esc_html__( 'Widget area before blog/archive post list.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'Blog After Posts', 'rx-theme' ),
'id' => 'rx-blog-after-posts',
'description' => esc_html__( 'Widget area after blog/archive post list.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'Archive Header Area', 'rx-theme' ),
'id' => 'rx-archive-header',
'description' => esc_html__( 'Widget area inside archive/category/tag header.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'Category Sidebar', 'rx-theme' ),
'id' => 'rx-category-sidebar',
'description' => esc_html__( 'Sidebar shown on category archive pages.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'Tag Sidebar', 'rx-theme' ),
'id' => 'rx-tag-sidebar',
'description' => esc_html__( 'Sidebar shown on tag archive pages.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'Search Results Sidebar', 'rx-theme' ),
'id' => 'rx-search-sidebar',
'description' => esc_html__( 'Sidebar shown on search result pages.', 'rx-theme' ),
)
);
/**
* Single post widget areas.
*/
$register(
array(
'name' => esc_html__( 'Single Post Sidebar', 'rx-theme' ),
'id' => 'rx-single-post-sidebar',
'description' => esc_html__( 'Sidebar shown on single blog posts.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'Before Single Post Content', 'rx-theme' ),
'id' => 'rx-before-single-content',
'description' => esc_html__( 'Widget area before single post content.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'After Single Post Content', 'rx-theme' ),
'id' => 'rx-after-single-content',
'description' => esc_html__( 'Widget area after single post content.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'Inside Single Post After Paragraph 1', 'rx-theme' ),
'id' => 'rx-inside-post-p1',
'description' => esc_html__( 'Widget/ad area after first paragraph of single post.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'Inside Single Post After Paragraph 3', 'rx-theme' ),
'id' => 'rx-inside-post-p3',
'description' => esc_html__( 'Widget/ad area after third paragraph of single post.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'Inside Single Post After Paragraph 5', 'rx-theme' ),
'id' => 'rx-inside-post-p5',
'description' => esc_html__( 'Widget/ad area after fifth paragraph of single post.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'Related Posts Widget Area', 'rx-theme' ),
'id' => 'rx-related-posts-area',
'description' => esc_html__( 'Widget area around related posts section.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'Author Box Widget Area', 'rx-theme' ),
'id' => 'rx-author-box-area',
'description' => esc_html__( 'Widget area near author bio box.', 'rx-theme' ),
)
);
/**
* Page widget areas.
*/
$register(
array(
'name' => esc_html__( 'Page Sidebar', 'rx-theme' ),
'id' => 'rx-page-sidebar',
'description' => esc_html__( 'Sidebar shown on static pages.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'Before Page Content', 'rx-theme' ),
'id' => 'rx-before-page-content',
'description' => esc_html__( 'Widget area before page content.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'After Page Content', 'rx-theme' ),
'id' => 'rx-after-page-content',
'description' => esc_html__( 'Widget area after page content.', 'rx-theme' ),
)
);
/**
* Medical/content-focused widget areas.
*/
$register(
array(
'name' => esc_html__( 'Medical Article Sidebar', 'rx-theme' ),
'id' => 'rx-medical-article-sidebar',
'description' => esc_html__( 'Special sidebar for medical article pages.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'Medical Disclaimer Area', 'rx-theme' ),
'id' => 'rx-medical-disclaimer-area',
'description' => esc_html__( 'Widget area for medical disclaimer blocks.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'Evidence References Area', 'rx-theme' ),
'id' => 'rx-evidence-references-area',
'description' => esc_html__( 'Widget area for references, citations, or evidence notes.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'Doctor Review Box Area', 'rx-theme' ),
'id' => 'rx-doctor-review-area',
'description' => esc_html__( 'Widget area for doctor/reviewer information.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'Health CTA Area', 'rx-theme' ),
'id' => 'rx-health-cta-area',
'description' => esc_html__( 'Widget area for appointment, newsletter, or health call-to-action blocks.', 'rx-theme' ),
)
);
/**
* Advertisement widget areas.
*/
$register(
array(
'name' => esc_html__( 'Ad Before Header', 'rx-theme' ),
'id' => 'rx-ad-before-header',
'description' => esc_html__( 'Advertisement area before site header.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'Ad After Header', 'rx-theme' ),
'id' => 'rx-ad-after-header',
'description' => esc_html__( 'Advertisement area after site header.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'Ad Before Content', 'rx-theme' ),
'id' => 'rx-ad-before-content',
'description' => esc_html__( 'Advertisement area before main content.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'Ad Middle Content', 'rx-theme' ),
'id' => 'rx-ad-middle-content',
'description' => esc_html__( 'Advertisement area in the middle of content.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'Ad After Content', 'rx-theme' ),
'id' => 'rx-ad-after-content',
'description' => esc_html__( 'Advertisement area after main content.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'Ad Before Footer', 'rx-theme' ),
'id' => 'rx-ad-before-footer',
'description' => esc_html__( 'Advertisement area before footer.', 'rx-theme' ),
)
);
for ( $i = 1; $i <= 10; $i++ ) {
$register(
array(
'name' => sprintf(
/* translators: %d: advertisement slot number */
esc_html__( 'Custom Advertisement Slot %d', 'rx-theme' ),
$i
),
'id' => 'rx-custom-ad-slot-' . $i,
'description' => sprintf(
/* translators: %d: advertisement slot number */
esc_html__( 'Custom advertisement widget slot %d.', 'rx-theme' ),
$i
),
)
);
}
/**
* Footer widget areas.
*/
$register(
array(
'name' => esc_html__( 'Footer Top Area', 'rx-theme' ),
'id' => 'rx-footer-top',
'description' => esc_html__( 'Full-width widget area above footer columns.', 'rx-theme' ),
)
);
for ( $i = 1; $i <= 8; $i++ ) {
$register(
array(
'name' => sprintf(
/* translators: %d: footer column number */
esc_html__( 'Footer Column %d', 'rx-theme' ),
$i
),
'id' => 'rx-footer-column-' . $i,
'description' => sprintf(
/* translators: %d: footer column number */
esc_html__( 'Footer widget column %d.', 'rx-theme' ),
$i
),
)
);
}
$register(
array(
'name' => esc_html__( 'Footer Bottom Left', 'rx-theme' ),
'id' => 'rx-footer-bottom-left',
'description' => esc_html__( 'Bottom footer left widget area.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'Footer Bottom Right', 'rx-theme' ),
'id' => 'rx-footer-bottom-right',
'description' => esc_html__( 'Bottom footer right widget area.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'Footer Copyright Area', 'rx-theme' ),
'id' => 'rx-footer-copyright',
'description' => esc_html__( 'Footer copyright/custom text area.', 'rx-theme' ),
)
);
/**
* WooCommerce widget areas.
*/
if ( class_exists( 'WooCommerce' ) ) {
$register(
array(
'name' => esc_html__( 'WooCommerce Shop Sidebar', 'rx-theme' ),
'id' => 'rx-woocommerce-shop-sidebar',
'description' => esc_html__( 'Sidebar for WooCommerce shop and product archive pages.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'WooCommerce Product Sidebar', 'rx-theme' ),
'id' => 'rx-woocommerce-product-sidebar',
'description' => esc_html__( 'Sidebar for single product pages.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'WooCommerce Before Shop Loop', 'rx-theme' ),
'id' => 'rx-woocommerce-before-shop-loop',
'description' => esc_html__( 'Widget area before WooCommerce product loop.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'WooCommerce After Shop Loop', 'rx-theme' ),
'id' => 'rx-woocommerce-after-shop-loop',
'description' => esc_html__( 'Widget area after WooCommerce product loop.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'WooCommerce Cart Sidebar', 'rx-theme' ),
'id' => 'rx-woocommerce-cart-sidebar',
'description' => esc_html__( 'Widget area for cart page.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'WooCommerce Checkout Sidebar', 'rx-theme' ),
'id' => 'rx-woocommerce-checkout-sidebar',
'description' => esc_html__( 'Widget area for checkout page.', 'rx-theme' ),
)
);
}
/**
* bbPress widget areas.
*/
if ( class_exists( 'bbPress' ) || function_exists( 'bbpress' ) ) {
$register(
array(
'name' => esc_html__( 'bbPress Forum Sidebar', 'rx-theme' ),
'id' => 'rx-bbpress-sidebar',
'description' => esc_html__( 'Sidebar for bbPress forum pages.', 'rx-theme' ),
)
);
}
/**
* BuddyPress widget areas.
*/
if ( class_exists( 'BuddyPress' ) || function_exists( 'buddypress' ) ) {
$register(
array(
'name' => esc_html__( 'BuddyPress Sidebar', 'rx-theme' ),
'id' => 'rx-buddypress-sidebar',
'description' => esc_html__( 'Sidebar for BuddyPress community pages.', 'rx-theme' ),
)
);
}
/**
* LMS widget areas.
*/
if ( class_exists( 'LearnDash_Settings_Section' ) || defined( 'LEARNDASH_VERSION' ) ) {
$register(
array(
'name' => esc_html__( 'LearnDash Sidebar', 'rx-theme' ),
'id' => 'rx-learndash-sidebar',
'description' => esc_html__( 'Sidebar for LearnDash course pages.', 'rx-theme' ),
)
);
}
if ( defined( 'LP_PLUGIN_FILE' ) || class_exists( 'LearnPress' ) ) {
$register(
array(
'name' => esc_html__( 'LearnPress Sidebar', 'rx-theme' ),
'id' => 'rx-learnpress-sidebar',
'description' => esc_html__( 'Sidebar for LearnPress course pages.', 'rx-theme' ),
)
);
}
if ( class_exists( 'LifterLMS' ) ) {
$register(
array(
'name' => esc_html__( 'LifterLMS Sidebar', 'rx-theme' ),
'id' => 'rx-lifterlms-sidebar',
'description' => esc_html__( 'Sidebar for LifterLMS pages.', 'rx-theme' ),
)
);
}
/**
* Custom post type sidebars.
*/
$post_types = get_post_types(
array(
'public' => true,
),
'objects'
);
if ( ! empty( $post_types ) && is_array( $post_types ) ) {
foreach ( $post_types as $post_type ) {
if ( in_array( $post_type->name, array( 'post', 'page', 'attachment', 'product' ), true ) ) {
continue;
}
$register(
array(
'name' => sprintf(
/* translators: %s: post type label */
esc_html__( '%s Sidebar', 'rx-theme' ),
esc_html( $post_type->labels->singular_name )
),
'id' => 'rx-cpt-sidebar-' . sanitize_key( $post_type->name ),
'description' => sprintf(
/* translators: %s: post type label */
esc_html__( 'Sidebar for %s content.', 'rx-theme' ),
esc_html( $post_type->labels->name )
),
)
);
}
}
/**
* Global utility widget areas.
*/
$register(
array(
'name' => esc_html__( '404 Page Widget Area', 'rx-theme' ),
'id' => 'rx-404-widget-area',
'description' => esc_html__( 'Widget area for 404 not found page.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'Maintenance Notice Area', 'rx-theme' ),
'id' => 'rx-maintenance-notice-area',
'description' => esc_html__( 'Widget area for maintenance or site-wide notices.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'Cookie Notice Area', 'rx-theme' ),
'id' => 'rx-cookie-notice-area',
'description' => esc_html__( 'Widget area for cookie/privacy notice.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'Newsletter Area', 'rx-theme' ),
'id' => 'rx-newsletter-area',
'description' => esc_html__( 'Newsletter subscription widget area.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'Social Follow Area', 'rx-theme' ),
'id' => 'rx-social-follow-area',
'description' => esc_html__( 'Social follow widget area.', 'rx-theme' ),
)
);
$register(
array(
'name' => esc_html__( 'Floating CTA Area', 'rx-theme' ),
'id' => 'rx-floating-cta-area',
'description' => esc_html__( 'Floating call-to-action widget area.', 'rx-theme' ),
)
);
}
endif;
/**
* Get all RX sidebar IDs.
*
* @return array
*/
if ( ! function_exists( 'rx_theme_get_sidebar_ids' ) ) :
function rx_theme_get_sidebar_ids() {
return array(
'rx-primary-sidebar',
'rx-secondary-sidebar',
'rx-sticky-sidebar',
'rx-mobile-offcanvas-sidebar',
'rx-header-top-left',
'rx-header-top-right',
'rx-header-before-logo',
'rx-header-after-logo',
'rx-header-ad',
'rx-header-search',
'rx-below-navbar',
'rx-home-hero',
'rx-home-featured',
'rx-home-before-content',
'rx-home-after-content',
'rx-blog-before-posts',
'rx-blog-after-posts',
'rx-archive-header',
'rx-category-sidebar',
'rx-tag-sidebar',
'rx-search-sidebar',
'rx-single-post-sidebar',
'rx-before-single-content',
'rx-after-single-content',
'rx-inside-post-p1',
'rx-inside-post-p3',
'rx-inside-post-p5',
'rx-related-posts-area',
'rx-author-box-area',
'rx-page-sidebar',
'rx-before-page-content',
'rx-after-page-content',
'rx-medical-article-sidebar',
'rx-medical-disclaimer-area',
'rx-evidence-references-area',
'rx-doctor-review-area',
'rx-health-cta-area',
'rx-ad-before-header',
'rx-ad-after-header',
'rx-ad-before-content',
'rx-ad-middle-content',
'rx-ad-after-content',
'rx-ad-before-footer',
'rx-footer-top',
'rx-footer-bottom-left',
'rx-footer-bottom-right',
'rx-footer-copyright',
'rx-404-widget-area',
'rx-maintenance-notice-area',
'rx-cookie-notice-area',
'rx-newsletter-area',
'rx-social-follow-area',
'rx-floating-cta-area',
);
}
endif;
/**
* Safely display a sidebar.
*
* Usage:
* rx_theme_sidebar( 'rx-primary-sidebar' );
*
* @param string $sidebar_id Sidebar ID.
* @param string $wrapper_class Extra wrapper class.
* @return void
*/
if ( ! function_exists( 'rx_theme_sidebar' ) ) :
function rx_theme_sidebar( $sidebar_id = 'rx-primary-sidebar', $wrapper_class = '' ) {
$sidebar_id = sanitize_key( $sidebar_id );
if ( empty( $sidebar_id ) || ! is_active_sidebar( $sidebar_id ) ) {
return;
}
$classes = array(
'rx-sidebar-wrap',
'rx-sidebar-wrap-' . $sidebar_id,
);
if ( ! empty( $wrapper_class ) ) {
$classes[] = sanitize_html_class( $wrapper_class );
}
echo '<aside class="' . esc_attr( implode( ' ', $classes ) ) . '" role="complementary" aria-label="' . esc_attr( rx_theme_get_sidebar_label( $sidebar_id ) ) . '">';
dynamic_sidebar( $sidebar_id );
echo '</aside>';
}
endif;
/**
* Get readable sidebar label.
*
* @param string $sidebar_id Sidebar ID.
* @return string
*/
if ( ! function_exists( 'rx_theme_get_sidebar_label' ) ) :
function rx_theme_get_sidebar_label( $sidebar_id ) {
$labels = array(
'rx-primary-sidebar' => esc_html__( 'Primary Sidebar', 'rx-theme' ),
'rx-secondary-sidebar' => esc_html__( 'Secondary Sidebar', 'rx-theme' ),
'rx-sticky-sidebar' => esc_html__( 'Sticky Sidebar', 'rx-theme' ),
'rx-mobile-offcanvas-sidebar' => esc_html__( 'Mobile Sidebar', 'rx-theme' ),
'rx-header-top-left' => esc_html__( 'Header Top Left', 'rx-theme' ),
'rx-header-top-right' => esc_html__( 'Header Top Right', 'rx-theme' ),
'rx-header-ad' => esc_html__( 'Header Advertisement', 'rx-theme' ),
'rx-header-search' => esc_html__( 'Header Search', 'rx-theme' ),
'rx-below-navbar' => esc_html__( 'Below Navigation', 'rx-theme' ),
'rx-home-hero' => esc_html__( 'Homepage Hero', 'rx-theme' ),
'rx-home-featured' => esc_html__( 'Homepage Featured', 'rx-theme' ),
'rx-blog-before-posts' => esc_html__( 'Before Blog Posts', 'rx-theme' ),
'rx-blog-after-posts' => esc_html__( 'After Blog Posts', 'rx-theme' ),
'rx-archive-header' => esc_html__( 'Archive Header', 'rx-theme' ),
'rx-single-post-sidebar' => esc_html__( 'Single Post Sidebar', 'rx-theme' ),
'rx-page-sidebar' => esc_html__( 'Page Sidebar', 'rx-theme' ),
'rx-medical-article-sidebar' => esc_html__( 'Medical Article Sidebar', 'rx-theme' ),
'rx-medical-disclaimer-area' => esc_html__( 'Medical Disclaimer', 'rx-theme' ),
'rx-evidence-references-area' => esc_html__( 'Evidence References', 'rx-theme' ),
'rx-doctor-review-area' => esc_html__( 'Doctor Review', 'rx-theme' ),
'rx-footer-top' => esc_html__( 'Footer Top', 'rx-theme' ),
'rx-footer-bottom-left' => esc_html__( 'Footer Bottom Left', 'rx-theme' ),
'rx-footer-bottom-right' => esc_html__( 'Footer Bottom Right', 'rx-theme' ),
'rx-footer-copyright' => esc_html__( 'Footer Copyright', 'rx-theme' ),
'rx-404-widget-area' => esc_html__( '404 Page Widgets', 'rx-theme' ),
'rx-newsletter-area' => esc_html__( 'Newsletter Area', 'rx-theme' ),
'rx-social-follow-area' => esc_html__( 'Social Follow Area', 'rx-theme' ),
'rx-floating-cta-area' => esc_html__( 'Floating CTA Area', 'rx-theme' ),
);
if ( isset( $labels[ $sidebar_id ] ) ) {
return $labels[ $sidebar_id ];
}
return ucwords( str_replace( array( 'rx-', '-' ), array( '', ' ' ), $sidebar_id ) );
}
endif;
/**
* Check if any sidebar in a list is active.
*
* @param array $sidebar_ids Sidebar IDs.
* @return bool
*/
if ( ! function_exists( 'rx_theme_any_sidebar_active' ) ) :
function rx_theme_any_sidebar_active( $sidebar_ids = array() ) {
if ( empty( $sidebar_ids ) || ! is_array( $sidebar_ids ) ) {
return false;
}
foreach ( $sidebar_ids as $sidebar_id ) {
if ( is_active_sidebar( sanitize_key( $sidebar_id ) ) ) {
return true;
}
}
return false;
}
endif;
/**
* Display sidebar only if it is active.
*
* @param string $sidebar_id Sidebar ID.
* @param string $class Extra class.
* @return void
*/
if ( ! function_exists( 'rx_theme_display_sidebar_if_active' ) ) :
function rx_theme_display_sidebar_if_active( $sidebar_id, $class = '' ) {
if ( is_active_sidebar( $sidebar_id ) ) {
rx_theme_sidebar( $sidebar_id, $class );
}
}
endif;
/**
* Display header widgets.
*
* Usage in header.php:
* rx_theme_header_widgets();
*
* @return void
*/
if ( ! function_exists( 'rx_theme_header_widgets' ) ) :
function rx_theme_header_widgets() {
$sidebars = array(
'rx-ad-before-header',
'rx-header-top-left',
'rx-header-top-right',
'rx-header-before-logo',
'rx-header-after-logo',
'rx-header-ad',
'rx-header-search',
'rx-below-navbar',
'rx-ad-after-header',
);
if ( ! rx_theme_any_sidebar_active( $sidebars ) ) {
return;
}
echo '<div class="rx-header-widgets">';
foreach ( $sidebars as $sidebar_id ) {
rx_theme_sidebar( $sidebar_id );
}
echo '</div>';
}
endif;
/**
* Display footer widgets.
*
* Usage in footer.php:
* rx_theme_footer_widgets();
*
* @return void
*/
if ( ! function_exists( 'rx_theme_footer_widgets' ) ) :
function rx_theme_footer_widgets() {
$footer_columns = array();
for ( $i = 1; $i <= 8; $i++ ) {
$footer_columns[] = 'rx-footer-column-' . $i;
}
$all_footer_sidebars = array_merge(
array( 'rx-footer-top', 'rx-ad-before-footer' ),
$footer_columns,
array( 'rx-footer-bottom-left', 'rx-footer-bottom-right', 'rx-footer-copyright' )
);
if ( ! rx_theme_any_sidebar_active( $all_footer_sidebars ) ) {
return;
}
echo '<footer class="rx-footer-widgets" role="contentinfo">';
rx_theme_sidebar( 'rx-ad-before-footer' );
rx_theme_sidebar( 'rx-footer-top' );
if ( rx_theme_any_sidebar_active( $footer_columns ) ) {
echo '<div class="rx-footer-columns rx-footer-columns-8">';
foreach ( $footer_columns as $sidebar_id ) {
if ( is_active_sidebar( $sidebar_id ) ) {
echo '<div class="rx-footer-column">';
rx_theme_sidebar( $sidebar_id );
echo '</div>';
}
}
echo '</div>';
}
if ( rx_theme_any_sidebar_active( array( 'rx-footer-bottom-left', 'rx-footer-bottom-right', 'rx-footer-copyright' ) ) ) {
echo '<div class="rx-footer-bottom-widgets">';
rx_theme_sidebar( 'rx-footer-bottom-left' );
rx_theme_sidebar( 'rx-footer-bottom-right' );
rx_theme_sidebar( 'rx-footer-copyright' );
echo '</div>';
}
echo '</footer>';
}
endif;
/**
* Display homepage widgets.
*
* Usage in front-page.php:
* rx_theme_homepage_widgets();
*
* @return void
*/
if ( ! function_exists( 'rx_theme_homepage_widgets' ) ) :
function rx_theme_homepage_widgets() {
if ( ! is_front_page() && ! is_home() ) {
return;
}
$sidebars = array(
'rx-home-hero',
'rx-home-featured',
'rx-home-before-content',
);
for ( $i = 1; $i <= 12; $i++ ) {
$sidebars[] = 'rx-home-section-' . $i;
}
$sidebars[] = 'rx-home-after-content';
if ( ! rx_theme_any_sidebar_active( $sidebars ) ) {
return;
}
echo '<div class="rx-homepage-widgets">';
foreach ( $sidebars as $sidebar_id ) {
rx_theme_sidebar( $sidebar_id );
}
echo '</div>';
}
endif;
/**
* Display archive widgets.
*
* Usage in archive.php/category.php/tag.php:
* rx_theme_archive_widgets();
*
* @return void
*/
if ( ! function_exists( 'rx_theme_archive_widgets' ) ) :
function rx_theme_archive_widgets() {
if ( ! is_archive() && ! is_search() && ! is_home() ) {
return;
}
if ( is_category() ) {
rx_theme_sidebar( 'rx-category-sidebar' );
return;
}
if ( is_tag() ) {
rx_theme_sidebar( 'rx-tag-sidebar' );
return;
}
if ( is_search() ) {
rx_theme_sidebar( 'rx-search-sidebar' );
return;
}
rx_theme_sidebar( 'rx-primary-sidebar' );
}
endif;
/**
* Display single post sidebar.
*
* Usage in single.php:
* rx_theme_single_sidebar();
*
* @return void
*/
if ( ! function_exists( 'rx_theme_single_sidebar' ) ) :
function rx_theme_single_sidebar() {
if ( ! is_single() ) {
return;
}
$post_type = get_post_type();
if ( 'post' === $post_type ) {
if ( is_active_sidebar( 'rx-single-post-sidebar' ) ) {
rx_theme_sidebar( 'rx-single-post-sidebar' );
} else {
rx_theme_sidebar( 'rx-primary-sidebar' );
}
return;
}
if ( 'product' === $post_type && class_exists( 'WooCommerce' ) ) {
if ( is_active_sidebar( 'rx-woocommerce-product-sidebar' ) ) {
rx_theme_sidebar( 'rx-woocommerce-product-sidebar' );
}
return;
}
$cpt_sidebar = 'rx-cpt-sidebar-' . sanitize_key( $post_type );
if ( is_active_sidebar( $cpt_sidebar ) ) {
rx_theme_sidebar( $cpt_sidebar );
return;
}
rx_theme_sidebar( 'rx-primary-sidebar' );
}
endif;
/**
* Display page sidebar.
*
* Usage in page.php:
* rx_theme_page_sidebar();
*
* @return void
*/
if ( ! function_exists( 'rx_theme_page_sidebar' ) ) :
function rx_theme_page_sidebar() {
if ( ! is_page() ) {
return;
}
if ( is_active_sidebar( 'rx-page-sidebar' ) ) {
rx_theme_sidebar( 'rx-page-sidebar' );
return;
}
rx_theme_sidebar( 'rx-primary-sidebar' );
}
endif;
/**
* Display WooCommerce sidebar.
*
* Usage in woocommerce.php:
* rx_theme_woocommerce_sidebar();
*
* @return void
*/
if ( ! function_exists( 'rx_theme_woocommerce_sidebar' ) ) :
function rx_theme_woocommerce_sidebar() {
if ( ! class_exists( 'WooCommerce' ) ) {
return;
}
if ( function_exists( 'is_product' ) && is_product() ) {
rx_theme_sidebar( 'rx-woocommerce-product-sidebar' );
return;
}
if ( function_exists( 'is_cart' ) && is_cart() ) {
rx_theme_sidebar( 'rx-woocommerce-cart-sidebar' );
return;
}
if ( function_exists( 'is_checkout' ) && is_checkout() ) {
rx_theme_sidebar( 'rx-woocommerce-checkout-sidebar' );
return;
}
if ( function_exists( 'is_shop' ) && ( is_shop() || is_product_taxonomy() ) ) {
rx_theme_sidebar( 'rx-woocommerce-shop-sidebar' );
return;
}
}
endif;
/**
* Add widgets inside post content after selected paragraphs.
*
* This can insert active widget areas after paragraph 1, 3, and 5.
*/
add_filter( 'the_content', 'rx_theme_insert_widgets_inside_content', 20 );
if ( ! function_exists( 'rx_theme_insert_widgets_inside_content' ) ) :
/**
* Insert widget areas inside single post content.
*
* @param string $content Post content.
* @return string
*/
function rx_theme_insert_widgets_inside_content( $content ) {
if ( is_admin() || ! is_singular( 'post' ) || ! in_the_loop() || ! is_main_query() ) {
return $content;
}
$insertions = array(
1 => 'rx-inside-post-p1',
3 => 'rx-inside-post-p3',
5 => 'rx-inside-post-p5',
);
foreach ( $insertions as $paragraph_number => $sidebar_id ) {
if ( is_active_sidebar( $sidebar_id ) ) {
$widget_html = rx_theme_get_sidebar_html( $sidebar_id, 'rx-inside-content-widget' );
$content = rx_theme_insert_after_paragraph( $widget_html, $paragraph_number, $content );
}
}
return $content;
}
endif;
/**
* Get sidebar HTML as string.
*
* @param string $sidebar_id Sidebar ID.
* @param string $class Extra class.
* @return string
*/
if ( ! function_exists( 'rx_theme_get_sidebar_html' ) ) :
function rx_theme_get_sidebar_html( $sidebar_id, $class = '' ) {
if ( ! is_active_sidebar( $sidebar_id ) ) {
return '';
}
ob_start();
rx_theme_sidebar( $sidebar_id, $class );
return ob_get_clean();
}
endif;
/**
* Insert HTML after a selected paragraph.
*
* @param string $insertion HTML to insert.
* @param int $paragraph_id Paragraph number.
* @param string $content Content.
* @return string
*/
if ( ! function_exists( 'rx_theme_insert_after_paragraph' ) ) :
function rx_theme_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
if ( empty( $insertion ) || empty( $content ) ) {
return $content;
}
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ( $paragraphs as $index => $paragraph ) {
if ( trim( $paragraph ) ) {
$paragraphs[ $index ] .= $closing_p;
}
if ( (int) $paragraph_id === ( $index + 1 ) ) {
$paragraphs[ $index ] .= $insertion;
}
}
return implode( '', $paragraphs );
}
endif;
/**
* Add RX body classes based on active sidebars.
*/
add_filter( 'body_class', 'rx_theme_sidebar_body_classes' );
if ( ! function_exists( 'rx_theme_sidebar_body_classes' ) ) :
/**
* Add sidebar-related body classes.
*
* @param array $classes Body classes.
* @return array
*/
function rx_theme_sidebar_body_classes( $classes ) {
if ( is_active_sidebar( 'rx-primary-sidebar' ) ) {
$classes[] = 'rx-has-primary-sidebar';
}
if ( is_active_sidebar( 'rx-secondary-sidebar' ) ) {
$classes[] = 'rx-has-secondary-sidebar';
}
if ( is_active_sidebar( 'rx-sticky-sidebar' ) ) {
$classes[] = 'rx-has-sticky-sidebar';
}
if ( is_active_sidebar( 'rx-mobile-offcanvas-sidebar' ) ) {
$classes[] = 'rx-has-mobile-offcanvas-sidebar';
}
if ( is_singular( 'post' ) && is_active_sidebar( 'rx-single-post-sidebar' ) ) {
$classes[] = 'rx-has-single-post-sidebar';
}
if ( is_page() && is_active_sidebar( 'rx-page-sidebar' ) ) {
$classes[] = 'rx-has-page-sidebar';
}
if ( class_exists( 'WooCommerce' ) && is_active_sidebar( 'rx-woocommerce-shop-sidebar' ) ) {
$classes[] = 'rx-has-woocommerce-sidebar';
}
return $classes;
}
endif;
/**
* Optional fallback content for empty sidebars during Customizer preview.
*/
if ( ! function_exists( 'rx_theme_sidebar_fallback' ) ) :
/**
* Sidebar fallback.
*
* @param string $message Fallback message.
* @return void
*/
function rx_theme_sidebar_fallback( $message = '' ) {
if ( ! current_user_can( 'edit_theme_options' ) ) {
return;
}
if ( empty( $message ) ) {
$message = esc_html__( 'Add widgets here from Appearance > Widgets.', 'rx-theme' );
}
echo '<section class="widget rx-widget rx-widget-fallback">';
echo '<h2 class="widget-title rx-widget-title">' . esc_html__( 'Widget Area', 'rx-theme' ) . '</h2>';
echo '<p>' . esc_html( $message ) . '</p>';
echo '</section>';
}
endif;
/**
* Render sidebar with fallback for admins.
*
* @param string $sidebar_id Sidebar ID.
* @param string $class Extra class.
* @return void
*/
if ( ! function_exists( 'rx_theme_sidebar_with_fallback' ) ) :
function rx_theme_sidebar_with_fallback( $sidebar_id, $class = '' ) {
if ( is_active_sidebar( $sidebar_id ) ) {
rx_theme_sidebar( $sidebar_id, $class );
return;
}
if ( current_user_can( 'edit_theme_options' ) && is_customize_preview() ) {
echo '<aside class="rx-sidebar-wrap rx-sidebar-empty">';
rx_theme_sidebar_fallback();
echo '</aside>';
}
}
endif;
/**
* Add block editor support for widgets.
*/
add_action( 'after_setup_theme', 'rx_theme_widget_block_support' );
if ( ! function_exists( 'rx_theme_widget_block_support' ) ) :
/**
* Add theme support related to widgets.
*
* @return void
*/
function rx_theme_widget_block_support() {
add_theme_support( 'widgets' );
add_theme_support( 'html5', array( 'script', 'style' ) );
}
endif;
/**
* Disable block widgets only if a constant says so.
*
* Add this in wp-config.php or functions.php if needed:
* define( 'RX_DISABLE_BLOCK_WIDGETS', true );
*/
if ( defined( 'RX_DISABLE_BLOCK_WIDGETS' ) && RX_DISABLE_BLOCK_WIDGETS ) {
add_filter( 'use_widgets_block_editor', '__return_false' );
}
/**
* Shortcode to display any RX sidebar.
*
* Usage:
* [rx_sidebar id="rx-primary-sidebar"]
*/
add_shortcode( 'rx_sidebar', 'rx_theme_sidebar_shortcode' );
if ( ! function_exists( 'rx_theme_sidebar_shortcode' ) ) :
/**
* Sidebar shortcode.
*
* @param array $atts Shortcode attributes.
* @return string
*/
function rx_theme_sidebar_shortcode( $atts ) {
$atts = shortcode_atts(
array(
'id' => 'rx-primary-sidebar',
'class' => '',
),
$atts,
'rx_sidebar'
);
$sidebar_id = sanitize_key( $atts['id'] );
$class = sanitize_html_class( $atts['class'] );
if ( ! is_active_sidebar( $sidebar_id ) ) {
return '';
}
return rx_theme_get_sidebar_html( $sidebar_id, $class );
}
endif;
/**
* Template tag: before content widgets.
*
* @return void
*/
if ( ! function_exists( 'rx_theme_before_content_widgets' ) ) :
function rx_theme_before_content_widgets() {
rx_theme_sidebar( 'rx-ad-before-content' );
if ( is_singular( 'post' ) ) {
rx_theme_sidebar( 'rx-before-single-content' );
}
if ( is_page() ) {
rx_theme_sidebar( 'rx-before-page-content' );
}
if ( is_home() || is_archive() || is_search() ) {
rx_theme_sidebar( 'rx-blog-before-posts' );
}
}
endif;
/**
* Template tag: after content widgets.
*
* @return void
*/
if ( ! function_exists( 'rx_theme_after_content_widgets' ) ) :
function rx_theme_after_content_widgets() {
rx_theme_sidebar( 'rx-ad-after-content' );
if ( is_singular( 'post' ) ) {
rx_theme_sidebar( 'rx-after-single-content' );
rx_theme_sidebar( 'rx-related-posts-area' );
rx_theme_sidebar( 'rx-author-box-area' );
rx_theme_sidebar( 'rx-medical-disclaimer-area' );
rx_theme_sidebar( 'rx-evidence-references-area' );
rx_theme_sidebar( 'rx-doctor-review-area' );
rx_theme_sidebar( 'rx-health-cta-area' );
}
if ( is_page() ) {
rx_theme_sidebar( 'rx-after-page-content' );
}
if ( is_home() || is_archive() || is_search() ) {
rx_theme_sidebar( 'rx-blog-after-posts' );
}
}
endif;
/**
* Automatically hook some widget zones into common theme locations.
*
* You can call do_action( 'rx_before_content' ) and do_action( 'rx_after_content' )
* inside your templates.
*/
add_action( 'rx_before_content', 'rx_theme_before_content_widgets', 10 );
add_action( 'rx_after_content', 'rx_theme_after_content_widgets', 10 );
/**
* WooCommerce hook integrations.
*/
if ( class_exists( 'WooCommerce' ) ) {
add_action(
'woocommerce_before_shop_loop',
function() {
rx_theme_sidebar( 'rx-woocommerce-before-shop-loop' );
},
5
);
add_action(
'woocommerce_after_shop_loop',
function() {
rx_theme_sidebar( 'rx-woocommerce-after-shop-loop' );
},
50
);
}
/**
* Add simple inline CSS for default layout support.
*
* You can move this CSS into assets/css/theme.css later.
*/
add_action( 'wp_head', 'rx_theme_sidebar_inline_css', 30 );
if ( ! function_exists( 'rx_theme_sidebar_inline_css' ) ) :
/**
* Sidebar CSS.
*
* @return void
*/
function rx_theme_sidebar_inline_css() {
?>
<style id="rx-theme-sidebars-css">
.rx-sidebar-wrap {
box-sizing: border-box;
width: 100%;
margin-bottom: 24px;
}
.rx-widget {
box-sizing: border-box;
margin-bottom: 24px;
padding: 20px;
background: #fff;
border: 1px solid rgba(0,0,0,.08);
border-radius: 12px;
overflow-wrap: break-word;
}
.rx-widget-title {
margin-top: 0;
margin-bottom: 14px;
font-size: 20px;
line-height: 1.3;
}
.rx-footer-widgets {
width: 100%;
clear: both;
}
.rx-footer-columns {
display: grid;
grid-template-columns: repeat(4, minmax(0, 1fr));
gap: 24px;
}
.rx-footer-column .rx-sidebar-wrap {
margin-bottom: 0;
}
.rx-header-widgets {
width: 100%;
clear: both;
}
.rx-inside-content-widget {
margin: 28px 0;
}
.rx-has-sticky-sidebar .rx-sidebar-wrap-rx-sticky-sidebar {
position: sticky;
top: 24px;
}
@media (max-width: 1024px) {
.rx-footer-columns {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
}
@media (max-width: 767px) {
.rx-footer-columns {
grid-template-columns: 1fr;
}
.rx-widget {
padding: 16px;
}
}
</style>
<?php
}
endif;
How to use in template files
In sidebar.php
<?php
defined( 'ABSPATH' ) || exit;
if ( is_singular( 'post' ) ) {
rx_theme_single_sidebar();
} elseif ( is_page() ) {
rx_theme_page_sidebar();
} elseif ( class_exists( 'WooCommerce' ) && function_exists( 'is_woocommerce' ) && is_woocommerce() ) {
rx_theme_woocommerce_sidebar();
} else {
rx_theme_archive_widgets();
}
In header.php
<?php rx_theme_header_widgets(); ?>
In footer.php
<?php rx_theme_footer_widgets(); ?>
Before main content
<?php do_action( 'rx_before_content' ); ?>
After main content
<?php do_action( 'rx_after_content' ); ?>
Display any sidebar manually
<?php rx_theme_sidebar( 'rx-primary-sidebar' ); ?>
Use sidebar with shortcode
[rx_sidebar id="rx-primary-sidebar"]
This file gives your RX Theme a very large and flexible sidebar/widget system for blog, medical article, homepage, footer, ads, WooCommerce, mobile, and future custom post type areas.