Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save prosenjit-manna/4cce501b9678bb9ce025 to your computer and use it in GitHub Desktop.
Save prosenjit-manna/4cce501b9678bb9ce025 to your computer and use it in GitHub Desktop.
Dynamic sidebars for use with ( custom metaboxes & fields for wordpress, https://github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress) along with smof options panel or options far,work by devin - Allows you to select how many automatically generated sidebars you want & select the sidebar of your choice per page from a metabox (cmb meta…
/* Author: Dan Beaven
* Company: Firestorm Graphics
* Web: http://firestorm-graphics.com
*/
* Released under the GPL license
* http://www.opensource.org/licenses/gpl-license.php
*
* This is an add-on for WordPress
* http://wordpress.org/
*
* **********************************************************************
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* **********************************************************************
*/
// Add this to the top near the declaration for categories
$of_sidebars = array();
$of_sidebars_obj = $GLOBALS['wp_registered_sidebars'];
foreach ($of_sidebars_obj as $option)
{
$of_sidebars[$option['id']] = $option['name'];
}
$sidebars_tmp = array_unshift($of_sidebars, "Select a sidebar:");
// Then add this to your field types and change id to your preference
$of_options[] = array( "name" => __("Sidebar", THEMENAME),
"desc" => "Select your sidebar of choice.",
"id" => "alt-sidebar",
"std" => "",
"type" => "select",
"options" => "$of_sidebars);
/* Author: Dan Beaven
* Company: Firestorm Graphics
* Web: http://firestorm-graphics.com
*/
* Released under the GPL license
* http://www.opensource.org/licenses/gpl-license.php
*
* This is an add-on for WordPress
* http://wordpress.org/
*
* **********************************************************************
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* **********************************************************************
*/
global $data, $num_sidebars;
$get_sidebar_num = $data['sidebar_num']; // pull sidebar qty from smof field with id "sidebar_num"
if($get_sidebar_num !="") {
$num_sidebars = $data['sidebar_num']; // Check if number of Sidebars is defined.
}
else {
$num_sidebars="2"; // If not then generate default of 2
}
$i=1;
while($i<=$num_sidebars)
{
if ( function_exists('register_sidebar')) {
register_sidebar(array('name'=>'sidebar-'.$i, // sidebar- name is sidebar plus a number
'before_widget' => '<article id="%1$s" class="row widget %2$s"><div class="sidebar-section eleven columns centered">',
'after_widget' => '</div></article><div class="clearfix"></div>',
'before_title' => '<h3><div class="sidebar-title"><strong>',
'after_title' => '</strong></div></h3>',
));
}// end if statement
$i++;
} // end while
/* Author: Dan Beaven
* Company: Firestorm Graphics
* Web: http://firestorm-graphics.com
*/
* Released under the GPL license
* http://www.opensource.org/licenses/gpl-license.php
*
* This is an add-on for WordPress
* http://wordpress.org/
*
* **********************************************************************
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* **********************************************************************
*/
//:: This is for use with optionsframework by devin - place in your functions.php file.
//:: This registers a default sidebar and then pulls the set amount of sidebars set in the theme options.
//:: Dont forget to change widget class's to suit your theme.
//:: Credit to us appreciated
global $wpdb;
// Default Sidebar
if ( function_exists('register_sidebar') ) {
$default_sidebar = array('Default Sidebar');
foreach ($default_sidebar as $side) {
register_sidebar(array(
'name' => $side,
'id' => 'sidebar',
'description' => __('Widgets in this area will be shown in the default Sidebar', FIRE_THEMENAME ),
'before_widget' => '<aside id="%1$s" class="widget %2$s well col-md-12">',
'after_widget' => '</aside>',
'before_title' => '<h4 class="widget-title">',
'after_title' => '</h4><hr>',
));
}
}
$sidebar_default = ( get_option( 'fire_sidebars_num' ) != '' ) ? get_option( 'fire_sidebars_num' ) : '2';
$sidebars_num = ( of_get_option('fire_sidebars_num') !='' ) ? of_get_option('fire_sidebars_num') : $sidebar_default;
// Custom Sidebar Columns
$i=1;
while( $i <= $sidebars_num )
{
if ( function_exists('register_sidebar') )
{
register_sidebar(
array(
'name' => __('Sidebar '.$i, 'YOURTHEME'),
'id' => 'sidebar'.$i,
'description' => __('Widgets in this area will be shown in Sidebar '.$i.'.', 'YOURTHEME' ),
'before_widget' => '<aside id="%1$s" class="widget %2$s well col-md-12">',
'after_widget' => '</aside>',
'before_title' => '<h4 class="widget-title">',
'after_title' => '</h4><hr>',
));
}
$i++;
}
/* Author: Dan Beaven
* Company: Firestorm Graphics
* Web: http://firestorm-graphics.com
*/
* Released under the GPL license
* http://www.opensource.org/licenses/gpl-license.php
*
* This is an add-on for WordPress
* http://wordpress.org/
*
* **********************************************************************
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* **********************************************************************
*/
// this is the select field for the custom metabox see for the script i refer to as cmb (https://github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress)
array(
'name' => 'Select Sidebar',
'desc' => 'Select your prefered widgetized sidebar to display.',
'id' => $prefix . '_sidebar_select',
'type' => 'sidebar_select',
'std' => '',
),
/* Author: Dan Beaven
* Company: Firestorm Graphics
* Web: http://firestorm-graphics.com
*/
* Released under the GPL license
* http://www.opensource.org/licenses/gpl-license.php
*
* This is an add-on for WordPress
* http://wordpress.org/
*
* **********************************************************************
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* **********************************************************************
*/
// PLace this in the options.php file in options framework
//:: Sidebars
$options[] = array(
'name' => __('Number of Sidebars', 'options_framework_theme'),
"desc" => __('Select qty sidebars', 'options_framework_theme'),
'plac' => __('Default is 2 sidebars', 'options_framework_theme'),
'tooltip' => __('Slide to your desired option.','options_framework_theme'),
'id' => $shortname.'_sidebars_num',
'std' => '',
'type' => 'text'
);
/* Author: Dan Beaven
* Company: Firestorm Graphics
* Web: http://firestorm-graphics.com
*/
* Released under the GPL license
* http://www.opensource.org/licenses/gpl-license.php
*
* This is an add-on for WordPress
* http://wordpress.org/
*
* **********************************************************************
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* **********************************************************************
*/
// create a new file and add the following, then in your functions.php do an include_once, this will register a new field type for cmb ( custom metaboxes & fields for wordpress, https://github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress), which will retrieve all registered sidebars.
<?php
/**
* Adds a new sidebar_select field type to CMB Metaboxes
*
* Builds the select box from all registered sidebars.
* Use in themes to create per page or post layout options without having to create new templates
*
*
*/
if ( is_admin() ) { // Optional is_admin check. I only include the metabox class on the admin side.
add_action( 'cmb_render_sidebar_select', 'firestorm_sidebar_select', 10, 2 );
add_filter( 'cmb_validate_sidebar_select', 'firestorm_validate_sidebar_select' );
}
/**
* Adds a custom field type to the meta box class
*
* @param array $field Your new field
* @param mixed $meta Current saved meta_value for the key
*/
function firestorm_sidebar_select( $field, $meta ) {
$sidebars = $GLOBALS['wp_registered_sidebars'];
echo '<select name="', $field['id'], '" id="', $field['id'], '">';
foreach ($sidebars as $option) {
echo '<option value="', $option['id'], '"', $meta == $option['id'] ? ' selected="selected"' : '', '>', $option['name'], '</option>';
}
echo '</select>';
echo '<p class="cmb_metabox_description">', $field['desc'], '</p>';
}
/**
* Validation function for sidebar_select meta box field
*
* @param string $new The new value to validate before saving
* @return bool|string If validates returns the value
*/
function firestorm_validate_sidebar_select( $new ) {
$sidebars = $GLOBALS['wp_registered_sidebars'];
if ( !array_key_exists( $new, (array)$sidebars ) ) $new ='';
return $new;
}
?>
/* Author: Dan Beaven
* Company: Firestorm Graphics
* Web: http://firestorm-graphics.com
*/
* Released under the GPL license
* http://www.opensource.org/licenses/gpl-license.php
*
* This is an add-on for WordPress
* http://wordpress.org/
*
* **********************************************************************
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* **********************************************************************
*/
// in your template files you can output the sidebars like:
create a sidebar.php file and place in root of the theme and <?php get_sidebar(); ?> in your templates, then add the following to sidebar.php,
<?php
global $post;
$sidebar_select = get_post_meta ($post->ID, 'YOUR_META_BOX_ID_HERE', true );
if( $sidebar_select !="") : // field set in metabox script
dynamic_sidebar( $sidebar_select );
else:
dynamic_sidebar( 'sidebar' ); // fallback sidebar if you want or just use return false;
endif;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment