1
votes

I've created a custom theme after viewing some tutorials and I'm having some issues with widgets saving/displaying. Whenever I drag and drop a widget in the admin panel, if i refresh the page and it just disappears and nothing is displayed. I've googled for hours and I'm not closer. Hopefully someone here has seen the same issue

This area always shows up as empty once I save and reload from the admin page (none of the widgets are active either) enter image description here

Things I've checked for:

  1. Verify that the ID is not in Caps
  2. Verify < ?php get_sidebar(); ? > is included in on page.php
  3. Disabled all plugins
  4. Switch themes to wordpress default to confirm that widgets are actually working
  5. Enabled Accessibility mode (doesn't show up after save)
  6. Verify latest version of Jquery

I've included this in my functions.php:

// Register Widget
function ecma_widgets_init() {
    register_sidebar( array(
        'name'          => __( 'Main Widget Area', 'ecma' ),
        'id'            => 'custom1',
        'description'   => __( 'Appears in the footer section of the site.', 'ecma' ),
        'before_widget' => '<div>',
        'after_widget'  => '</div>',
        'before_title'  => '<h3 class="blue">',
        'after_title'   => '</h3>',
    ) );

    register_sidebar( array(
        'name'          => __( 'Secondary Widget Area', 'ecma' ),
        'id'            => 'custom2',
        'description'   => __( 'Appears on posts and pages in the sidebar.', 'ecma' ),
        'before_widget' => '<div>',
        'after_widget'  => '</div>',
        'before_title'  => '<h3 class="blue">',
        'after_title'   => '</h3>',
    ) );
}
add_action( 'widgets_init', 'ecma_widgets_init' );

Sidebar.php

<?php
/**
 * The sidebar containing the secondary widget area, displays on posts and pages.
 *
 * If no active widgets in this sidebar, it will be hidden completely.
 *
 */

if ( is_active_sidebar( 'home_right_sidebar' ) ) : ?>
            <div id="sidebar">
                <?php dynamic_sidebar( 'custom1' ); ?>
            </div><!-- .widget-area -->
<?php endif; ?>         
2
Did you hit save on the widgets after dropping them on the widget areas? :) I copy pasted your code into the functions.php file in 2013 on a default 3.6 installation and it worked just fine.Andrew Bartel
Yep I hit save on the widgets as well :). I built the theme from scratch so I must be missing something somewhereRomeNYRR
I would suggest using a starter them like underscores for a custom theme: underscores.meAndrew Bartel
I've spent days building this theme from scratch, but I'll check out underscores. ThanksRomeNYRR
Andrew, you can post your reply as an answer for credit. I used the starter them and was able to integrate my CSS and HTML pretty easily. Thank you sir, my widgets are now workingRomeNYRR

2 Answers

2
votes

I found that $control_ops['id_base'] and first argument of parent::__construct() should be identical.

class MostRecentWidget extends WP_Widget {

    public function __construct() {
        $widget_ops = array(
            'classname' => 'most-recent-widget',
            'description' => 'Displays Most Recent in sidebar'
        );
        $control_ops = array(
            'id_base' => 'most-recent-widget'
        );
        parent::__construct('most-recent-widget', 'Most Recent Widget', $widget_ops, $control_ops);
    }
}
0
votes

While writing themes from scratch is possible, in my opinion it's must easier to use starter themes like underscores. That way, much of the basic structure is taken care of, and you can be sure that anything that breaks is additional code you put on top of it, making troubleshooting a lot easier.