Skip to content

Instantly share code, notes, and snippets.

@dkotter
Created November 21, 2013 18:47
Show Gist options
  • Save dkotter/7587308 to your computer and use it in GitHub Desktop.
Save dkotter/7587308 to your computer and use it in GitHub Desktop.
<?php
/**
* Handle meta boxes that are used on multiple content types.
*
*/
class PM_Meta_Boxes {
private $_blacklisted_post_types = array(
'pm-best-of',
'pm-dentists',
'pm-top-doctors',
);
/**
* The only instance of the PM_Meta_Boxes object.
*
* @var PM_Meta_Boxes
*/
private static $instance;
/**
* Returns the main instance.
*
* @return PM_Meta_Boxes
*/
public static function instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new PM_Meta_Boxes;
self::$instance->setup_actions();
}
return self::$instance;
}
/**
* A dummy constructor.
*
* @return PM_Meta_Boxes
*/
private function __construct() {}
/**
* Initiate the main actions and filters for meta box related functionality.
*
* @return void
*/
public function setup_actions() {
// Add meta boxes
add_action( 'edit_form_after_title', array( $this, 'subhead' ) );
// Queues the metabox save actions
add_action( 'save_post', array( $this, 'save_subhead' ), 10, 2 );
}
/**
* Show subhead field under title field.
*
* @return void
*/
public function subhead() {
if ( in_array( get_post_type(), $this->_blacklisted_post_types ) )
return;
$subhead = get_post_meta( get_the_ID(), 'pm_subhead', true ) ? get_post_meta( get_the_ID(), 'pm_subhead', true ) : '';
?>
<div id="subhead">
<div id="titlewrap">
<input type="text" id="pm-subhead" name="pm-subhead" class="widefat" value="<?php echo esc_attr( $subhead ); ?>" placeholder="Enter sub-headline here" />
</div> <!-- end #titlewrap -->
</div> <!-- end #subhead -->
<?php
wp_nonce_field( 'save', 'pm-subhead-save' );
}
/**
* Save the subhead value.
*
* @param int $post_id Post ID.
* @param object $post Post object.
* @return void
*/
public function save_subhead( $post_id, $post ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if ( ! isset( $_POST['pm-subhead-save'] ) || ! wp_verify_nonce( $_POST['pm-subhead-save'], 'save' ) )
return;
if ( ! current_user_can( 'edit_post', $post_id ) )
return;
// Save subhead
if ( isset( $_POST['pm-subhead'] ) && '' !== $_POST['pm-subhead'] )
update_post_meta( $post_id, 'pm_subhead', wp_kses_post( $_POST['pm-subhead'] ) );
else
delete_post_meta( $post_id, 'pm_subhead' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment