Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I attempted to make a minor change to a function in the Twenty Eleven theme (wordpress 3.4.2)

I get this error message:

Fatal error: Cannot redeclare twentyeleven_widgets_init() (previously declared in /home/labfocus/public_html/wp-content/themes/twentyeleven-child/functions.php:10) in /home/labfocus/public_html/wp-content/themes/twentyeleven/functions.php on line 438

Here is the function that I am attempting to add in place of the original

<?php
if ( !function_exists('twentyeleven_widgets_init')) {
/**
 * Register our sidebars and widgetized areas. Also register the default Ephemera widget.
 *
 * @since Twenty Eleven 1.0
 */
function twentyeleven_widgets_init() {

    register_widget( 'Twenty_Eleven_Ephemera_Widget' );

    register_sidebar( array(
        'name' => __( 'Main Sidebar', 'twentyeleven' ),
        'id' => 'sidebar-1',
        'before_widget' => '<div class="su-spoiler-title">%2$s</div><aside id="%1$s" class="widget %2$s">',
        'after_widget' => "</aside></div>",
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ) );

    register_sidebar( array(
        'name' => __( 'Showcase Sidebar', 'twentyeleven' ),
        'id' => 'sidebar-2',
        'description' => __( 'The sidebar for the optional Showcase Template', 'twentyeleven' ),
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => "</aside>",
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ) );

    register_sidebar( array(
        'name' => __( 'Footer Area One', 'twentyeleven' ),
        'id' => 'sidebar-3',
        'description' => __( 'An optional widget area for your site footer', 'twentyeleven' ),
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => "</aside>",
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ) );

    register_sidebar( array(
        'name' => __( 'Footer Area Two', 'twentyeleven' ),
        'id' => 'sidebar-4',
        'description' => __( 'An optional widget area for your site footer', 'twentyeleven' ),
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => "</aside>",
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ) );

    register_sidebar( array(
        'name' => __( 'Footer Area Three', 'twentyeleven' ),
        'id' => 'sidebar-5',
        'description' => __( 'An optional widget area for your site footer', 'twentyeleven' ),
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => "</aside>",
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ) );
    }
}

Can anyone see what's wrong with this function?

Update: the question is really that I want to change how the Main Sidebar displays before_widget and after_widget. I've added a <div>

share|improve this question
    
I just saw a note in this article for creating twenty eleven child themes that uses add_action and add_filter - would these help? –  Nohl Oct 12 '12 at 18:00

2 Answers 2

Why exactly are you trying to redeclare that function?

You can either edit the original function, or, better yet, use remove_action to remove add_action( 'widgets_init', 'twentyeleven_widgets_init' ); and then reattach your own function add_action('widgets_init', 'your_widgets_init_function' );

share|improve this answer
    
Don't edit parent theme, but adding own function is the way to go. Original is coded to be pluggable (able to be declared in different place). –  Rarst Oct 12 '12 at 11:46
    
I want to modify the Main Sidebar and this is the only thing I could think of. Is there a way to change before_widget and after_widget without redoing the whole function? –  Nohl Oct 12 '12 at 17:34
    
@paulruescher I had read in multiple places (including the WP codex) that the correct procedure for a child theme was to use the if(!function_exists()) structure. I welcome any other methods. –  Nohl Oct 12 '12 at 17:36
    
I just found this thread that indicates that some twentyeleven functions lack the if(!function_exists()) code required for the trick to work. @paulruescher can you suggest the correct usage of remove_action to fix it? –  Nohl Oct 12 '12 at 17:56
1  
I would look into trying to do it properly, but personally what I would do is use remove_action. In functions.php, there is an add_action right below twentyeleven_widgets_init. Use the first paramater passed in your remove_action. Then, write your new twentyeleven_widgets_init, and use add_action to re-add the function using that original add_action parameter –  paulruescher Oct 13 '12 at 20:51

I had the same problem... SOLUTION: http://venutip.com/content/right-way-override-theme-functions but use 'after_setup_theme' instead of 'init' or it won't work because the child functions.php is called before the parent functions.php. Another useful article: http://ottopress.com/2010/wordpress-protip-child-themes/

share|improve this answer
1  
An answer should be more than just a link to an external site. Please add a solution. –  toscho Dec 24 '12 at 2:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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