Replacing theme functions
To replace a function, make sure you first have the child theme installed. Copy the function you wish to replace from the parent theme to the functions.php
file in your child theme and make your modifications.
For example, to change the duet_page_title
function, your child theme functions.php
file would look like this:
<?php
add_action( 'wp_enqueue_scripts', 'duet_child_enqueue_scripts' );
function duet_child_enqueue_scripts() {
if ( ! is_admin() ) {
// Add parent theme stylesheet
wp_enqueue_style(
'duet_parent_style',
get_template_directory_uri() . '/style.css',
false,
null
);
}
}
// Your custom functions go here...
function duet_page_title( $title ) {
// Your customizations here
return "My custom title";
}
This is just the tip of the iceberg. Using this approach you may modify or remove any functions from the parent theme from the safety of your child theme.
Replacing theme functions in older themes
By default all of the theme specific functions in Linen, Paperpunch, Photography, React, Shelf, Titan, Traction, and Vigilance are loaded through the -extend.php
file of your parent theme. These functions power different parts of your theme.
Let’s learn how to override a default function through a child theme. In this example, we will replace the printHeaderItems()
function. First, we open up the functions.php
file in our child theme and insert this code:
function load_traction_child_extend() {
global $traction;
remove_action( 'after_setup_theme', 'load_traction_pro_theme' );
locate_template( array( 'traction-child-extend.php' ), true );
if ( class_exists( 'MyChildTheme' ) ) {
$traction = new MyChildTheme;
}
}
add_action( 'after_setup_theme', 'load_traction_child_extend', 5 );
The code above first creates a new function to load our theme object from the child theme. Next, it removes the theme object loading function from our parent theme (so it doesn’t load twice). Finally, it calls a new file (we are about to create it) and loads a new class that will hold our custom functions from that file.
Create a brand new file called traction-child-extend.php
in your child theme folder and insert the following code into that new file:
<?php
class MyChildTheme extends Traction {
function printHeaderItems() {
// Custom Code Goes Here...
}
}
This new printHeaderItems
function in our child theme will replace / override the function from our parent theme.