Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 91442105fb01fc34bee6d8d38cf8a52580682fb2 @andrewryno committed Dec 22, 2011
Showing with 357 additions and 0 deletions.
  1. +47 −0 readme.txt
  2. BIN screenshot-1.png
  3. BIN screenshot-2.png
  4. +310 −0 unique-page-sidebars.php
@@ -0,0 +1,47 @@
+=== Unique Page Sidebars ===
+Contributors: andrewryno
+Donate link: http://andrewryno.com/plugins/unique-page-sidebars/
+Tags: sidebars, dynamic sidebars, sidebar management, dynamic widgets, widgets per page, sidebars per page
+Tested up to: 3.3
+Stable tag: 0.1
+
+This plugin allows for the creation and management of widget sidebars on a per-page basis from a single dynamic_sidebar() call in your theme's sidebar.
+
+== Description ==
+
+Over the years developing WordPress themes, I have had many designs that require a different sidebar for just about every page on the site. While it has been possible to accomplish this using a combination of PHP if/else statements or plugins, I have yet to find a decent solution for managing sidebars on a page-by-page basis. Since many of the sites I now work on require this functionality, I decided it was time to write a rather simple but effective plugin that handles this. Therefore, I'm introducing Unique Page Sidebars.
+
+There are obviously some limitations to this plugin, and I don't expect me to use it on every site I develop, but I chose the most common use-case I have for needing dynamic sidebars, and developed this plugin to fit that need. That said, here is a list of features:
+
+* Creating an unlimited number of sidebars, each with customizable names, descriptions and before/after title/widget tags (see options of `register_sidebar()` on the WordPress codex)
+* Mapping any of the created sidebars to anywhere from 1 to unlimited number of pages
+* Only one line of code needed in your theme
+* Can easily default to a default sidebar
+
+However, there are a few drawbacks (some of which I'm looking to working out in subsequent versions):
+
+* Only one sidebar per-page
+* No 'default' options for new sidebars
+
+== Installation ==
+
+To install, download the .zip, unpack it and upload to your WordPress installation in /wp-content/plugins/. Log in to the admin backend and activate it on the Plugins page. You should now see another menu item under "Appearance" called "Manage Sidebars" which will be where you can manage all of the sidebars.
+
+In your theme, all you need to add is the following line where you would like the dynamic sidebar to show up:
+
+`<?php dynamic_sidebar( apply_filters( 'ryno_sidebar', 'default-sidebar-id' ) ); ?>`
+
+In the above snippet of code, 'default-sidebar-id' is simply the ID for your default sidebar which you should define in your functions.php file using `register_sidebar()`.
+
+== Frequently Asked Questions ==
+
+
+
+== Screenshots ==
+
+
+
+== Changelog ==
+
+= 0.1 =
+* Initial version
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,310 @@
+<?php
+/*
+Plugin Name: Unique Page Sidebars
+Plugin URI: http://andrewryno.com/plugins/unique-page-sidebars/
+Description: Allows for the creation of sidebars on a per-page basis all from a single dynamic_sidebar() call from where they should appear.
+Author: Andrew Ryno
+Version: 0.1
+Author URI: http://andrewryno.com/
+*/
+
+/**
+ * Register all hooks
+ */
+add_filter( 'ups_sidebar', 'ups_display_sidebar' );
+add_action( 'init', 'ups_options_init' );
+add_action( 'admin_init', 'ups_options_admin_init' );
+add_action( 'admin_menu', 'ups_options_add_page' );
+
+/**
+ * Displays the sidebar which is attached to the page being viewed.
+ *
+ * @since Unique Page Sidebars 0.1
+ */
+function ups_display_sidebar( $default_sidebar ) {
+ global $post;
+
+ $sidebars = get_option( 'ups_sidebars' );
+ foreach ( $sidebars as $id => $sidebar ) {
+ if ( array_key_exists( 'pages', $sidebar ) ) {
+ if ( array_key_exists( 'children', $sidebar ) && $sidebar['children'] == 'on' ) {
+ $child = array_key_exists( $post->post_parent, $sidebar['pages'] );
+ } else {
+ $child = false;
+ }
+ if ( array_key_exists( $post->ID, $sidebar['pages'] ) || $child ) {
+ return $id;
+ }
+ }
+ }
+
+ return $default_sidebar;
+}
+
+/**
+ * Add the options page to the "Appearance" admin menu
+ *
+ * @since Unique Page Sidebars 0.1
+ */
+function ups_options_add_page() {
+ add_theme_page( 'Manage Sidebars', 'Manage Sidebars', 'edit_theme_options', 'ups_sidebars', 'ups_sidebars_do_page' );
+}
+
+/**
+ * Registers all sidebars for use on the front-end and Widgets page
+ *
+ * @since Unique Page Sidebars 0.1
+ */
+function ups_options_init() {
+ $sidebars = get_option( 'ups_sidebars' );
+
+ if ( is_array( $sidebars ) ) {
+ foreach ( (array) $sidebars as $id => $sidebar ) {
+ unset( $sidebar['pages'] );
+ $sidebar['id'] = $id;
+ register_sidebar( $sidebar );
+ }
+ }
+}
+
+/**
+ * Adds the metaboxes to the main options page for the sidebars in the database.
+ *
+ * @since Unique Page Sidebars 0.1
+ */
+function ups_options_admin_init() {
+ wp_enqueue_script('common');
+ wp_enqueue_script('wp-lists');
+ wp_enqueue_script('postbox');
+
+ // Register setting to store all the sidebar options in the *_options table
+ register_setting( 'ups_sidebars_options', 'ups_sidebars', 'ups_sidebars_validate' );
+
+ $sidebars = get_option( 'ups_sidebars' );
+ if ( is_array( $sidebars ) && count ( $sidebars ) > 0 ) {
+ foreach ( $sidebars as $id => $sidebar ) {
+ add_meta_box(
+ esc_attr( $id ),
+ esc_html( $sidebar['name'] ),
+ 'ups_sidebar_do_meta_box',
+ 'ups_sidebars',
+ 'normal',
+ 'default',
+ array(
+ 'id' => esc_attr( $id ),
+ 'sidebar' => $sidebar
+ )
+ );
+
+ unset( $sidebar['pages'] );
+ $sidebar['id'] = esc_attr( $id );
+ register_sidebar( $sidebar );
+ }
+ } else {
+ add_meta_box( 'ups-sidebar-no-sidebars', 'No sidebars', 'ups_sidebar_no_sidebars', 'ups_sidebars', 'normal', 'default' );
+ }
+
+ // Sidebar metaboxes
+ add_meta_box( 'ups-sidebar-add-new-sidebar', 'Add New Sidebar', 'ups_sidebar_add_new_sidebar', 'ups_sidebars', 'side', 'default' );
+ add_meta_box( 'ups-sidebar-about-the-plugin', 'About the Plugin', 'ups_sidebar_about_the_plugin', 'ups_sidebars', 'side', 'default' );
+}
+
+function ups_sidebar_no_sidebars() {
+ ?>
+ <p>You haven&rsquo;t added any sidebars yet. Add one using the form on the right hand side!</p>
+ <?php
+}
+
+/**
+ * Callback function which creates the theme page and adds a spot for the metaboxes
+ *
+ * @since Unique Page Sidebars 0.1
+ */
+function ups_sidebars_do_page() {
+ if ( ! isset( $_REQUEST['settings-updated'] ) )
+ $_REQUEST['settings-updated'] = false;
+ ?>
+ <div class="wrap">
+ <?php screen_icon(); ?><h2>Manage Sidebars</h2>
+ <?php if ( false !== $_REQUEST['settings-updated'] ) : ?>
+ <div class="updated fade"><p><strong>Sidebar settings saved.</strong> You can now go manage the <a href="<?php echo get_admin_url(); ?>widgets.php">widgets</a> for your sidebars.</p></div>
+ <?php endif; ?>
+ <div id="poststuff" class="metabox-holder has-right-sidebar">
+ <div id="post-body" class="has-sidebar">
+ <div id="post-body-content" class="has-sidebar-content">
+ <form method="post" action="options.php">
+ <?php settings_fields( 'ups_sidebars_options' ); ?>
+ <?php do_meta_boxes( 'ups_sidebars', 'normal', null ); ?>
+ </form>
+ </div>
+ </div>
+ <div id="side-info-column" class="inner-sidebar">
+ <?php do_meta_boxes( 'ups_sidebars', 'side', null ); ?>
+ </div>
+ </div>
+ </div>
+ <?php
+}
+
+/**
+ * Callback function which adds the content of the metaboxes for each sidebar
+ *
+ * @since Unique Page Sidebars 0.1
+ */
+function ups_sidebar_do_meta_box( $post, $metabox ) {
+ $sidebars = get_option( 'ups_sidebars' );
+ $sidebar_id = esc_attr( $metabox['args']['id'] );
+ $sidebar = $sidebars[$sidebar_id];
+
+ if ( ! isset( $sidebar['pages'] ) ) {
+ $sidebar['pages'] = array();
+ }
+
+ $options_fields = array(
+ 'name' => 'Name',
+ 'description' => 'Description',
+ 'before_title' => 'Before Title',
+ 'after_title' => 'After Title',
+ 'before_widget' => 'Before Widget',
+ 'after_widget' => 'After Widget',
+ 'children' => 'Child Behavior'
+ );
+
+ $get_posts = new WP_Query;
+ $posts = $get_posts->query( array(
+ 'offset' => 0,
+ 'order' => 'ASC',
+ 'orderby' => 'title',
+ 'posts_per_page' => -1,
+ 'post_type' => 'page',
+ 'suppress_filters' => true,
+ 'update_post_term_cache' => false,
+ 'update_post_meta_cache' => false
+ ) );
+ ?>
+ <div class="wp-tab-wrapper" style="float: left; width: 25%;">
+ <ul class="wp-tab-bar">
+ <li class="wp-tab-active">All Pages</li>
+ </ul>
+ <div class="wp-tab-panel">
+ <ul id="pagechecklist" class="categorychecklist form-no-clear" style="margin-top:0">
+ <?php foreach ( $posts as $post ) : ?>
+ <li>
+ <label>
+ <?php
+ $checked = '';
+ if ( array_key_exists( $post->ID, $sidebar['pages'] ) ) {
+ $checked = ' checked="checked"';
+ }
+ ?>
+ <input type="checkbox" class="menu-item-checkbox" name="ups_sidebars[<?php echo esc_attr( $sidebar_id ); ?>][pages][<?php echo esc_attr( $post->ID ); ?>]" value="<?php echo esc_attr( $post->post_title ); ?>"<?php echo $checked; ?> />
+ <?php echo esc_html( $post->post_title ); ?>
+ </label>
+ </li>
+ <?php endforeach; ?>
+ </ul>
+ </div>
+ </div>
+ <div style="float: right; width: 70%;">
+ <table class="form-table">
+ <?php foreach ( $options_fields as $id => $label ) : ?>
+ <tr valign="top">
+ <th scope="row"><label for="ups_sidebars[<?php echo esc_attr( $sidebar_id ); ?>][<?php echo esc_attr( $id ); ?>]"><?php echo esc_html( $label ); ?></label></th>
+ <td>
+ <?php if ( 'children' == $id ) : ?>
+ <?php
+ $checked = '';
+ if ( array_key_exists( 'children', $sidebar ) && $sidebar['children'] == 'on' ) {
+ $checked = ' checked="checked"';
+ }
+ ?>
+ <label>
+ <input type="checkbox" name="ups_sidebars[<?php echo esc_attr( $sidebar_id ); ?>][<?php echo esc_attr( $id ); ?>]" value="on" id="ups_sidebars[<?php echo esc_attr( $sidebar_id ); ?>][<?php echo esc_attr( $id ); ?>]"<?php echo $checked; ?> />
+ <span class="description">Set page children to use the parent page sidebar by default?</span>
+ </label>
+ <?php else : ?>
+ <input id="ups_sidebars[<?php echo esc_attr( $sidebar_id ); ?>][<?php echo esc_attr( $id ); ?>]" class="regular-text" type="text" name="ups_sidebars[<?php echo esc_attr( $sidebar_id ); ?>][<?php echo esc_attr( $id ); ?>]" value="<?php echo esc_html( $sidebar[$id] ); ?>" />
+ <?php endif; ?>
+ </td>
+ </tr>
+ <?php endforeach; ?>
+ </table>
+ </div>
+ <div class="clear submitbox">
+ <input type="submit" class="button-primary" value="Save all sidebars" />&nbsp;&nbsp;&nbsp;
+ <label><input type="checkbox" name="ups_sidebars[delete]" value="<?php echo esc_attr( $sidebar_id ); ?>" /> Delete this sidebar?</label>
+ </div>
+ <?php
+}
+
+/**
+ * Validates and handles all the post data (adding, updating, deleting sidebars)
+ *
+ * @since Unique Page Sidebars 0.1
+ */
+function ups_sidebars_validate( $input ) {
+ if ( isset( $input['add_sidebar'] ) ) {
+ $sidebars = get_option( 'ups_sidebars' );
+ if ( '' != $input['add_sidebar'] ) {
+ $sidebar_num = count( $sidebars ) + 1;
+ $sidebars['ups-sidebar-' . $sidebar_num] = array(
+ 'name' => esc_html( $input['add_sidebar'] ),
+ 'description' => '',
+ 'before_title' => '',
+ 'after_title' => '',
+ 'before_widget' => '',
+ 'after_widget' => '',
+ 'pages' => array(),
+ 'children' => 'off'
+ );
+ }
+ return $sidebars;
+ }
+
+ if ( isset( $input['delete'] ) ) {
+ foreach ( (array) $input['delete'] as $delete_id ) {
+ unset( $input[$delete_id] );
+ }
+ unset( $input['delete'] );
+ return $input;
+ }
+
+ return $input;
+}
+
+/**
+ * Handles the content of the metabox which allows adding new sidebars
+ *
+ * @since Unique Page Sidebars 0.1
+ */
+function ups_sidebar_add_new_sidebar() {
+ ?>
+ <form method="post" action="options.php" id="add-new-sidebar">
+ <?php settings_fields( 'ups_sidebars_options' ); ?>
+ <table class="form-table">
+ <tr valign="top">
+ <th scope="row">Name</th>
+ <td>
+ <input id="ups_sidebars[add_sidebar]" class="text" type="text" name="ups_sidebars[add_sidebar]" value="" />
+ </td>
+ </tr>
+ </table>
+ <p class="submit" style="padding: 0;">
+ <input type="submit" class="button-primary" value="Add Sidebar" />
+ </p>
+ </form>
+ <?php
+}
+
+/**
+ * Handles the content of the metabox that describes the plugin
+ *
+ * @since Unique Page Sidebars 0.1
+ */
+function ups_sidebar_about_the_plugin() {
+ ?>
+ <p>This plugin was developed by <a href="http://andrewryno.com/">Andrew Ryno</a>, a WordPress developer based in Phoenix, AZ who never found a decent solution to having sidebars on different pages.</p>
+ <p>Like the plugin? Think it could be improved? Feel free to contribute over at <a href="http://github.com/andrewryno">GitHub</a>!</p>
+ <p>If you have any other feedback or need help, go ahead and <a href="http://andrewryno.com/plugins/unique-page-sidebars/">leave a comment</a>.</p>
+ <?php
+}

0 comments on commit 9144210

Please sign in to comment.