Craig Simpson

Web developer from Moray, Scotland

Extending your WordPress theme with add_theme_support

20th September 2017

No Comments

If you build WordPress themes you’ll already be familiar with enabling features using add_theme_support. This function provides an easy means to enable optional features on a per theme basis – WordPress’ custom logo, custom background, title tag support and many more.

It can also be used to extend your theme with custom functionality, and that’s what I want to demonstrate in this post. The example I’m going to use is tweaking a Genesis child theme so that it better supports Beaver Builder.

I’ve done a couple of themes for this client already, and up until now I’ve just added some PHP snippets to the theme functions.php file to remove excess markup, make the theme full width and remove the page and post titles.

But what I really wanted to do was have this code included in my core functionality plugin, then only switch it on if I want the theme to support Beaver Builder. And this is where add_theme_support comes in.

Within my core functionality plugin, I added the following:

1add_action( 'wp_loaded', __NAMESPACE__ . '\\add_beaver_builder_support' );
2/**
3 * Register support for our `add_theme_support` function early so our theme can see it.
4 *
5 * @since 1.0.0
6 */
7function add_beaver_builder_support() {
8 
9 if ( ! current_theme_supports( 'generico-beaver-builder' ) ) {
10 return;
11 }
12 
13 add_action( 'genesis_meta', __NAMESPACE__ . '\\beaver_builder_tweaks' );
14 /**
15 * Hook our filters and actions after `genesis_meta`.
16 *
17 * @since 1.0.0
18 */
19 function beaver_builder_tweaks() {
20 
21 // Force full width content layout.
22 add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_full_width_content' );
23 
24 // Remove all of the extra markup around <main>.
25 add_filter( 'genesis_markup_site-inner', '__return_null' );
26 add_filter( 'genesis_markup_content-sidebar-wrap', '__return_null' );
27 
28 // Remove all post titles.
29 remove_action( 'genesis_post_title', 'genesis_do_post_title' );
30 remove_action( 'genesis_entry_header', 'genesis_do_post_title' );
31 remove_action( 'genesis_entry_header', 'genesis_entry_header_markup_open', 5 );
32 remove_action( 'genesis_entry_header', 'genesis_entry_header_markup_close', 15 );
33 }
34}

With this code in my core functionality plugin I can use add_theme_support( 'generico-beaver-builder' ); within the theme to add my Beaver Builder snippets automatically.

The result is cleaner code within Beaver Builder pages, with excess markup and page titles removed, and much cleaner code in my theme. And the same principle could be applied to any number of other uses.

Leave a Reply

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