Editing Theme Functions – Pluggable Functions

A pluggable function is a function setup in a way that allows you to simply copy it to your child theme’s functions.php, in order to edit it.

Proceed With Caution

Pluggable functions are not as popular around the WordPress community, as they once were. And over time, we’ve made strides to reduce the amount of pluggable functions within our theme framework, but have left many, for the sake of backwards-compatibility.

The majority of our theme framework’s remaining pluggable functions can be found within the theme at:

/framework/includes/display.php
Note: If you’re using a theme with Theme Blvd Framework prior to v2.5, you will see a lot more pluggable functions throughout.

If you’re new to making PHP customizations from a child theme, pluggable functions are seemingly the easier way to go, at first glance. But as you start copying entire functions, that you didn’t write, to your child theme just to make small changes, you’ll see that things quickly become a mess. I personally would avoid editing functions in this way, and take advantage of hooking in custom functions instead, via actions. Most everything in the above mentioned display.php file is hooked to some action, which means it can be unhooked in favor of a new function that you write and hook in.

Also, keep in mind that a lot of themes out there aren’t going to utilize actions and filters throughout the theme, to the extent that we do. So, setting up pluggable functions in a theme is a natural way to quickly make a theme’s functionality more extendable to child themes.

Classic Pluggable Function Setup

For a long time, it’s been fairly common practice to set up pluggable functions throughout a WordPress theme’s functions file, in order to add a certain level of child theme extendability. You’ll commonly see functions setup like this within a theme:

if ( ! function_exists('foo') ) :
/** 
 * Foo function that does stuff.
 */
function foo() {
	// do whatever ...
}
endif;

Or maybe like this:

/** 
 * Foo function that does stuff.
 */
if ( ! function_exists('foo') ) {
	function foo() {
		// do whatever ...
	}
}

These basically say: “If foo() doesn’t already exist, create it.”

Since WordPress loads the child theme’s functions.php before the parent theme, this allows you to declare your function first, before the parent theme gets to it.

In this case, we could declare foo() from our child theme, causing the parent theme to skip over it, and thus use our child theme’s version. So, we’d simply copy the function we found, in the parent theme, to the child theme’s functions.php and make our edits.

/**
 * My version of the Foo function.
 */
function foo() {
	// Now we make our changes ...
}

Utilizing Pluggable Functions in a Theme Blvd Theme

When working with your Theme Blvd theme, there’s one little tidbit to remember when working with pluggable functions. If you remember from the article, Child Theme Setup, your child theme’s functions.php will most likely have the framework included at the top something like this:

<?php
/*-------------------------------------------------------*/
/* Run Theme Blvd framework
/*-------------------------------------------------------*/

require_once( get_template_directory() . '/framework/themeblvd.php' );

/*-------------------------------------------------------*/
/* Start Child Theme
/*-------------------------------------------------------*/

// Your actions and filters ...

This is a major difference between our suggested child theme setup and what you may have done with other themes — That being, including the framework at the top of your functions.php. And when you run the framework at the top of your functions file, you are essentially swapping the order of things in WordPress, as now you’re loading the the parent theme functions before your child theme functions.

This means that if you want to edit one of the framework’s pluggable functions, it would need to get declared before the framework runs. So, keeping with the example we talked about previously, let’s say we were rummaging through the framework’s core files and we came across our fictional foo() function.

if ( ! function_exists('foo') ) :
/** 
 * Foo function that does stuff.
 */
function foo() {
	// do whatever ...
}
endif;

In order for us to modify this, as a pluggable function, we’d need to put our version before the framework runs in functions.php of our child theme.

<?php
/**
 * My version of the Foo function.
 */
function foo() {
	// Now we make our changes ...
}

/*-------------------------------------------------------*/
/* Run Theme Blvd framework (required)
/*-------------------------------------------------------*/

require_once( get_template_directory() . '/framework/themeblvd.php' );

/*-------------------------------------------------------*/
/* Start Child Theme
/*-------------------------------------------------------*/

// Your actions and filters ...

Conclusion

I know, in the beginning, it’s tempting to want to take advantage of pluggable functions wherever you can. It’s that classic copy and paste mindset. But eventually you’re going to hit a wall. So try to challenge yourself and learn more about actions hooks, as an alternate way of editing functionality.