Replace a non pluggable function in a WordPress parent theme with a customized function in the child theme

A pluggable function is a a function wrapped in a function_exists() call eg.

<?php 
if ( ! function_exists( 'my_pluggable_function' ) ) {
    function my_pluggable_function() {
        /* Some code goes here */
    }
}
?>

When using a child theme, you can override pluggable functions by defining them first in your child theme’s functions.php file. The child theme’s functions.php file is included before the parent theme’s file, so the child theme functions would be used.

If the functions that are not pluggable are instead attached to a filter or action hook you can replace them using the following method in the child theme’s functions.php. I have used the example of the widgets_init action.

  1. Unhook the parent theme’s widget function (declared in the parent theme’s functions.php) from the widgets_init action
  2. Create a customized function to register widget areas for the child theme
  3. Hook our new function to the widgets_init action

Again, this code would go in the child theme’s functions.php with no need to modify the parent theme’s functions.php.

<?php 
remove_action( 'widgets_init', 'parenttheme_widgets_init' );

function childtheme_widgets_init() {
    /* Some code to register widget areas goes here */
}

add_action( 'widgets_init', 'childtheme_widgets_init' );
?>

Leave a Reply

Your email address will not be published. Required fields are marked *