• Resolved helferlein

    (@helferlein)


    Hi all,

    Please excuse my lack of knowledge when it comes to PHP and WordPress as I try to explain my issue:

    I’m creating a child theme using twentyfourteen as parent. The widget-title is wrapped within an <h1> tag. I want to override that <h1> tag with an <h3>.

    I tried this code in my child theme functions.php:

    function twentyfourteen_cms3_widgets_init() {
    	require get_template_directory() . '/inc/widgets.php';
    	register_widget( 'Twenty_Fourteen_Ephemera_Widget' );
    
    	register_sidebar( array(
    		'name'          => __( 'Primary Sidebar', 'twentyfourteen' ),
    		'id'            => 'sidebar-1',
    		'description'   => __( 'Main sidebar that appears on the left.', 'twentyfourteen' ),
    		'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'          => __( 'Content Sidebar', 'twentyfourteen' ),
    		'id'            => 'sidebar-2',
    		'description'   => __( 'Additional sidebar that appears on the right.', 'twentyfourteen' ),
    		'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 Widget Area', 'twentyfourteen' ),
    		'id'            => 'sidebar-3',
    		'description'   => __( 'Appears in the footer section of the site.', 'twentyfourteen' ),
    		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
    		'after_widget'  => '</aside>',
    		'before_title'  => '<h3 class="widget-title">',
    		'after_title'   => '</h3>',
    	) );
    }
    
    remove_action( 'widgets_init', 'twentyfourteen_widgets_init' );
    add_action( 'widgets_init', 'twentyfourteen_cms3_widgets_init' );

    Which throws an error message:
    Fatal error: Cannot redeclare class Twenty_Fourteen_Ephemera_Widget in /mypath/wordpress/wp-content/themes/twentyfourteen/inc/widgets.php on line 250

    When I tried it again without these two lines:

    require get_template_directory() . '/inc/widgets.php';
    	register_widget( 'Twenty_Fourteen_Ephemera_Widget' );

    But I still found the ogrinial <h1> tags in the frontend.

    How can I successfully accomplish this task?

Viewing 4 replies - 1 through 4 (of 4 total)
  • parcodeisuoni

    (@parcodeisuoni)

    Hello,
    you need to unregister sidebars first, then you can register your sidebars with the changes you need.

    Thread Starter helferlein

    (@helferlein)

    Hi parcodeisuoni,

    thank you, it seems to work.
    Since I am not an experienced programmer, I would appreciate if you can briefly examine if my code does not contain any blunder?

    function remove_twentyfourteen_widgets(){
    
    	unregister_sidebar( 'sidebar-1' );
    	unregister_sidebar( 'sidebar-2' );
    	unregister_sidebar( 'sidebar-3' );
    }
    add_action( 'widgets_init', 'remove_twentyfourteen_widgets', 11 );
    
    function twentyfourteen_cms3_widgets_init() {	
    
    	register_sidebar( array(
    		'name'          => __( 'Primary Sidebar', 'twentyfourteen' ),
    		'id'            => 'sidebar-1',
    		'description'   => __( 'Main sidebar that appears on the left.', 'twentyfourteen' ),
    		'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'          => __( 'Content Sidebar', 'twentyfourteen' ),
    		'id'            => 'sidebar-2',
    		'description'   => __( 'Additional sidebar that appears on the right.', 'twentyfourteen' ),
    		'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 Widget Area', 'twentyfourteen' ),
    		'id'            => 'sidebar-3',
    		'description'   => __( 'Appears in the footer section of the site.', 'twentyfourteen' ),
    		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
    		'after_widget'  => '</aside>',
    		'before_title'  => '<h3 class="widget-title">',
    		'after_title'   => '</h3>',
    	) );
    }
    
    remove_action( 'widgets_init', 'twentyfourteen_widgets_init', 11 );
    add_action( 'widgets_init', 'twentyfourteen_cms3_widgets_init', 11 );

    Thread Starter helferlein

    (@helferlein)

    Since this seems to function properly, i mark this as resolved.

    Actually the underlying problem lies in TwentyFourteen’s functions.php code. The registration for Twenty_Fourteen_Ephemera_Widget is done incorrectly in twentyfourteen_widget_init(). I ran across the same problem when I was adding widgets to my child theme.

    To correct the problem remove the following line from twentyfourteen_widget_init()
    register_widget( 'Twenty_Fourteen_Ephemera_Widget' );
    and move the require .. widgets.php above the function. Rename the function twentyfourteen_widget_init to twentyfourteen_sidebar_init. Do not forget to rename it in
    add_action( ‘widgets_init’, ‘twentyfourteen_widget_init’ );

    In widgets.php paste the following at the bottom of the file:

    function twentyfourteen_widgets_init() {
    	register_widget( 'Twenty_Fourteen_Ephemera_Widget' );
    	do_action('widgets_init');
    }
    // call widget initialization
    add_action('init', 'twentyfourteen_widgets_init');

    To summarize, the function containing register_sidebar is queued using add_action (‘widgets_init’.
    However the function containing register_widget must perform do_action(‘widget_init’) after all widgets have been registered. This function is then queued using add_action (‘init’

    A quick look at WordPress’ default-widgets.php will show the correct way to initialize widgets.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Override twentyfourteen functions.php using child theme’ is closed to new replies.