1

I am using a theme_mod string from customizer to generate custom sidebar, but it is called from another function. This is my code:

function call_sidebar_function() {

    if ( get_theme_mod( 'enable_sidebar' ) ) {
        $name = "mySidebarName";
        $numberOfSidebars = get_theme_mod('number_of_sidebars');
        generate_sidebars($name, $numberOfSidebars);
    }
}

function generate_sidebars($name, $numberOfSidebars) {
    $i = 1;
    foreach ($numberOfSidebars as $sidebar) {           
        register_sidebar(
            array(
                'name' => $name.$i,
                'id' => $name.$i,
            )
        );
        $i++;
    }
}
add_action( 'widgets_init', 'generate_sidebars' );
  • 2
    There is no call of function widget_init in your posted code. So it's pretty hard to say, what is wrong exactly... – Krzysiek Dróżdż Apr 4 '18 at 20:44
4

widgets_init isn't a function, it's an action hook. The callback you specify for that hook is generate_sidebars which requires two parameters, but the widgets_init hook doesn't pass any parameters to its callbacks.

I think what you're trying to do is this:

add_action( 'widgets_init', 'call_sidebar_function' );

Which will call the call_sidebar_function() function which will then call the generate_sidebars() function with the correct parameters.

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.