WordPress.org

Ready to get started?Download WordPress

Forums

Better way to code child functions that use existing functions from parent? (9 posts)

  1. s b
    Member
    Posted 2 years ago #

    Hi, this is a question about whether there's a better way to do what I've done. I'm a total coding novice (I'm trying to learn!) so apologies if this is in the codex - I scoured the codex and web but couldn't find anything that answered the question.

    I'm using Buttercream theme but it's a generic question. I've created a child theme to change the look of some elements, and adda sidebar on the right. The original theme has 3 widget-ready "sidebars" in the footer but is a single column theme.

    My theme uses modular CSS. My changes are to the parent style.css but also to the media.css, ie.css and cupcake.css sheets. (I name them because I refer to them in the code below).

    The parent enqueues all relevant stylesheets in its functions.php. It also registers the footer sidebars there.

    Taking my cue from the Codex, I've created a child functions.php. I really only need to add to the existing parent sidebar and enqueuing functions - but no matter what I tried I couldn't get my child theme to recognise my added functions.

    The only way I could get everything working was to remove the relevant parent functions in their entirety and then copy and paste the whole lot of parent code into my child functions.php and amend it there.

    For example, here's the parent functions for enqueuing stylsheets. (Sorry I've pasted the whole block as I wasn't 100% sure where the relevant bit finished!) http://pastebin.com/ZzTPxMkY

    I've had to add the following into my child functions.php:

    function remove_buttercream_scripts() {
    
     remove_action( 'wp_enqueue_scripts', 'buttercream_scripts' );
    }
    
    add_action( 'init', 'remove_buttercream_scripts' );

    Then I've added a new function called buttercream_df_scripts which is just a copy/paste of the pastebin code, but adds:

    wp_register_style( 'buttercream_df_media', get_stylesheet_directory_uri() . '/layouts/media.css', 'mediacss' );
    		wp_enqueue_style( 'buttercream_df_media' );

    to enqueue my child media.css file, and does the same for ie.css and cupcake.css.

    Of course if the parent enqueue functions are updated, this solution means I'll miss out on any updates.

    My question - is there a better way that will ensure I still get the benefit of any parent theme updates to this part of the code?

    I tried JUST adding this to my child functions.php instead of all the above:

    if (!function_exists('buttercream_scripts')) {
        function buttercream_df_styles() {
    	wp_register_style( 'buttercream_df_media', get_stylesheet_directory_uri() . '/layouts/media.css', 'mediacss' );
    	wp_enqueue_style( 'buttercream_df_media' );
    
    	wp_register_style( 'buttercream_df_ie', get_stylesheet_directory_uri() . '/buttercream-df/layouts/ie.css', 'mediaie' );
    	wp_enqueue_style( 'buttercream_df_ie' ); 
    
    	wp_register_style( 'buttercream_df_cupcake', get_stylesheet_directory_uri() . '/css/cupcake.css', 'buttercream_cupcake' );
    	wp_enqueue_style( 'buttercream_df_cupcake' );
    	}
    	add_action( 'wp_enqueue_scripts', 'buttercream_df_styles' );
    }

    but it didn't work.

    Same question has arisen when I want to modify things in other php templates that already exist in the parent theme.

    For example my parent header.php file has a conditional call on the parent ie.css if lt IE 9. To add the same thing for my child ie.css I've had to copy/paste the whole header.php and save in my child under the same file name. But this overrides the parent file completely.

    Can I add all my changes to parent templates in the one place somewhere? Functions perhaps? But what function would it be? This would be much neater and easier to keep track of, and also ensure I get the benefit of any updates to the parent templates still.

    Thanks so much for your help!

  2. Caroline Moore
    Member
    Posted 2 years ago #

    First, let me just say, I'm impressed that you're tackling this and making an effort to learn more about theming in general. That's awesome. :)

    So, in general: In the case of a child theme, functions.php ADDS functionality and does not completely overwrite the functions.php file for the parent theme.

    Similarly, if you're changing something in one of the stylesheets, you don't need to deregister the parent theme's stylesheets. Register a your new stylesheets in the child theme's functions.php, and any duplicate rules will override rules in the parent theme's stylesheets.

    I think the problem is with the first IF statement in your child theme's functions.php:

    if ( !function_exists( 'buttercream_scripts' ) )

    What this says is, "If the function 'buttercream_scripts' does NOT exist, do this stuff." This won't work in the child theme because the function buttercream_scripts DOES exist in the parent theme. As a result, none of the included functions get run and none of your stylesheets get registered. You can remove the IF wrapper entirely, or change it to:

    if ( !function_exists( 'buttercream_df_styles' ) )

    You may need to do some additional troubleshooting from there, but I think this points you in the right direction. :)

    As for the header.php issue, you're correct, you'd have to override header.php entirely in order to make changes. There is a way to register IE conditionals in functions.php, I just only learned about this recently so it was not done this way in Buttercream. You can read more about it here, last note on the ticket:
    http://themes.trac.wordpress.org/ticket/7246

  3. cfm168
    Member
    Posted 2 years ago #

    Hi sixhours,

    Do you have ready for download child patchwork theme?
    There is Custom CSS form on your plugin, I copied style CSS and made some
    changes on it, but it doesn't effect to my website. Is that the way it is just act as note book?

  4. Caroline Moore
    Member
    Posted 2 years ago #

    The Custom CSS field is working properly for me. My guess is it's something in your code that's causing it not to work. I wouldn't copy the entire style.css file into that field--just those elements you want to override.

    I don't have a child theme template for Patchwork ready to go, but you can create one following these instructions.

    HTH!

  5. cfm168
    Member
    Posted 2 years ago #

    sixhours, Could you advise the key point of how to make style.css works on your Custom CSS?

    As my understand, copy whole file of your original style.css should have nothing causing it not to work.

  6. Caroline Moore
    Member
    Posted 2 years ago #

    Given the complexity of what you're trying to do, it makes more sense to use a child theme.

    One problem with copying the entire style.css means a lot of styles are included twice, which shouldn't cause problems per se, but it's sloppy and inefficient. It's also possible the comments in style.css are fouling up the Custom CSS field--that field gets filtered before entering the database for security. I can't say for sure without seeing a link to your site.

    The Custom CSS field is not meant to act as a replacement for a child theme--just for small tweaks. Duplicating the entire stylesheet is not a small tweak. :)

  7. cfm168
    Member
    Posted 2 years ago #

    sixhours, created child theme and worked perfectly.

    One more question, how to adjust vertical line of widgets move closer to the main primary site-content border?

    Thanks!

  8. Caroline Moore
    Member
    Posted 2 years ago #

    Check the margins or padding on the primary content column and/or the secondary widgets column. You may also need to adjust the width(s) of the column(s) depending on what kind of look you're going for.

    I recommend using a plugin like Firebug (getfirebug.com), which allows you to Inspect your website so you can see the exact CSS for each div.

  9. haha888
    Member
    Posted 2 years ago #

    Hi isxhours,

    Using Firebug but still can not figure out how to adjust vertical gap get closer between

    <div id="primary" class="site-content">
    and
    <div id="secondary" class="widget-area" role="complementary">

    Please help.

Topic Closed

This topic has been closed to new replies.

About this Topic