2

Any reason this code does not work in the admin section (specifically when ajaxing but the issue persists on other pages) of WordPress but works just fine

ob_start();
    dynamic_sidebar('frontpage_widgets');
$content = ob_get_clean();

print_r($content); // nothing 

the sidebars are set from what I see (because I have called my code which registered the sidebars)

print_r($GLOBALS['wp_registered_sidebars'])

    Array
    (
        [frontpage_widgets] => Array
            (
                [name] => Frontpage Widgets
                [id] => frontpage_widgets
                [description] => Widgets for the Frontpage Widgetspage
                [class] => 
                [before_widget] => 
                ...

Is there something preventing the widgets from displaying in the admin section?

Edit - I seems that the widgets are not being set globally: $wp_registered_widgets is empty

  • 1
    Can you add a little more context to your question? Where is this code? Is it for a plugin or theme? The more information you provide, the better chance your question will be answered. – Nathan Johnson Apr 7 '17 at 18:08
  • 1
    Just to clarify, calling dynamic_sidebar('frontpage_widgets'); does work perfectly fine for you on the frontend? – Nicolai Apr 7 '17 at 18:08
  • 1
    on a new installation, dynamic_sidebar works in admin with the same result as in frontend. maybe you have plugins or theme which block that, try to desactive them all – mmm Apr 7 '17 at 19:46
  • 1
    That's strange. Your code works fine in my admin panel. – Jack Johansson Apr 7 '17 at 20:49
  • 2
    Exactly where and when are you running this? I think you are early. – iyrin Apr 8 '17 at 8:26
2
+100

Yes, you have to make sure to hook this function at some point after the sidebars are registered. It's not quite explained, but implied in the codex that this function dynamic_sidebar( $index ) expects them to be registered and loaded as registered by the time it runs. Otherwise, it won't have anything to match the $index argument, which in your case is 'frontpage_widgets'.

If your sidebars were registered by number, they should be retrieved by number. If they had names when you registered them, use their names to retrieve them.

If I had to guess off hand, I'd say wp_loaded() is the earliest action you can safely hook it to. Or if you need to, you can hook into wp_register_sidebar_widget to hook into the specific widget you are testing for.

I assume you were just using the object buffering for debugging purposes. You can almost always find an appropriate action to hook or area of your template to edit. I'm finding that understanding the order in which actions are fired is invaluable. Take a look at this answer by birgire as a good reference for when different actions are fired. This will make debugging a lot easier. Usually, finding the proper place to hook will be fairly intuitive depending on what you're working with.

-1

Try to logout the admin, and then see the code behavior. Sometimes, due to login session this behavior is seen. If you provide more details to your question, it might be helpful to give better answer.

  • "Sometimes, due to login session this behavior is seen": Can you please provide any evidence to support that? Why would login session affect dynamic_sidebar()? – Fayaz Apr 9 '17 at 8:59
  • Sometimes, themes display content based on admin roles. So I suggested to do logout and refresh the same page. – Atul Jindal Apr 9 '17 at 9:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.