Quick custom-theme updates using WordPress theme code

If you have a site that looks modern but was created more than a couple years ago, it is possible that the coding standards used back then still work to the naked eye but are throwing errors in the background. To find the errors, enable debug mode, as outlined here.

On one of my sites, I was getting these errors:

Notice: register_sidebar was called incorrectly. No id was set in the arguments array for the "Sidebar" sidebar. Defaulting to "sidebar-1". Manually set the id to "sidebar-1" to silence this notice and keep existing sidebar content. Please see Debugging in WordPress for more information. (This message was added in version 4.2.0.) in /nas/content/live/amrendev/wp-includes/functions.php on line 5225

Notice: register_sidebar was called incorrectly. No id was set in the arguments array for the "Footer" sidebar. Defaulting to "sidebar-2". Manually set the id to "sidebar-2" to silence this notice and keep existing sidebar content. Please see Debugging in WordPress for more information. (This message was added in version 4.2.0.) in /nas/content/live/amrendev/wp-includes/functions.php on line 5225

The code throwing those errors hadn’t been touched in years:

if ( function_exists('register_sidebar') ) {
	register_sidebar(
		array('name'=>'Sidebar',
			'before_widget' => '<section id="%1$s" class="widget %2$s">',
			'after_widget'  => '</section>',
			'before_title'  => '<h2 class="widget-title">',
			'after_title'   => '</h2>',
		)
	);
	register_sidebar(
		array('name'=>'Footer',
			'before_widget' => '<section id="%1$s" class="widget %2$s">',
			'after_widget'  => '</section>',
			'before_title'  => '<h2 class="widget-title">',
			'after_title'   => '</h2>',
		)
	);
}

After staring at this code for a few minutes and trying some ineffective changes, it occurred to me that the basic layout of this site was similar to that of the default Twenty Sixteen theme, which is still kept up-to-date by Automattic / WordPress. Therefore, why not try replacing this older code on my site with the newer code from Twenty Sixteen? That newer code turned out to be in the functions.php file:

function themename_widgets_init() {
	register_sidebar(
		array(
			'name'          => __( 'Sidebar', 'themename' ),
			'id'            => 'sidebar-1',
			'description'   => __( 'Add widgets here to appear in your sidebar.', 'themename' ),
			'before_widget' => '<section id="%1$s" class="widget %2$s">',
			'after_widget'  => '</section>',
			'before_title'  => '<h2 class="widget-title">',
			'after_title'   => '</h2>',
		)
	);

	register_sidebar(
		array(
			'name'          => __( 'Content Bottom 1', 'themename' ),
			'id'            => 'sidebar-2',
			'description'   => __( 'Appears at the bottom of the content on posts and pages.', 'themename' ),
			'before_widget' => '<section id="%1$s" class="widget %2$s">',
			'after_widget'  => '</section>',
			'before_title'  => '<h2 class="widget-title">',
			'after_title'   => '</h2>',
		)
	);

	register_sidebar(
		array(
			'name'          => __( 'Content Bottom 2', 'themename' ),
			'id'            => 'sidebar-3',
			'description'   => __( 'Appears at the bottom of the content on posts and pages.', 'themename' ),
			'before_widget' => '<section id="%1$s" class="widget %2$s">',
			'after_widget'  => '</section>',
			'before_title'  => '<h2 class="widget-title">',
			'after_title'   => '</h2>',
		)
	);
}
add_action( 'widgets_init', 'themename_widgets_init' );

Obviously, everywhere it says themename it used to say twentysixteen. I replaced twentysixteen with my theme name, copied the code, and pasted it into the functions.php file on my site. (Note that I was making these changes on a development copy, not on the live site. Making code changes on a live site can lead to sorrow.)

Once I did this, I just had to change one reference from “Footer” to “Content Bottom 1”, although I could have changed “Content Bottom 1” to “Footer” in functions.php to save that step.

Because WordPress coding standards evolve over time, you should check any custom theme code you are using to ensure that it is not throwing any errors. If it is throwing errors and there is a default WordPress theme that generically has the same layout as your custom theme, you might be able to copy the WordPress theme code to your custom theme to bring it up to date … at least where there are corresponding sections of code.

Just be aware than file locations in newer versions of WordPress have changed, too. For example, files such as content.php, content-single.php, content-none.php, etc. used to be at the top level inside the theme folder. Now, there are in the template-parts folder, so any hard-coded paths in your legacy code may have to be modified, especially if you adopt the new file locations for your existing template files.