Better way to code child functions that use existing functions from parent?
-
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!) https://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!
- The topic ‘Better way to code child functions that use existing functions from parent?’ is closed to new replies.