Beginner trying to register widgets

Amazing! Without anyone listening, I just rubber-ducked myself into a solution. The names of my template files were wrong. I’ve been tearing my hair out for two hours over a stupid, simple error that is my fault. I’m going to leave this here anyway. Thank you. (Feel free to delete.)

This is probably not CP specific, but good help is more frequent here, so …

I’m building a CP theme from scratch, because existing themes are too complex and I don’t need all the conditionals. I have a basic layout and I am trying to widgetise the sidebar and footer. I have this code in functions.php:

// Register sidebars
add_action( 'widgets_init', 'sidebar_register' );
function sidebar_register() {

    register_sidebar(
    	array(
        'name'          => __( 'Right Sidebar', 'etp' ),
        'description'   => __( 'Sidebar on the Right'),
        'id'            => 's-r',
        'class'         => ' ',
        'before_widget' => ' <div class="sidebar">',
        'after_widget'  => '</div>',
        'before_title'  => '<h2 class="widgettitle">',
        'after_title'   => '</h2>',
        )
    );

    register_sidebar(
    	array(
        'name'          => __( 'Footer Sidebar', 'etp' ),
        'description'   => __( 'Footer Sidebar' ),
        'id'            => 's-f',
        'class'         => ' ',
        'before_widget' => ' <div class="sidebar">',
        'after_widget'  => '</div>',
        'before_title'  => '<h2 class="widgettitle">',
        'after_title'   => '</h2>',
        )
    );

}

It seems to work, because I can see the widget areas and can add arbitrary text to them.

I added <?php get_sidebar('s-r'); ?> and <?php get_sidebar( 's-f'); ?> to index.php and now the Right sidebar displays what seem to be WP/CP default widgets (Search, Pages, Archive, Categories) and there is nothing at all in the Footer Sidebar.

I have template files called sidebar-right.php and sidebar-footer.php that contain <?php dynamic_sidebar( 's-r' ); ?> and <?php dynamic_sidebar( 's-f' ); ?>

SOLUTION: The files should be called sidebar-s-r.php and sidebar-s-f.php

I have tried everything I can think of. Using the sidebar name instead of id, expanding the id to make it longer, various other permutations.

I have no idea where the default widgets are coming from. The theme I am working on is the only theme in /themes.

I am completely stuck here and hope someone will be able to set me straight.

Thank you

Jeremy

2 Likes

First tip would to NOT use small single hyphenated ids ...-s-r is just a bad idea and may easily be conflicting. Try rightside or something more readable. Remember that humans must be able to follow what your code does. Being literal helps.

For dynamic sidebars you do not need a file. The only file that is needed in WP/CP for a sidebar is the actual file named sidebar.php

/**
 * The template for the sidebar containing the main widget area
 *
 * @package    ClassicPress
 * @subpackage themename
 * @since      themename 1.0.0
 */
?>
<div class="sidebar-widget-section">
				
    <?php if ( is_active_sidebar( 'sidebar-primary' ) ) : ?>

	<aside id="secondary" class="sidebar widget-area" role="complementary">
		
		<?php dynamic_sidebar( 'sidebar-primary' ); ?>

	</aside><!-- .sidebar .widget-area -->
    
	<?php endif; ?>

</div>
1 Like

Thanks for the advice on naming; you are probably right and it is early enough that I can make that change easily.

Perhaps you can explain where CP gets a sidebar from if my theme is the only one in Themes? Before I got mine working there was one with a bunch of default widgets in it.

The default widgets are part of ClassicPress and can be used by any sidebar.
I also do have a file for each custom sidebar in my themes (sidebar-name.php as an example)
I would say you are on the right track, they need to be registered in functions.php, then the file is called in your page template <?php get_sidebar('name'); ?>.

To be more clear: you have a sidebar.php in your theme, (that can be also sidebar-right.php or *-left.php) and you can use default core widgets without having to code them into the theme. You need to register your own specific custom widgets in functions.php or in a plugin (if you want other themes to use them also) only when your widget supplies a feature that core does not. When you do that you are able to use core widgets + the ones you code yourself.

1 Like

Hi,

Thanks for sharing this, we will keep that in mind.

Thanks

1 Like