• Resolved Jam

    (@pimaniii)


    I know this is a common WordPress questions but I cant seem to find a simple generalized answer.

    If I want to modify a parent’s functions.php, how can I do this in the child? I don’t know how to simply ‘add’ a new function as the edit I need to make is minor and well nested in a bunch of code.

    Thoughts?

Viewing 8 replies - 1 through 8 (of 8 total)
  • If I want to modify a parent’s functions.php, how can I do this in the child?

    there is no generalized answer – you will need to be specific on what you want to change – pluggable functions, filter, actions…

    Thread Starter Jam

    (@pimaniii)

    I guess I can see why!

    I want to update a function ‘register sidebar’ with two more sidebars. I’m not sure how I can just add on a new function, or modify it in my child theme.

    The beginning starts like:

    if ( function_exists('register_sidebar') ) {
    	register_sidebar(array(
    		'name' => 'Pages Sidebar',
    		'description' => esc_html__('A widget area, used as a sidebar for regular pages.', 'udesign'),
    		'before_widget' => '<div id="%1$s" class="widget %2$s substitute_widget_class">',
    		'after_widget' => '</div>',
    		'before_title' => '<h3 class="widgettitle">',
    		'after_title' => '</h3>',
    	));

    you can simply register the (two) new sidebars in functions.php of the child theme;

    example from a Twenty Twelve child theme:

    function twentytwelvechild_widgets_init() {
    	register_sidebar( array(
    		'name' => __( 'Third Front Page Widget Area', 'twentytwelve' ),
    		'id' => 'sidebar-4',
    		'description' => __( 'Appears when using the optional Front Page template with a page set as Static Front Page', 'twentytwelve' ),
    		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
    		'after_widget' => '</aside>',
    		'before_title' => '<h3 class="widget-title">',
    		'after_title' => '</h3>',
    	) );
    }
    add_action( 'widgets_init', 'twentytwelvechild_widgets_init', 15 );

    (the priority 15 lets this ‘sidebar’ appear after the original ones)

    https://codex.www.ads-software.com/Function_Reference/register_sidebar
    https://codex.www.ads-software.com/Widgetizing_Themes#Another_complete_example

    Thread Starter Jam

    (@pimaniii)

    Wow awesome! So I put the following but it causes a server error. Is there some small syntax error? I know my register_sidebars work. Can I simply put these new registers in a unique function in the child theme?

    I think my error is with the last add_actions. Is ‘widgets_init’ almost universal to all themes?

    function u-design-register-new-sidebars() {
    
    	register_sidebar(array(
    		'name' => 'PagesSidebar_HR',
    		'description' => esc_html__('A widget area, used as a sidebar for the Page Template HR.', 'udesign'),
    		'before_widget' => '<div id="%1$s" class="widget %2$s substitute_widget_class">',
    		'after_widget' => '</div>',
    		'before_title' => '<h3 class="widgettitle">',
    		'after_title' => '</h3>',
    	));
    
    	register_sidebar(array(
    		'name' => 'PagesSidebar_IT',
    		'description' => esc_html__('A widget area, used as a sidebar for the Page Template IT.', 'udesign'),
    		'before_widget' => '<div id="%1$s" class="widget %2$s substitute_widget_class">',
    		'after_widget' => '</div>',
    		'before_title' => '<h3 class="widgettitle">',
    		'after_title' => '</h3>',
    	));
    
    }
    
    add_action( 'widgets_init', 'u-design-register-new-sidebars()', 17 );

    php function name: no hyphens, just underlines;
    https://www.php.net/manual/en/functions.user-defined.php

    add_action uses the function name only, not the brackets:
    https://codex.www.ads-software.com/Function_Reference/add_action

    add_action( 'widgets_init', 'u_design_register_new_sidebars', 17 );

    you also need to correct the function name at the top of the code.

    needless to say that a functions.php needs to start with <?php at the very start in the first line.

    Thread Starter Jam

    (@pimaniii)

    What do you mean by

    you also need to correct the function name at the top of the code.

    Right now I have (with the php at the top)

    // Register new sidebars
    
    function customsidebars() {
    
    	register_sidebar(array(
    		'name' => 'PagesSidebar_HR',
    		'description' => esc_html__('A widget area, used as a sidebar for the Page Template HR.', 'udesign'),
    		'before_widget' => '<div id="%1$s" class="widget %2$s substitute_widget_class">',
    		'after_widget' => '</div>',
    		'before_title' => '<h3 class="widgettitle">',
    		'after_title' => '</h3>',
    	));
    
    	register_sidebar(array(
    		'name' => 'PagesSidebar_IT',
    		'description' => esc_html__('A widget area, used as a sidebar for the Page Template IT.', 'udesign'),
    		'before_widget' => '<div id="%1$s" class="widget %2$s substitute_widget_class">',
    		'after_widget' => '</div>',
    		'before_title' => '<h3 class="widgettitle">',
    		'after_title' => '</h3>',
    	));
    
    }
    
    add_action( 'widgets_init', 'customsidebars', 15 );

    It doesnt crash, but does not properly register the sidebars. Am I missing something?

    does not properly register the sidebars.

    what are the results?
    do you get the two new ‘sidebar’ areas under ‘appearance – widgets’?

    your last posted code works in a child of Twenty Twelve;

    if you have problems, this might come from your used theme – ‘u design’?

    if so, try to contact the theme’s developer for support.

    Thread Starter Jam

    (@pimaniii)

    Oh I see that it created a new widget instead of using the old one in the parent theme.

    Thank you very much for your time and help!!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Functions.php’ is closed to new replies.