Roots Sidebar Display

Whipped up this Gist so that clients of mine can make decisions on showing/hiding sidebars without changing the template of the page. The metabox shows on all “public” post types, though this can be filtered.

Archived below for reference (though gist is prettier):

<?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;
1 Like

Thanks for sharing this.

Your gist offers one of the features that is already included in my Roots Wrapper Override plugin.

My plugin will also allow the user to select what sidebar template to use, and will display the options in a dropdown using ‘nice names’ (like custom page templates) to make it easier for clients to use.

My plugin also allows the same overrides for the Roots base template, and any other page element added via the Roots_Wrapping class.

If your clients ever need anything more advanced you should check it out.

1 Like

Hey @Foxaii , is this plugin still compatible/supported in Sage 9?

I don’t have a Sage 9 compatible version yet but will consider it when Sage 9 gets closer to release.

3 Likes

Cool. Please ping me if you do that! Thanks.