Skip to content

Instantly share code, notes, and snippets.

@farinspace
Created November 25, 2019 03:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save farinspace/33daf115639bddeb3462d3222f07a08f to your computer and use it in GitHub Desktop.
Save farinspace/33daf115639bddeb3462d3222f07a08f to your computer and use it in GitHub Desktop.
Adding meta boxes
<?php
add_action( 'add_meta_boxes', 'my_register_meta_boxes' );
/**
* Register meta boxes.
*/
function my_register_meta_boxes() {
// id, title, callback, screen, context, priority, callback_args
add_meta_box( 'nooz-meta-box__coverage-source', 'Source', 'my_get_source_meta_box', 'nooz_coverage', 'normal', 'high', NULL );
}
/**
* Get source meta box content.
*/
function my_get_source_meta_box( $post ) {
wp_nonce_field( 'mdnooz_source', 'mdnooz_source_nonce' );
include( __DIR__ . '/coverage-source-meta.php' );
}
add_action( 'save_post', 'my_save_source_meta_box' );
/**
* Save source meta box data.
*/
function my_save_source_meta_box( $post_id ) {
if ( ! my_can_save_meta_box( $post_id, 'mdnooz_source' ) ) return;
update_post_meta( $post_id, '_mdnooz_link_url', $_POST['_mdnooz_link_url'] );
update_post_meta( $post_id, '_mdnooz_link_target', isset( $_POST['_mdnooz_link_target'] ) ? $_POST['_mdnooz_link_target'] : '' );
update_post_meta( $post_id, '_mdnooz_source', $_POST['_mdnooz_source'] );
}
/**
* Checks if meta box data should be saved. If nonce will also be checked
* if a value is given.
*
* @param int $post_id Post ID
* @param string $nonce_id Nonce action name (nonce_id)
*
* @return bool
*/
function my_can_save_meta_box( $post_id, $nonce_id = NULL ) {
$is_post_type = 'nooz_coverage' === get_post_type( $post_id );
$is_valid_nonce = is_null( $nonce_id ) || ( isset( $_POST[$nonce_id . '_nonce'] ) && wp_verify_nonce( $_POST[$nonce_id . '_nonce'], $nonce_id ) );
$is_autosave = defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE;
return $is_post_type && $is_valid_nonce && ! $is_autosave;
}
<div class="nooz-meta-box__body">
<div class="nooz-meta-box-field-group nooz-meta-box-field-group--link">
<div class="nooz-meta-box-field nooz-meta-box-field--link-url nooz-meta-box-field--text">
<?php $field_name = '_mdnooz_link_url'; $field_value = get_post_meta( $post->ID, $field_name, TRUE ); ?>
<label class="nooz-meta-box-field__label" for="<?php echo $field_name; ?>"><?php _e( 'URL', 'mdnooz' ); ?></label>
<input class="nooz-meta-box-field__input nooz-meta-box-field__input--text" type="text" name="<?php echo $field_name; ?>" value="<?php echo $field_value; ?>">
<p class="nooz-meta-box-field__description"><?php _e( 'The URL of the news coverage.', 'mdnooz' ); ?></p>
</div>
<div class="nooz-meta-box-field nooz-meta-box-field--link-target nooz-meta-box-field--checkbox">
<?php $field_name = '_mdnooz_link_target'; $field_value = metadata_exists( 'post', $post->ID, $field_name ) ? get_post_meta( $post->ID, $field_name, TRUE ) : '_blank'; ?>
<input class="nooz-meta-box-field__input nooz-meta-box-field__input--checkbox" type="checkbox" <?php checked( $field_value, '_blank' ); ?> name="<?php echo $field_name; ?>" value="_blank">
<label class="nooz-meta-box-field__label nooz-meta-box-field__label--inline" for="<?php echo $field_name; ?>"><?php _e( 'Open link in a new tab', 'mdnooz' ); ?></label>
</div>
</div>
<div class="nooz-meta-box-field nooz-meta-box-field--text">
<?php $field_name = '_mdnooz_source'; $field_value = get_post_meta( $post->ID, $field_name, TRUE ); ?>
<label class="nooz-meta-box-field__label"><?php _e( 'Source name', 'mdnooz' ); ?></label>
<input class="nooz-meta-box-field__input nooz-meta-box-field__input--text" type="text" id="<?php echo $field_name; ?>" name="<?php echo $field_name; ?>" value="<?php echo esc_attr( $field_value ); ?>">
<p class="nooz-meta-box-field__description"><?php _e( 'The name of the source (e.g. Mashable, PR Newswire, CNN).', 'mdnooz' ); ?></p>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment