0

I have registered several sidebars via functions.php like this:

// Register sidebars
function register_widgets() {

// Main sidebar
  register_sidebar( array(
    'name' => __( 'Main Sidebar', 'Core' ),
    'id' => 'main',
    'description' => __( 'The main sidebar appears on the right on each page except the front page template and blog.', 'Core' ),
    'before_widget' => '<aside id="%1$s" class="widget %2$s">',
    'after_widget' => '</aside>',
    'before_title' => '<h3 class="widget-title">',
    'after_title' => '</h3>',
  ) );
}
add_action( 'widgets_init', 'register_widgets' );

As I'm adding more than one sidebar, I'd like to move the code to an include. This is what I have now in functions.php:

// Load widgets
require_once("includes/widgets.php");

Now that I've moved the sidebar code from functions.php, my theme no longer displays any widget options via Appearance > Widgets.

I can't see that I've made any errors. What have I done wrong?

2

You've made a mistake in your widgets.php file, most probably forgetting to add an opening </php tag at the start of your file. Also, I forgot one more thing, require_once("includes/widgets.php"); should not be in any function or hook

Just for the sake of interes, you can cut down on code if you register many sidebars by using a foreach loop. Here is how I register my sidebars in my theme

<?php
function pietergoosen_widgets_init() {
$mainWidgets = array (
                'sidebar-10' => array (
                    __( 'Main Sidebar', 'pietergoosen' ) => __( 'Main Sidebar for the website pages', 'pietergoosen' ),
                ),
                'sidebar-11' => array (
                    __( 'Content Sidebar', 'pietergoosen' ) => __( 'Additional sidebar on the right of pages', 'pietergoosen' ),
                ),
                // Sidebars 12, 13 and 14 not redundant as from twentyfourteen
                'sidebar-15' => array (   // Used in footer-functions.php   
                    __( 'Footer Widget One', 'pietergoosen' ) => __( 'Footer widgets', 'pietergoosen' ),
                ),
                'sidebar-16' => array (   // Used in footer-functions.php   
                    __( 'Footer Widget Two', 'pietergoosen' ) => __( 'Footer widgets', 'pietergoosen' ),
                ),
                'sidebar-17' => array (   // Used in footer-functions.php   
                    __( 'Footer Widget Three', 'pietergoosen' ) => __( 'Footer widgets', 'pietergoosen' ),
                ),
                'sidebar-18' => array (   // Used in footer-functions.php  
                    __( 'Footer Widget Four', 'pietergoosen' ) => __( 'Footer widgets', 'pietergoosen' ),
                ),
                'sidebar-19' => array (   // Used in content-functions.php   
                    __( 'Inside first post widget for advertising', 'pietergoosen' ) => __( 'Inside first post widget', 'pietergoosen' ),
                ),
                'sidebar-20' => array (   // Used in content-functions.php    
                    __( 'After first post widget', 'pietergoosen' ) => __( 'After first post widget for ads', 'pietergoosen' ),
                )
            );

foreach ( $mainWidgets as $mainId => $mainWidget ) {
foreach ($mainWidget as $mainWidgetname => $mainWidgetdescription)
    {
    register_sidebar(
        array (
                'name'          => __( $mainWidgetname, 'pietergoosen' ),
                'id'            =>  $mainId,
                'description'   => __( $mainWidgetdescription, 'pietergoosen' ),
                'before_widget' => '<aside id="%1$s" class="widget %2$s">',
                'after_widget'  => '</aside>',
                'before_title'  => '<h1 class="widget-title">',
                'after_title'   => '</h1>',
        ));
    }
}
}

add_action( 'widgets_init', 'pietergoosen_widgets_init' );  
  • The foreach loop tip, that is such an awesome idea!! – Matt Royal Jun 25 '14 at 6:21
0

Why not use the WordPress function locate_template(). Information on it in the WordPress Codex here

Create a folder called includes in your theme or child theme with your widgets.php and then add this to your functions.php in your theme.

function mywebsite_sidebars() {
    // checks in parent and child themes  
    locate_template( array( 'includes/widgets.php' ), true, true );
}
add_action( 'after_setup_theme', 'mywebsite_sidebars' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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