Skip to content

Instantly share code, notes, and snippets.

@joshlevinson
Created October 4, 2014 03:10
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshlevinson/d92b0a3d2c63ecd62d85 to your computer and use it in GitHub Desktop.
Save joshlevinson/d92b0a3d2c63ecd62d85 to your computer and use it in GitHub Desktop.
Roots Sidebar Display
<?php
/*
* Allows manual override on a per page/post basis
* for the display of the Roots Sidebar
*
* Allows for filtering the post types that show the metabox
*
*/
class roots_sidebar_class {
public $slug = 'roots_sidebar_display';
/**
* Hook into the appropriate actions when the class is constructed.
*/
public function __construct() {
add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) );
add_action( 'save_post', array( $this, 'save' ) );
add_filter( 'roots/display_sidebar', array( $this, 'roots_sidebar_display_filter' ), 0 );
}
/**
* Adds the meta box container.
*/
public function add_meta_box( $post_type ) {
$post_types = apply_filters( $this->slug . '_post_types',
get_post_types(
array(
'public' => true,
'show_ui' => true
)
)
);
if ( in_array( $post_type, $post_types ) ) {
add_meta_box(
$this->slug
, __( 'Select whether to show or hide sidebar', $this->slug )
, array( $this, 'render_meta_box_content' )
, $post_type
, 'advanced'
, 'high'
);
}
}
/**
* Save the meta when the post is saved.
*
* @param int $post_id The ID of the post being saved.
*/
public function save( $post_id ) {
/*
* We need to verify this came from the our screen and with proper authorization,
* because save_post can be triggered at other times.
*/
// Check if our nonce is set.
if ( ! isset( $_POST[ $this->slug . '_nonce' ] ) ) {
return $post_id;
}
$nonce = $_POST[ $this->slug . '_nonce' ];
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $nonce, $this->slug ) ) {
return $post_id;
}
// If this is an autosave, our form has not been submitted,
// so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
// Check the user's permissions.
if ( 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return $post_id;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
}
/* OK, its safe for us to save the data now. */
// Sanitize the user input.
if ( ! isset( $_POST[ $this->slug ] ) ) {
delete_post_meta( $post_id, '_' . $this->slug );
return $post_id;
}
if ( empty( $_POST[ $this->slug ] ) ) {
delete_post_meta( $post_id, '_' . $this->slug );
return $post_id;
}
$display = sanitize_text_field( $_POST[ $this->slug ] );
// Update the meta field.
update_post_meta( $post_id, '_' . $this->slug, $display );
}
/**
* Render Meta Box content.
*
* @param WP_Post $post The post object.
*/
public function render_meta_box_content( $post ) {
// Add an nonce field so we can check for it later.
wp_nonce_field( $this->slug, $this->slug . '_nonce' );
// Use get_post_meta to retrieve an existing value from the database.
$display = get_post_meta( $post->ID, '_' . $this->slug, true );
echo '<select name="' . $this->slug . '">';
echo '<option value="">' . __( 'Make a choice, or leave at default', $this->slug ) . '</option>';
echo '<option value="show" ' . selected( $display, 'show', false ) . '>' . __( 'Show sidebar', $this->slug ) . '</option>';
echo '<option value="hide" ' . selected( $display, 'hide', false ) . '>' . __( 'Hide sidebar', $this->slug ) . '</option>';
echo '</select>';
}
function roots_sidebar_display_filter( $sidebar ) {
global $post;
$display = get_post_meta( $post->ID, '_' . $this->slug, true );
switch ( $display ) {
case 'show':
return true;
case 'hide':
return false;
default:
return $sidebar;
}
return $sidebar;
}
}
new roots_sidebar_class;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment