Skip to content

Instantly share code, notes, and snippets.

@mboynes
Forked from cassler/sidebar-selector.php
Created April 8, 2014 05:00
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 mboynes/10092746 to your computer and use it in GitHub Desktop.
Save mboynes/10092746 to your computer and use it in GitHub Desktop.
<?php
/**
* Sidebar Picker
*/
if ( !class_exists( 'My_Sidebar_Picker' ) ) :
class My_Sidebar_Picker {
private static $instance;
private $dynamic_widget_areas;
private function __construct() {
/* Don't do anything, needs to be initialized via instance() method */
}
public static function instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new My_Sidebar_Picker;
self::$instance->setup();
}
return self::$instance;
}
public function setup() {
add_action( 'save_post', array( $this, 'save_sidebar_link' ) );
add_action( 'admin_print_styles-post.php', array( $this, 'sidebar_css' ) );
add_action( 'admin_print_styles-post-new.php', array( $this, 'sidebar_css' ) );
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
$this->dynamic_widget_areas = array(
/* rename or create new dynamic sidebars */
"Sidebar 01",
"Sidebar 02",
"Sidebar 03",
"Sidebar 04",
"Sidebar 05",
"Sidebar 06",
"Sidebar 07",
"Search Template",
);
if ( function_exists('register_sidebar') ) {
foreach ( $this->dynamic_widget_areas as $widget_area_name ) {
register_sidebar( array(
'name'=> $widget_area_name,
'before_widget' => '<div id="%1$s" class="widget %2$s left half">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widgettitle">',
'after_title' => '</h3>',
) );
}
}
}
public function add_meta_boxes() {
add_meta_box( "sidebar_meta", "Sidebar Selection", array( $this, "sidebar_link" ), "page", "side", "default" );
}
public function save_sidebar_link( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( ! isset( $_POST['some_nonce_field'] ) ) {
return;
}
check_admin_referer( 'some_nonce_key', 'some_nonce_field' );
if ( in_array( $_POST['link'], $this->dynamic_widget_areas ) ) {
update_post_meta( $post_id, "_sidebar", $_POST["link"] );
} else {
delete_post_meta( $post_id, '_sidebar' );
}
}
public function sidebar_link( $post ) {
$link = get_post_meta( $post->ID, '_sidebar', true );
wp_nonce_field( 'some_nonce_key', 'some_nonce_field' );
?>
<div class="link_header">
<select name="link" class="sidebar-selection">
<option value=''>Select Sidebar</option>
<option value=''>-----------------------</option>
<?php foreach ( $this->dynamic_widget_areas as $list ) : ?>
<option <?php selected( $list, $link ) ?>><?php echo esc_html( $list ); ?></option>
<?php endforeach ?>
</select><br />
</div>
<p>Select sidebar to use on this page.</p>
<?php
}
public function sidebar_css() {
echo'
<style type="text/css">
.sidebar-selection{width:100%;}
</style>
';
}
}
function My_Sidebar_Picker() {
return My_Sidebar_Picker::instance();
}
add_action( 'after_setup_theme', 'My_Sidebar_Picker' );
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment