dynamic_sidebar() returns false in admin section - WordPress Development Stack Exchange most recent 30 from wordpress.stackexchange.com 2019-08-21T12:03:43Z https://wordpress.stackexchange.com/feeds/question/262569 http://www.creativecommons.org/licenses/by-sa/3.0/rdf https://wordpress.stackexchange.com/q/262569 2 dynamic_sidebar() returns false in admin section rugbert https://wordpress.stackexchange.com/users/19536 2017-04-05T16:55:37Z 2017-04-09T23:37:27Z <p>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</p> <pre><code>ob_start(); dynamic_sidebar('frontpage_widgets'); $content = ob_get_clean(); print_r($content); // nothing </code></pre> <p>the sidebars are set from what I see (because I have called my code which registered the sidebars)</p> <p><code>print_r($GLOBALS['wp_registered_sidebars'])</code></p> <pre><code> Array ( [frontpage_widgets] =&gt; Array ( [name] =&gt; Frontpage Widgets [id] =&gt; frontpage_widgets [description] =&gt; Widgets for the Frontpage Widgetspage [class] =&gt; [before_widget] =&gt; ... </code></pre> <p>Is there something preventing the widgets from displaying in the admin section?</p> <p>Edit - I seems that the widgets are not being set globally: $wp_registered_widgets is empty</p> https://wordpress.stackexchange.com/questions/262569/-/262990#262990 -1 Answer by Atul Jindal for dynamic_sidebar() returns false in admin section Atul Jindal https://wordpress.stackexchange.com/users/58957 2017-04-09T07:35:52Z 2017-04-09T07:35:52Z <p>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.</p> https://wordpress.stackexchange.com/questions/262569/-/263044#263044 2 Answer by iyrin for dynamic_sidebar() returns false in admin section iyrin https://wordpress.stackexchange.com/users/33393 2017-04-09T23:24:49Z 2017-04-09T23:37:27Z <p>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 <a href="https://codex.wordpress.org/Function_Reference/dynamic_sidebar" rel="nofollow noreferrer"><code>dynamic_sidebar( $index )</code></a> expects them to be registered and loaded as registered by the time it runs. Otherwise, it won't have anything to match the <code>$index</code> argument, which in your case is <code>'frontpage_widgets'</code>. </p> <blockquote> <p>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.</p> </blockquote> <p>If I had to guess off hand, I'd say <a href="https://codex.wordpress.org/Plugin_API/Action_Reference/wp_loaded" rel="nofollow noreferrer"><code>wp_loaded()</code></a> is the earliest action you can safely hook it to. Or if you need to, you can hook into <a href="https://codex.wordpress.org/Plugin_API/Action_Reference/wp_register_sidebar_widget" rel="nofollow noreferrer"><code>wp_register_sidebar_widget</code></a> to hook into the specific widget you are testing for.</p> <p>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 <a href="https://wordpress.stackexchange.com/a/162869/33393">answer by birgire</a> 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. </p>