Extending your Genesis theme with add_theme_support

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:

<?php
/**
* Modify theme to support Beaver Builder better.
*
* - Set the theme layout to full width.
* - Remove the `site-inner` markup.
* - Remove the `content-sidebar-wrap` markup.
* - Remove the entry header and page/ post title.
*
* @package Generico\Core
*
* @since 1.0.0
*/
namespace Generico\Core;
add_action( 'wp_loaded', __NAMESPACE__ . '\\add_beaver_builder_support' );
/**
* Register support for our `add_theme_support` function early so our theme can see it.
*
* @since 1.0.0
*/
function add_beaver_builder_support() {
if ( ! current_theme_supports( 'generico-beaver-builder' ) ) {
return;
}
add_action( 'genesis_meta', __NAMESPACE__ . '\\beaver_builder_tweaks' );
/**
* Hook our filters and actions after `genesis_meta`.
*
* @since 1.0.0
*/
function beaver_builder_tweaks() {
// Force full width content layout.
add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_full_width_content' );
// Remove all of the extra markup around <main>.
add_filter( 'genesis_markup_site-inner', '__return_null' );
add_filter( 'genesis_markup_content-sidebar-wrap', '__return_null' );
// Remove all post titles.
remove_action( 'genesis_post_title', 'genesis_do_post_title' );
remove_action( 'genesis_entry_header', 'genesis_do_post_title' );
remove_action( 'genesis_entry_header', 'genesis_entry_header_markup_open', 5 );
remove_action( 'genesis_entry_header', 'genesis_entry_header_markup_close', 15 );
}
}
view raw beaver.php hosted with ❤ by GitHub

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.

Reader Interactions

Leave a Reply

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