0

A custom page template which has been in use for 3 or 4 years which rendered a custom sidebar has suddenly stopped rendering it. I can't for the life work out what the problem is.

/* functions.php */

/**
 * Register Areas sidebars and widgetized areas.
 *
 */
function my_widgets_init() {

    register_sidebar( array(
        'name'          => 'My Sidebar',
        'id'            => 'my_sidebar',
        'before_widget' => '<div>',
        'after_widget'  => '</div><div>&nbsp;</div>',
        'before_title'  => '<h2 class="rounded">',
        'after_title'   => '</h2>',
    ) );

}
add_action( 'widgets_init', 'my_widgets_init' );

/* wordpress page template file*/
<?php if ( is_active_sidebar( 'my_sidebar' ) ) : ?>
    <div>
        <?php dynamic_sidebar( 'my_sidebar' ); ?>
    </div>
<?php endif; ?>

In the Wordpress dashboard, 3 or 4 widgets are assigned to the area as would be expected. But calling dynamic_sidebar() renders an empty string.

Any ideas?

* UPDATE *

This is the output of the debugging code provided in the first response.

Sidebar ID: new_sidebar ( Active)
Sidebar Found
4 Widgets Found

All Sidebars Widgets:
Array
(
    [wp_inactive_widgets] => Array
        (
            [0] => calendar-4
            [1] => rss-3
            [2] => em_calendar-4
            [3] => recent-posts-2
        )

    [hca_alert_area] => Array
        (
        )

  [SNIP - other sidebars]

    [new_sidebar] => Array
        (
            [0] => search-2
            [1] => nav_menu-2
            [2] => em_widget-2
            [3] => recent-posts-3
        )

    [footer-sidebar] => Array
        (
            [0] => nav_menu-3
        )

    [array_version] => 3
)

I've spent a crazy amount of time trying to work out the problem. The site uses a no-longer supported version of the Cherry Framework template. I'm reluctant to update the entire theme purely because of this glitch if I can fix it.

2
  • 1
    you are checking 'is_active_sidebar' with a different id than used in 'dynamic _sidebar' - is that on purpose? have you done any updates or changed any plugins
    – Michael
    Commented Jul 6, 2019 at 20:09
  • That was a typo. Nope nothing has changed. Wordpress is recognising that the sidebar exists, but appears not to be finding any widgets.
    – fred2
    Commented Jul 6, 2019 at 20:15

2 Answers 2

1

In a case like this, you're probably going to need to check the conditions in dynamic_sidebar to find out what is not happening.

if (isset($_GET['debugsidebar'])) {add_action('init', 'debug_sidebar');}  
function debug_sidebar() {

    $id = $_GET['debugsidebar'];
    $sidebarswidgets = get_option('sidebars_widgets');

    if (is_active_sidebar($id)) {$status = " Active";} else {$status = "Inactive";}
    echo "Sidebar ID: ".$id." (".$status.")<br>";
    if (array_key_exists($id, $sidebarswidgets)) {
        $found = "Sidebar Found";
        if (is_array($sidebarswidgets[$id])) {
            $widgets = count($sidebarswidgets[$id])." Widgets Found";
        } else {$widgets = "Widget Array not Found!";}
        echo $found."<br".$widgets."<br>";
    else {echo "Sidebar Not Found<br>";}

    echo "<br>All Sidebars Widgets: ".print_r($sidebarswidgets,true)."<br>"; 
    exit;
}

Which you put in your theme functions.php and then can check via http://example.com/?debugsidebar=my_sidebar

There is a small difference in the bugout checks is is_active_sidebar and dynamic_sidebar

From is_active_sidebar function:

! empty( $sidebars_widgets[ $index ] );

From dynamic_sidebar function:

if ( empty( $wp_registered_sidebars[ $index ] ) || empty( $sidebars_widgets[ $index ] ) || ! is_array( $sidebars_widgets[ $index ] ) ) {

Since the middle condition is the same, this means that either the sidebar is not being registered correctly (unlikely as that seems fine) - but more likely, the data for that key is not an array of widgets. You will find out from running the debug function.

It may possibly show you a mismatch or corrupt data in the sidebars_widgets option value (If needs be you can that check against a value from you wp_options table backup from when it was working.)

4
  • Brilliant - thanks so much for this, it's perfect.
    – fred2
    Commented Jul 8, 2019 at 16:10
  • Although this debugging code is really helpful, I am still stuck with regard to finding out why no widgets are output to the browser. Nothing seems to be out of place or corrupt. See additional data added to the original question.
    – fred2
    Commented Jul 31, 2019 at 22:00
  • 1
    You could create a copy of the function dynamic_sidebar and call it dynamic_sidebar_test or whatever, and call that in your template, then debug it line by line by adding output in the copied function for each condition to track down where it is failing.
    – majick
    Commented Aug 2, 2019 at 1:04
  • Thanks for that tip. Your two suggestions allowed me to diagnose the problem, which was a single line in the now unsupported 'Cherry Framework' code. I'm putting the exact solution in another answer, in case it helps anybody, but I'm marking this as the accepted answer.
    – fred2
    Commented Aug 13, 2019 at 14:15
0

The following lines in the obsolete Cherry Plugin (Version 9.2.8.1, which does not support PHP 7+ in theory) caused sidebars to be hidden all the time.

In file: wordpress/wp-content/plugins/cherry-plugin/includes/widgets/widgets-manager.php

Comment out the following code:

add_filter( 'widget_display_callback', 'maybe_hide_widget', 10, 3 );
function maybe_hide_widget( $instance, $widget_object, $args ) {
if ( ! check_widget_visibility( $args['widget_id'] ) )
    return false;
    return $instance;
}

It would also be possible to edit the check_widget_visibility() function (obviously) in the same file so that it did not always return false and continued to function as intended. However, for my purposes, simply disabling the entire mechanism was good enough.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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