0

I am implementing custom sidebars on a theme I am developing.

Everything works fine when I add new custom sidebars. However, when if I remove these new added custom sidebars I get the error Undefined index: custom_sidebars.

if ( function_exists( 'register_sidebar' ) ) {

        // CUSTOM SIDEBARS

        global $my_theme_option;

        $my_theme_custom_sidebars = $my_theme_option[ 'custom_sidebars' ];

        if ( isset( $my_theme_custom_sidebars ) && sizeof( $my_theme_custom_sidebars ) > 0 ) {
            foreach ( (array)$my_theme_custom_sidebars as $sidebar ) {
                register_sidebar( array(
                    'name' => $sidebar,
                    'id'   => 'sidebar-' . $sidebar,
                    'before_widget' => '<div id="%1$s" class="widget %2$s">',
                    'after_widget' => '</div> <!-- end of widget -->',
                    'before_title' => '<h3 class="widget-title">',
                    'after_title' => '</h3>'
                ));
            }
        }
    }

The error occurs on the following line:

$my_theme_custom_sidebars = $my_theme_option[ 'custom_sidebars' ];

I am a bit stuck on this, any help would be much appreciated.

6
  • don't ever use global variables in your code. Once you will refactor your code to remove the globals your problem will most likely dsapear. Nov 25, 2015 at 10:36
  • I need to use this global variable for the theme options.
    – Madeirense
    Nov 25, 2015 at 10:55
  • no you don't!!! Nov 25, 2015 at 10:59
  • I am using Redux Framework for my theme options and on my options panel i have a Sidebars section with a custom_sidebars field then on my functions.php file I use the above code to enable creation of custom sidebars on the options custom_sidebars field. Isn't this the way to go about it?
    – Madeirense
    Nov 25, 2015 at 11:05
  • 1
    no, I am not trying to troll you here, but if you use a global then you are doing something wrong as its value (in addition to other problems) will depend on the specific path in the code, and that specific code you show here might be executing before the global was initialized.. I find it hard to believe that redux lets you access the settings only via the global, and if it does I will strongly consider using a better framework. Nov 25, 2015 at 11:17

1 Answer 1

0

Sounds like $my_theme_option has no 'custom_sidebars' index, if you've remove it, it does not exist anymore.

just add a if isset() around it, just like you did at around the for loop

Also you don't need to check for functions like register_sidebar to exist if they are older then 2 years.

EDIT: DONT USE GLOBALS! There are functions for that e.g get_option()

    // CUSTOM SIDEBARS

    global $my_theme_option;

    if(isset( $my_theme_option[ 'custom_sidebars' ] )){

        $my_theme_custom_sidebars = $my_theme_option[ 'custom_sidebars' ];

        if ( sizeof( $my_theme_custom_sidebars ) > 0 ) {
            foreach ( (array)$my_theme_custom_sidebars as $sidebar ) {
                register_sidebar( array(
                    'name' => $sidebar,
                    'id'   => 'sidebar-' . $sidebar,
                    'before_widget' => '<div id="%1$s" class="widget %2$s">',
                    'after_widget' => '</div> <!-- end of widget -->',
                    'before_title' => '<h3 class="widget-title">',
                    'after_title' => '</h3>'
                ));
            }
        }
    }
2
  • Of course.. Thank you so much for the eye opener. What you want for xmas..??:)
    – Madeirense
    Nov 25, 2015 at 10:56
  • i've got my 50 rep and am finally able to comment thats enough for xmas :P, also try to follow the advice of Mark Kaplun. this thread has some valid and good points about the usage of globals stackoverflow.com/questions/12445972/stop-using-global-in-php
    – Hans Koch
    Nov 25, 2015 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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