Joshua David Nelson

Bullshit Free WordPress Development

  • Services
  • Code
  • About
  • Contact

Override Genesis Theme Settings (with helper class)

February 21, 2015 by Joshua Nelson

I use the Genesis Framework frequently with my full website development because it is a well-supported, powerful theme framework. It allows me to spend more time building the custom elements of a client’s site and less time working on the basic theme construction. I can leverage the vast amount of work already done building the theme to deliver a superior product at a competitive rate. All that is great, but it’s also a fun framework to use, too.

When building a custom site I often override genesis theme settings, and then remove the theme option metaboxes so client’s can’t accidentally mess up elements on their site. For instance, sometimes the custom child theme is specifically design to only show excerpts on the blog roll, or featured images of a specific size. These are both things I could set in the theme settings interface and import those settings to the live site. However, I like the ability to install a theme and go, without fussing about with inporting settings. I also recognize that if we don’t have options built into the child them, those settings shouldn’t be available to change.

So, that leads us to this place – set the option programmatically and remove the metabox. You can set defaults via a function, filter specific the theme settings to force them, and even remove theme setting metaboxes pretty easily. I put these together into a helper class to do that all more simply from your theme functions. Just drop this into your theme and reference in your functions.php file like so:

<?php
/**
* Using the Genesis Settings Helper Class
*
* @version 1.0.0
* @author Joshua David Nelson, [email protected]
* @license GPLv2.0+
*/
// Reference the helper class by uncommenting below and setting it to the path relative to this file
// require_once '/path/to/genesis-settings-override.php';
add_action( 'init', 'jdn_set_theme_options' );
function jdn_set_theme_options() {
if( class_exists( 'JDN_Override_Genesis_Settings' ) ) {
$override = new JDN_Override_Genesis_Settings;
// Override Theme Settings - these are all the defaults
$options = array(
'update' => 1,
'blog_title' => 'text', // or 'image'
'header_right' => 0,
'site_layout' => genesis_get_default_layout(),
'superfish' => 0,
'nav_extras' => '',
'nav_extras_twitter_id' => '',
'nav_extras_twitter_text' => __( 'Follow me on Twitter', 'genesis' ),
'feed_uri' => '',
'comments_feed_uri' => '',
'redirect_feeds' => 0,
'comments_pages' => 0,
'comments_posts' => 1,
'trackbacks_pages' => 0,
'trackbacks_posts' => 1,
'breadcrumb_home' => 0,
'breadcrumb_front_page' => 0,
'breadcrumb_posts_page' => 0,
'breadcrumb_single' => 0,
'breadcrumb_page' => 0,
'breadcrumb_archive' => 0,
'breadcrumb_404' => 0,
'breadcrumb_attachment' => 0,
'content_archive' => 'full', // or 'excerpts'
'content_archive_thumbnail' => 0,
'posts_nav' => 'older-newer', // or 'numeric'
'blog_cat' => '',
'blog_cat_exclude' => '',
'blog_cat_num' => 10,
'header_scripts' => '',
'footer_scripts' => '',
'image_size' => 'full', // or any other registered image size
'image_alignment' => '', // blank, 'alignleft' or 'alignright'
);
$override->set_options( $options );
// Remove Theme Settings Metaboxes
$metaboxes = array( 'breadcrumb', 'feeds', 'header', 'nav', 'breadcrumb', 'comments', 'posts', 'blogpage', 'scripts', 'version' );
$override->remove_metaboxes( $metaboxes );
}
}
view raw example.php hosted with ❤ by GitHub

Here’s the helper class referenced above.

<?php
/**
* Override theme settings and remove theme settings metaboxes with this helper class
*
* Requires WordPress v2.6.0 and PHP v5.3.0
*
* @version 2.0.0
* @author Joshua David Nelson, [email protected]
* @license GPLv2.0+
*
* Copyirght 2015, Joshua David Nelson
*/
class JDN_Override_Genesis_Settings {
// The array of options to set
private $options = array();
// The array of metaboxes to remove
private $metaboxes = array();
// The current option to be set
private $current_option = '';
// The current metabox to be removed
private $current_metabox = '';
// Set Genesis Theme Options with array
public function set_options( $these_options ) {
if( !is_array( $these_options ) )
return;
$this->options = $these_options;
foreach( $this->options as $option => $value ) {
$this->current_option = $option;
add_filter( "genesis_pre_get_option_{$option}", array( $this, $option ), 10, 1 );
}
}
//Override Theme Option with new value using the magic call method
public function __call( $func, $params ) {
if( in_array( $func, $this->options ) ) {
return $this->get_value( $func );
} else {
return $params;
}
}
// Get the option's new value
private function get_value( $option ) {
if( array_key_exists( $option, $this->options ) ) {
return $this->options[ $option ];
} else {
return null;
}
}
// Remove a group of metaboxes
public function remove_metaboxes( $metaboxes ) {
if( !is_array( $metaboxes ) )
return;
// Set metaboxes
$this->metaboxes = $metaboxes;
// Add action
add_action( 'genesis_theme_settings_metaboxes', array( $this, 'remove_genesis_metaboxes' ) );
}
// Remove genesis theme settings metaboxes
public function remove_genesis_metaboxes( $_genesis_theme_settings_pagehook ) {
foreach( $this->metaboxes as $metabox ) {
remove_meta_box( "genesis-theme-settings-{$metabox}", $_genesis_theme_settings_pagehook, 'main' );
}
}
}
view raw genesis-settings-override.php hosted with ❤ by GitHub

Check it out on Github.

Featured Image (license): Marcin Wichary

Update 2-23-15: The main class originally shown in the gist above was accidentally copied from an older version that didn’t work. I’ve updated it now to version 2.0.0 – using the magic __call() method – which works like a charm.

Filed Under: Genesis Tagged With: child theme, Genesis, Genesis Tutorial, theme features

  • Twitter
  • RSS Feed URL

About Me

I'm a WordPress Engineer. I build sleek, custom websites with WordPress and Genesis.

See my services and my recent work.

Contact me to get your project started.

Gravity Forms Plugin for WordPress Fastest WordPress Hosting

Recent Posts

  • Using Font Awesome Icons for WooCommerce Grid / List Toggle
  • Disable Blog: WordPress Gone Blog-less
  • Category (Taxonomy) Dropdown Filtered By Post Type
  • Weather in WordPress with Dark Sky
  • Fixing Your Deprecated Widget Constructors in WordPress 4.3
  • Twitter
  • RSS Feed URL
  • Code Snippets
  • My Plugins
  • Make a Payment

© Joshua David Nelson | Hand-Forged | WordPress + Genesis | Terms of Service | Legal | Contact