WordPress.org

Ready to get started?Download WordPress

Forums

The Bootstrap
[resolved] removing controls and settings set in parent from child theme (5 posts)

  1. webmetender
    Member
    Posted 1 year ago #

    Hi there,
    thank you Konstantin for this great theme, actually the cleanest one based on bootstrap i found around.

    I'm working on a child-theme based on your theme, for the first time using the Theme Customization API and I can't achieve what i entend to.

    Is there anyway to hook and replace some controls and settings set in theme-customizer.php from a child-theme?
    I didn't want to copy this file from parent to child theme and directly modify it, tried to $wp_customize->remove_control from functions.php located in child-theme but it fails.

    To be more precise, if I want to keep the 'Layout' section set in my the-bootstrap parent theme but remove the setting and control of the 'Default Layout' option and replace it by another radio option set, all this from a child theme, is the only solution copying and customizing theme-customizer.php or can it be done by hooking in any way maybe $wp_customize?

    Copying and customizing theme-customizer.php wouldn't be a problem but I thought that trying to interact this way would be the occasion to learn a bit more about oophp in wordpress.

    EDIT: actually i tried to customize a child-theme copy of theme-customizer.php but failed and the reason is obvious:
    as theme-customizer.php is included in functions.php, the only file not overridden by its child version, i saw a nice php fatal error due to the redeclaration of the_bootstrap_customize_register().
    So i fall back to previous question: how to hook dat?

    Thanks,

    Adam

    http://wordpress.org/extend/themes/the-bootstrap/

  2. jmjf
    Member
    Posted 1 year ago #

    I've made a couple of changes to the customizer, but I've done so by directly editing the parent code because I thought the changes are worth including in the-bootstrap proper and posted them against the theme on github. :)

    But, if you don't want to take that approach, it looks like the contents of the customizer are defined in theme-customizer.php in the_bootstrap_customize_register, which is followed by add_action( 'customize_register', 'the_bootstrap_customize_register' );

    So I think you could write my_theme_customize_register in your child theme's functions.php or in your child's theme-customizer.php. This function, which would probably duplicate a lot of the_bootstrap_customize_register. Then do:

    remove_action( 'customize_register', 'the_bootstrap_customize_register' );
    add_action( 'customize_register', 'my_theme_customize_register' );

    I haven't tested that, but I've done similar things elsewhere in my child theme and those cases worked.

    One of the challenges with hooking the customize_register function would be that you couldn't modify the existing sections (at least, I don't you can hook into the middle of an array declaration), only add a new section or new sections. It might be possible to make each section a separate function, which might let you replace individual sections by overriding the function (given proper "if exists" bracketing and if the admin side will allow child-theme function overrides). There could also be a function call at the end of customize_register that would allow you to override a default do-nothing function to add a new section (or new sections).

  3. jmjf
    Member
    Posted 1 year ago #

    But I should note, if you just want to add a new section, you can add_action your own customizer function with the new sections without removing the original and it will add your new sections.

  4. webmetender
    Member
    Posted 1 year ago #

    Hi jmjf and thank you for your answer.

    As I explained in the 'EDIT' section of my initial post, I already applied the solution you give but it doesn't work: theme-customizer.php is included in the functions.php file.

    Unlike any other files in child themes, functions.php does not replace the one called in parent theme but both are loaded, and functions.php from child is loaded first.

    So, if from the child functions.php is called
    remove_action( 'customize_register', 'the_bootstrap_customize_register' );
    it will be uneffective as
    add_action( 'customize_register', 'the_bootstrap_customize_register' );
    will be fired again later from parent functions.php!

    But i read again the-boostrap's code and found that theme-customizer.php is included in the the_bootstrap_setup() function which is launched by the 'after_setup_theme' action.

    Eureka! I know that the 'init' action fires after 'after_setup_theme' so I included in the functions.php of my child theme the following code

    function wmt_setup(){
    	if ( version_compare( get_bloginfo( 'version' ), '3.4', '<' ) )
    		require_once( get_template_directory() . '/inc/wmt-options.php' );
    	else
    		require_once( get_template_directory() . '/inc/wmt-customizer.php' );
    }
    add_action( 'init', 'wmt_setup' );

    You see it's inspired by the original code, I run as well the version check to serve a option page if it's needed.

    And all works fine now with my theme-customizer.php customization!

  5. webmetender
    Member
    Posted 1 year ago #

    Made a mistake in the code above, actually it's in the child theme functions.php and the 2 files required are in the child theme too, so the code should look like:

    function wmt_setup(){
    	if ( version_compare( get_bloginfo( 'version' ), '3.4', '<' ) )
    		require_once( get_stylesheet_directory() . '/inc/wmt-options.php' );
    	else
    		require_once( get_stylesheet_directory() . '/inc/wmt-customizer.php' );
    }
    add_action( 'init', 'wmt_setup' );

Topic Closed

This topic has been closed to new replies.

About this Theme

About this Topic