Last active
August 31, 2019 21:11
-
-
Save joshuadavidnelson/f4b5c3ae2fa337d81479 to your computer and use it in GitHub Desktop.
Adding Metaboxes to Genesis CPT Archive Settings, walkthrough publishing August 7th at https://joshuadnelson.com/blog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Add custom metabox to Genesis CPT Archive Settings. | |
* | |
* @author Joshua David Nelson, josh@joshuadnelson.com | |
* | |
* @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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Add post type support for Genesis Custom Post Type Archive Settings. | |
* | |
* @author Joshua David Nelson, josh@joshuadnelson.com | |
* | |
* @link https://joshuadnelson.com/adding-metaboxes-to-genesis-cpt-archive-settings | |
*/ | |
// Option 1: When registering the post type, add 'genesis-cpt-archives-settings' in the 'supports' argument | |
// See: http://codex.wordpress.org/Function_Reference/register_post_type | |
$args = array( | |
'post_type' => 'project', | |
'supports' => array( 'editor', 'title', 'genesis-cpt-archives-settings' ), | |
); | |
register_post_type( 'project', $args ); | |
// Option 2: After a post type is registered, you can add support. | |
// See: https://codex.wordpress.org/Function_Reference/add_post_type_support | |
add_post_type_support( 'project', 'genesis-cpt-archives-settings' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Template for Staff Archive. | |
* | |
* @link https://joshuadnelson.com/adding-metaboxes-to-genesis-cpt-archive-settings | |
* | |
* @package Child_Theme | |
* @since 1.0.0 | |
* @author Joshua Nelson <josh@joshuadnelson.com> | |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License | |
*/ | |
add_action( 'genesis_meta', 'jdn_staff_archive__hooks' ); | |
/** | |
* The page hooks. | |
*/ | |
function jdn_staff_archive__hooks() { | |
// Replace Generic title description with custom title, subtitle, & description | |
remove_action( 'genesis_before_loop', 'genesis_do_cpt_archive_title_description' ); | |
add_action( 'genesis_before_loop', 'jdn_staff_archive_title_description' ); | |
} | |
/** | |
* Add custom headline and description to relevant custom post type archive pages. | |
* | |
* @see genesis_do_cpt_archive_title_description | |
* | |
* @uses genesis_has_post_type_archive_support() Check if a post type should potentially support an archive setting page. | |
* @uses genesis_get_cpt_option() Get list of custom post types which need an archive settings page. | |
* | |
* @return null Return early if not on relevant post type archive. | |
*/ | |
function jdn_staff_archive_title_description() { | |
if ( ! is_post_type_archive() || ! genesis_has_post_type_archive_support() ) | |
return; | |
$headline = genesis_get_cpt_option( 'headline' ); | |
if ( empty( $headline ) && genesis_a11y( 'headings' ) ) { | |
$headline = post_type_archive_title( '', false ); | |
} | |
$intro_text = genesis_get_cpt_option( 'intro_text' ); | |
$headline = $headline ? sprintf( '<h1 %s>%s</h1>', genesis_attr( 'archive-title' ), strip_tags( $headline ) ) : ''; | |
$intro_text = $intro_text ? apply_filters( 'genesis_cpt_archive_intro_text_output', $intro_text ) : ''; | |
// Only displays subtitle if the main archive headline or intro text are also present | |
if ( $headline || $intro_text ) { | |
$subtitle = genesis_get_cpt_option( 'subtitle' ); | |
if( !empty( $subtitle ) ) | |
$headline .= '<h4 class="subtitle">' . esc_attr( $subtitle ) . '</h4>'; | |
printf( '<div %s>%s</div>', genesis_attr( 'cpt-archive-description' ), $headline . $intro_text ); | |
} | |
} | |
genesis(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Get the custom meta from the Genesis CPT Archive Settings. Please this in your template. | |
* | |
* @author Joshua David Nelson, josh@joshuadnelson.com | |
* | |
* @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>'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment