Categories: WordPress

Handle WP child-theme functions.php after parent theme

Normally the functions.php in your CHILD theme is called before the PARENT functions.php. In some cases, you might like to alter functionality loaded through the PARENT theme in your CHILD theme, which is not possible without some changes.

From the WordPress Codex :”Unlike style.css, the functions.php of a child theme does not override its counterpart from the parent. Instead, it is loaded in addition to the parent’s functions.php. (Specifically, it is loaded right before the parent’s file.)”

For our new setup, I am using a base class in our PARENT theme to define all our defaults. Some of the CHILD themes need to overwrite or adjust functionality. This allows to have a pure foundation, than alter and extend as needed.

PARENT functions.php
is extending the TimberSite class in my case, as I am using Timber (TWIG for WordPress) for my themes

  1. class cubicFusionBase extends TimberSite {
  2. function __construct(){
  3.  parent::__construct();
  4. }
  5.  
  6. }
  7. // If no CHILD theme is loading init the parent class
  8.  
  9. if(CHILD_THEME_LOADED != 1){
  10.  new cubicFusionBase();
  11. }

CHILD functions.php

  1. // Do not init the parent class
  2. define("CHILD_THEME_LOADED", 1);
  3.  
  4. // Wait for theme setup to be finished
  5. add_action( 'after_setup_theme', function() {
  6.  
  7. // Extend parent theme class
  8.  class cubicFusionChild extends cubicFusionBase {
  9.     function __construct() {
  10.      // Call parent class constructor
  11.      parent::__construct();
  12.   }
  13.   /*
  14.     Add or extend any other functionality here
  15.   */
  16.  
  17. }
  18. // Init Child class
  19. new cubicFusionChild();
  20. }, 42 );
  21.  
  22.  
  23.  

Cheers
Alex

Alex

I am a full-stack developer. I love programming,  design and know my way around server architecture as well.  I would never feel complete, with one of these missing. I have a broad range of interests, that’s why I constantly dive into new technologies and expand my knowledge where ever required. Technologies are evolving fast and I enjoy using the latest. Apart from that, I am a peace loving guy who tries to have people around him that think the same.  I truly believe in the principle: “If you help someone, someone will help you, when you need it."

Recent Posts

Particle Network Animations in Javascript

What are particle animations? Particle network animations in JavaScript typically involve creating visual representations of… Read More

4 days ago

B&B / Hotel Booking Solutions for WordPress | 2024

BOOKING SOLUTIONS 202x This is my take on a subset of booking, appointment, PMS or… Read More

1 month ago

WordPress Cron + WP-CLI + Ntfy

THE GOAL Create a system cron for WordPress, that is accessible and can be easily… Read More

3 months ago

2024 is here and now :)

2024, what's cooking? Slowly getting into the 2024 spirit. 3 projects coming to a close… Read More

4 months ago

2023 ends and whats next !

Short look back at 2023 This has been a busy and interesting year. I am… Read More

4 months ago

cubicFUSION Grid Tweaker – Elementor Grid made easy.

Elementor Pro provides grid containers as an experimental feature. The options provided are limited, when… Read More

5 months ago