Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add metabox and other functions for subhead #19

Merged
merged 10 commits into from
Jun 3, 2020
5 changes: 4 additions & 1 deletion functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
/** Customizer Additions */
require_once( 'library/bs-customizer-additions.php' );

/** The "Subhead" metabox and related functions */
require_once( 'library/metabox-subhead.php' );

/** Required and Recommended Plugins */
require_once( 'library/class-tgm-plugin-activation.php' );
require_once( 'library/bs-plugin-activation.php' );
Expand Down Expand Up @@ -173,4 +176,4 @@ function calhealth_require_files() {
}
}
}
add_action( 'after_setup_theme', 'calhealth_require_files' );
add_action( 'after_setup_theme', 'calhealth_require_files' );
108 changes: 108 additions & 0 deletions library/metabox-subhead.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
/**
* Functions related to meta boxes
*/

/**
* Register the 'subtitle' post meta
*/
function subtitle_meta_box_register_post_meta() {
return register_post_meta(
'post',
'subtitle',
array(
'object_subtype' => 'post',
'type' => 'string',
'description' => __( 'The subhead, deck, subtitle, or lead is a preface shown on article pages below the main title.' , 'allonsy' ),
'single' => true,
'sanitize_callback' => 'sanitize_textarea_field',
// 'auth_callback' => '',
'show_in_rest' => true,
),
);
}
add_action( 'init', 'subtitle_meta_box_register_post_meta' );

/**
* Output the subtitle metabox.
*
* @link Copied from https://github.com/INN/umbrella-wcij/blob/16247cdd8f03a593633c82f8836c4c1e8aa83987/wp-content/themes/wisconsinwatch/inc/metaboxes.php with improvements
*/
function subtitle_meta_box_display() {
global $post;
$values = get_post_custom( $post->ID );
wp_nonce_field( 'subtitle_meta_box_nonce', 'subtitle_meta_box_nonce' );
?>
<label for="subtitle" class="screen-reader-text"><?php esc_html_e( 'Subtitle', 'largo' ); ?></label>
<textarea name="subtitle" id="subtitle" class="widefat" rows="2" cols="20"><?php
// PHP open/close are at the textarea boundary so we don't prepend/append this with tabs.
if ( isset( $values['subtitle'] ) ) {
echo sanitize_text_field( $values['subtitle'][0] );
}
?></textarea>
<p><small><?php esc_html_e( 'Plain text is allowed in the post subtitle.', 'largo' ); ?></small></p>
<?php
}

/**
* Save action for the metabox
*
* @param int $post_id Post ID.
* @param WP_Post $post Post object.
* @param bool $update whether the post is being updated
* @return null
*/
function subtitle_meta_box_save( $post_id, $post, $update = false ) {
if ( ! isset( $_POST['subtitle_meta_box_nonce'] ) ) {
error_log(var_export( 'subtitle meta box nonce not set', true));
return;
}

if ( ! wp_verify_nonce( $_POST['subtitle_meta_box_nonce'], 'subtitle_meta_box_nonce' ) ) {
error_log(var_export( 'subtitle meta box nonce not valid', true));
return;
}

if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}

if ( wp_is_post_autosave( $post_id ) ) {
return;
}

if ( wp_is_post_revision( $post_id ) ) {
return;
}

$subtitle = wp_kses_post( $_POST['subtitle'] );

if ( ! empty( $subtitle ) ) {
update_post_meta( $post_id, 'subtitle', $subtitle );
} else {
delete_post_meta( $post_id, 'subtitle' );
}

return $subtitle;
}
add_action( 'save_post', 'subtitle_meta_box_save', 10, 3 );

/**
* Register our subtitle metabox
*
* This doesn't have anything special applied to it for Gutenberg.
* If Publicsource switches to Gutenberg, read https://developer.wordpress.org/block-editor/developers/backward-compatibility/meta-box/ and update this box as necessary.
*/
add_action(
'add_meta_boxes',
function() {
add_meta_box(
'subtitle', // id
__( 'Subtitle', 'allonsy' ), // title
'subtitle_meta_box_display', // callback
array( 'post' ), // screen
'normal', // context
'high' // priority
);
}
);
14 changes: 14 additions & 0 deletions single.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@
<section <?php post_class('main-content') ?> id="post-<?php the_ID(); ?>">
<?php if( get_theme_mod('internal-title-bar') == '' ) { ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
<?php
$subtitle = get_post_custom_values( 'subtitle', get_the_ID() );
if ( ! empty( $subtitle ) ) {
// should be single value post meta, but if it isn't....
if ( is_array( $subtitle ) ) {
$subtitle = $subtitle[0];
}

printf(
'<h2 class="subtitle">%1$s</h2>',
esc_html( $subtitle ),
);
}
?>
<?php if( get_theme_mod('about-the-author') != '' ): ?>
<div class="post-meta"><p><span class="author-name author-date">By <a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>" title="<?php echo esc_attr( get_the_author() ); ?>"><?php the_author(); ?></a> • <?php echo get_the_date( 'M j, Y', $post->ID ); ?></span></p></div>
<?php endif; ?>
Expand Down