WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Child theme and functions.php (6 posts)

  1. ThorHammer
    Member
    Posted 1 year ago #

    Ok. I am using twentytwelve and I have made a child theme from it. In order to keep some settings when upgrading the "mother" theme, I want some of the special functions to stay in a functions.php in the child theme folder.
    In twentytwelve there are two sidebars. But, on a special page template I have made in my child theme, I have two extra sidebars. Right now they are registered in twentytwelves functions.php, and these settings will go when I upgrade it.
    So... can i just add these into my empty functions.php in my vhild theme in order to keep the sidebars (and then remove these settings from twentytwelves (mother) function.php):

    function lbs-forside_widgets_init() {
    
    	register_sidebar( array(
    			'name' => __( 'Left Sidebar', 'lbs-forside' ),
    			'id' => 'sidebar-4',
    			'description' => __( 'Appears on posts and pages except the optional Front Page template, which has its own widgets', 'lbs-forside' ),
    			'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' => __( 'Right Sidebar', 'lbs-forside' ),
    			'id' => 'sidebar-5',
    			'description' => __( 'Appears on posts and pages except the optional Front Page template, which has its own widgets', 'lbs-forside' ),
    			'before_widget' => '<aside id="%1$s" class="widget %2$s">',
    			'after_widget' => '</aside>',
    			'before_title' => '<h3 class="widget-title">',
    			'after_title' => '</h3>',
    	) );
    
    }

    Right now I have deleted the functions.php in my child theme since my last effort was with the exact same functions.php as in the mother theme, which gave me a fatal error.

  2. ThorHammer
    Member
    Posted 1 year ago #

    Oh... I forgot, above the code above:
    if ( ! function_exists( 'lbs-forside_widgets_init' ) ) :
    And after the code above:

    endif;
    add_action( 'widgets_init', 'lbs-forside_widgets_init' );

    Since these sidebar will be removed from the mother theme they will exist only in my child theme. I am a bit anxious to put this into the empty functions in my child theme due to the near death experience I got when I got a fatal error when the child theme had the same functions.php as the mother. Is this safe to use now? Will the child accept both these sidebars and the default sidebars in the mother with this code?

  3. Rev. Voodoo
    Volunteer Moderator
    Posted 1 year ago #

    Yes, that is fine.

    You must not have the same named functions in parent and child (unless the function in the parent is pluggable and meant to be overridden by child, in which case the function is wrapped in if ( !function_exists )

    You prefix your functions - which is a good way to ensure there is no conflict

    And yes, the parent and child sidebars will function. I do it all the time from child themes and plugins

    Just remove from the parent theme first and all is well.

    Just make sure to properly open your child theme functions.php of course!

    <?php
  4. ThorHammer
    Member
    Posted 1 year ago #

    Thanks you for your reply!
    Then I will finally take the giant leap ... :-)

  5. ThorHammer
    Member
    Posted 1 year ago #

    It worked! Just one thing...
    In this code:
    if ( ! function_exists( 'lbs-forside_widgets_init' ) ) : I had to get rid of the -
    So I ended with:
    if ( ! function_exists( 'lbsforside_widgets_init' ) ) :

  6. Rev. Voodoo
    Volunteer Moderator
    Posted 1 year ago #

    Yeah... I usually don't use hyphens, capital letters, and definitely no spaces in a function... lowercase and underscores only

    You really shouldn't need to wrap your child theme functions in a function_exists wrapper anyway.

    That is done in the parent theme to make functions pluggable by a child theme. The child theme functions.php loads before the parent, so when it checks the function wouldn't exist.

Topic Closed

This topic has been closed to new replies.

About this Topic