Adding Genesis CPT Archive Settings Metaboxes

A handy thing shipping with the Genesis Framework, as of version 2.0, is the Custom Post Type (CPT) Archive Settings. This settings page allows you set a custom archive title and description, page layout, and set SEO options for the archive template.

To add the archive settings options page you must add the post type support, as shown here.

Custom Settings

I recently needed to add a custom field for all my post type archive templates (and I had a few), so the logical place for it is the CPT archive settings. It’s fairly easy to remove metaboxes from the CPT Settings.

Place the code below in your child theme‘s function.php file to add a “subtitle” field to your CPT archive settings:

<?php
/**
* Add custom metabox to Genesis CPT Archive Settings.
*
* @author Joshua David Nelson, [email protected]
*
* @link https://joshuadnelson.com/adding-metaboxes-to-genesis-cpt-archive-settings
*/
class JDN_Genesis_CPT_Archive_Settings {
/**
* Build the class
*/
public function __construct() {
// Init
add_action( 'init', array( $this, 'init' ) );
}
/**
* Hooks and such
*/
public function init() {
// Register the metabox
add_action( 'genesis_cpt_archives_settings_metaboxes', array( $this, 'register_cpt_metaboxes' ) );
// Sanitize the submitted values
add_action( 'genesis_settings_sanitizer_init', array( $this, 'sanitization' ) );
// Set default values
add_action( 'genesis_cpt_archive_settings_defaults', array( $this, 'cpt_archive_setting_defaults' ) );
}
/**
* Set the default.
*
* @param string $defaults
* @return string $defaults
*/
function cpt_archive_setting_defaults( $defaults ) {
// Default Subtitle Value
$defaults[ 'subtitle' ] = 'default subtitle';
return $defaults;
}
/**
* Register the metabox.
*
* @param string $_genesis_cpt_archives_settings_pagehook
*/
function register_cpt_metaboxes( $_genesis_cpt_archives_settings_pagehook ) {
// Add meta box
add_meta_box( 'page_options', __( 'Archive Template Options', 'domain' ), array( $this, 'custom_archive_options' ), $_genesis_cpt_archives_settings_pagehook, 'main', 'high');
}
/**
* Custom archive options.
*/
function custom_archive_options() {
// Get the current post type from the $_GET
if( isset( $_GET['post_type'] ) ) {
$post_type = $_GET['post_type'];
// If there's not a 'post_type' value, see if we can grab it from the settings page slug.
// I know, this is a little bass-ackwards, but it works.
} elseif( isset( $_GET['page'] ) ) {
$post_type = str_replace( "genesis-cpt-archive-", "", $_GET['page'] );
// If all else fails, jump ship.
} else {
return;
}
// Make sure you have a valid post type and it's one of the post types you want this to appear on
if( is_string( $post_type ) && in_array( $post_type, array( 'project', 'staff' ) ) ) {
echo '<p><label for="' . GENESIS_CPT_ARCHIVE_SETTINGS_FIELD_PREFIX . esc_attr( $post_type ) . '[subtitle]"><b>Subtitle</b></label></p>';
echo '<p><input type="text" name="' . GENESIS_CPT_ARCHIVE_SETTINGS_FIELD_PREFIX . esc_attr( $post_type ) . '[subtitle]" value="' . genesis_get_option( 'subtitle', GENESIS_CPT_ARCHIVE_SETTINGS_FIELD_PREFIX . $post_type ) . '"/></p>';
}
}
/**
* Sanitize the submitted vsalues.
*/
function sanitization() {
// Add the `no_html` filter to the subtitle field
genesis_add_option_filter( 'no_html', GENESIS_SETTINGS_FIELD,
array(
'subtitle',
) );
}
} // end class
global $_jdn_genesis_cpt_archive_settings;
$_jdn_genesis_cpt_archive_settings = new JDN_Genesis_CPT_Archive_Settings;

The code above is easily expandable with more fields within the metabox function. Here is a quick code breakdown:

  1. (Lines 9-33) This is set up in a class and gets all the functions hooked in correctly.
  2. (Lines 51-47) Set the default value.
  3. (Lines 54-59) Register the archive settings metabox, use the page hook variable.
  4. (Lines 66-78) We need to be sure we have a valid post type slug (this is a parameter passed in the url).
  5. (Line 81) Modify your post type slug(s) on this line – this checks the current post type to an array of those that you want this metabox to appear on.
  6. (Lines 82-83) The key difference from adding a metabox to the theme settings is the settings field constant, which is different and has the post type slug added to the end. It’s important to this that you utilize Genesis’ built-in settings field so the data is properly stored and easily retrieved. For custom post type archive settings, this field is built with a prefix (taken from the global variable) with the post type slug after it. Don’t forget to escape the post type slug, just in case. Thus: GENESIS_CPT_ARCHIVE_SETTINGS_FIELD_PREFIX . esc_attr( $post_type )
  7. (Line 83) Use genesis_get_option to grab the current value.
  8. (Lines 90-98) Add the sanitization filters to sanitize all the saved values.

Using it in your theme

Once this is up and working, you can grab the custom meta via genesis_get_cpt_option, and place it in your post type archive template, like this:

<?php
/**
* Get the custom meta from the Genesis CPT Archive Settings. Please this in your template.
*
* @author Joshua David Nelson, [email protected]
*
* @link https://joshuadnelson.com/adding-metaboxes-to-genesis-cpt-archive-settings
*/
$subtitle = genesis_get_cpt_option( 'subtitle' );
if( !empty( $subtitle ) )
echo '<h4 class="subtitle">' . esc_attr( $subtitle ) . '</h4>';

See a full archive template example for the subtitle here.

You can also use the genesis_get_option with the post type specific settings field like this: $value = genesis_get_option( $key, "genesis-cpt-archives-settings-{$post_type}") but I think the above is a bit cleaner.

Featured image credit: Photo by Mr Cup on Unsplash

Discussion

  1. Perfect timing for me, Josh. I was attempting to add a CMB2 metabox to Genesis Archive Settings but your method is better, I’m sure.

    • Tim,

      Glad you like it!

      I use CMB2 frequently, it’s great, and I thought about using it in this case as well – might do so in the future. I like that this method gives me the flexibility to use it on sites without CMB2.

      Cheers,
      Joshua

Leave a Comment

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

You may use these HTML tags and attributes: <a href="" title=""> <b> <blockquote cite=""> <cite> <em> <i> <strike> <strong> . If you'd like to include code in your post, please post it to http://gist.github.com and include a link.