• Resolved webrightnow

    (@webrightnow)


    Scratching my head a bit here…

    I did what I always do: created a custom widget area by adding the following code to functions.php:

    register_sidebar( array(
    	'name'          => __( 'Header Right' ),
    	'id'            => 'header-right',
    	'description'   => __( 'Widgets for the right side of the header.' ),
    	'before_widget' => '<div id="%1$s" class="%2$s">',
    	'after_widget'  => '</div>',
    ) );
    

    The widget area is showing correctly in the admin and I added two widgets, a plain text one and a search bar.

    Now in the header template I am calling the widget area thus:

    <div class="st-header-right">
    	<?php if ( is_active_sidebar( 'header-right' ) ) : ?>
    		<?php dynamic_sidebar( 'header-right' ); ?>
    	<?php endif; ?>
    </div>

    It should work, right? I doesn’t. The div is loaded, but the content is empty.

    What am I missing here??

    • This topic was modified 3 years, 8 months ago by webrightnow.
    • This topic was modified 3 years, 8 months ago by webrightnow.

    The page I need help with: [log in to see the link]

Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter webrightnow

    (@webrightnow)

    Well, interestingly if I put one of the other registered sidebars there, it shows fine. So I took one that was not used anywhere else and moved it to the header area. I’m still curious to find out why the theme’s original sidebar shows and my custom one doesn’t. The code in functions.php is exactly the same. There must be somewhere else in the theme that my custom sidebar needs to be activated…

    Moderator bcworkz

    (@bcworkz)

    Did you register the sidebar from a callback added to the ‘widgets_init’ action? If not, your code probably executed too early, before WP was completely stable.

    function wpdocs_theme_slug_widgets_init() {
    	register_sidebar( array(
    		'name'          => __( 'Header Right' ),
    		'id'            => 'header-right',
    		'description'   => __( 'Widgets for the right side of the header.' ),
    		'before_widget' => '<div id="%1$s" class="%2$s">',
    		'after_widget'  => '</div>',
    	) );
    }
    add_action( 'widgets_init', 'wpdocs_theme_slug_widgets_init' );
    Thread Starter webrightnow

    (@webrightnow)

    Yes, I simply placed the sidebar in amongst all the other ones listed in functions.php, at the top of the list in fact. That’s what I always do (unless the theme calls the sidebars in a separate file, but this one doesn’t, they are all in functions.php). Then the other sidebars are called, then the widgets_ini action is called with

    add_action( 'widgets_init', 'ashe_widgets_init' );

    So as far as I can see, my sidebar should behave like all the other ones, but it doesn’t… It remains invisible in the frontend.

    As I said, I solved it by reassigning an existing sidebar, but it remains a mystery to me.

    Moderator bcworkz

    (@bcworkz)

    Since you found a workaround, it no longer matters, but FWIW, the code I posted which includes the hook works fine in my twenty twenty-one child theme. Aside from a different function name, it ought to be functionally equivalent. And it works as expected on my site when I add your template code to my template.

    I suspect something odd about your theme or some strange plugin conflict unique to your site. But no matter now.

    Thread Starter webrightnow

    (@webrightnow)

    Thanks for your help anyway, much appreciated!

    Clear the cache, both browser and server part, it may work
    Kian William

    Thread Starter webrightnow

    (@webrightnow)

    Ah well, I did wonder about the cache. Unfortunately this is [my host’s] shared hosting package with cache in place for CSS and images, though PHP shouldn’t be affected.

    It’s also one of the slowest and most unreliable hosting solutions I have ever worked with: they call it “Managed WordPress” and I honestly cannot recommend it. Constant 503 errors, my IP keeps getting blocked so I’m having to keep switching my VPN connection to get back in. They told me their firewall doesn’t block connections, but clearly that’s nonsense. Really awful hosting.

    Now I know they give you an option to flush the cache from within the WP admin, so next time I’ll try that, but for now I’ll stick with my workaround.

    Thanks

    • This reply was modified 3 years, 7 months ago by bcworkz. Reason: removed host's name
    Moderator bcworkz

    (@bcworkz)

    I’m sorry you’re unhappy with your host. In the future, please avoid reviewing hosts in these forums, either critical or praise. Such content tends to attract trolls and flame wars which we do not want here. To avoid such content, topics involving host reviews tend to be closed or removed as a preemptive measure. Your account is not in any kind of trouble, this is just fair warning.

    Regarding caches you may have no control over, there are a number of “cache busting” techniques you could employ. For example: https://themetry.com/cache-busting-wordpress/
    Searching “WordPress cache busting” will bring up many more.

    Thread Starter webrightnow

    (@webrightnow)

    Thanks! I will try that next time. I would delete the host’s name from my previous comment but it won’t let me edit it now. Will keep it in mind though.

    Moderator bcworkz

    (@bcworkz)

    I edited out the host’s name for you. Thanks for your consideration.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Why won’t my custom widget area show?’ is closed to new replies.