I guess there are plugins to do this but I need a fairly short script and easily transferable from one theme to another allowing users to search for significant customizations to easily create a sidebar and therefore Widget zone specific to a page. One option present on the editing page which would also occasionally disable the sidebar without the need for a template of "no sidebar" or "full width" screen type. Customers and users often struggle with the notion of template page.
The specification is quite simple, offering a meta box offers three options for each page of a site no sidebar, sidebar default specific sidebar.
The management of the width of the page in Front will be in CSS, via the addition of classes on the body tag with the help of body_class ().
In theme, sidebar-dynamic.php file (for example) will be added and called in the page template using existing get_sidebar ('dynamic').
Necessary code to the file functions.php of the theme:
The specification is quite simple, offering a meta box offers three options for each page of a site no sidebar, sidebar default specific sidebar.
The management of the width of the page in Front will be in CSS, via the addition of classes on the body tag with the help of body_class ().
In theme, sidebar-dynamic.php file (for example) will be added and called in the page template using existing get_sidebar ('dynamic').
Necessary code to the file functions.php of the theme:
// gestion des sidebars
function sidebar_init() {
// default and permanent sidebar.
register_sidebar( array(
'name' => 'Sidebar',
'id' => 'default',
'description' => 'Default Sidebar',
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
) );
// on demand sidebar(s).
$args = array(
'sort_order' => 'ASC',
'sort_column' => 'post_title',
'hierarchical' => 0,
'meta_key' => 'sidebar_status',
'meta_value' => 'specific',
'post_type' => 'page',
'parent' => -1,
'child_of' => 0,
'post_status' => 'publish'
);
$pages = get_pages($args);
foreach ($pages as $page) {
$name = 'Sidebar '.esc_attr($page->post_title);
$id = 'sidebar-'.$page->ID;
$desc = 'Sidebar for '.esc_attr($page->post_title);
register_sidebar( array(
'name' => $name,
'id' => $id,
'description' => $desc,
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
) );
}
}
add_action( 'widgets_init', 'sidebar_init' );
// ajout de classes au body
function dynamicsidebar_body_class( $classes ) {
if(is_page()){
$status = get_post_meta(get_the_ID(),'sidebar_status',true);
switch ($status) {
case 'none':
$classes[]='no-sidebar';
break;
case 'specific':
if(is_active_sidebar('sidebar-'.get_the_ID())){
$classes[]='has-sidebar';
$classes[]='sidebar-specific';
}
else{
$classes[]='no-sidebar';
}
break;
case 'default':
if(is_active_sidebar('default')){
$classes[]='has-sidebar';
$classes[]='sidebar-default';
}
else{
$classes[]='no-sidebar';
}
break;
default:
$classes[]='no-sidebar';
break;
}
}
return $classes;
}
add_filter( 'body_class', 'dynamicsidebar_body_class' );
// ajout des metabox sur les pages
if ( is_admin() ) :
function sidebar_status_add_meta_box(){
add_meta_box('sidebar_status', 'Sidebar Status', 'sidebar_status_meta_box', 'page', 'side', 'high');
}
add_action( 'add_meta_boxes', 'sidebar_status_add_meta_box' );
function sidebar_status_meta_box(){
global $post;
?>
<label for="sidebar_status">Quel type de sidebar ? </label>
<em style="display:block;">Le choix d'une sidebar spécifique va vous créer une sidebar uniquement pour cette page que vous pourrez alors paramétrer</em>
<select style="display:block;" name="sidebar_status" id="sidebar_status">
<option value="none" <?php="" selected(="" get_post_meta($post-="">ID,'sidebar_status',true), 'none' ) ?> >None</option>
<option value="default" <?php="" selected(="" get_post_meta($post-="">ID,'sidebar_status',true), 'default' ) ?> >Default</option>
<option value="specific" <?php="" selected(="" get_post_meta($post-="">ID,'sidebar_status',true), 'specific' ) ?> >Create one</option>
</select>
<!--?php }
function save_sidebar_status_value( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if ( !current_user_can( 'edit_page', $post_id ) )
return;
if ( 'page' !== get_post_type($post_id) )
return;
if ( isset($_POST['sidebar_status']) ){
update_post_meta($post_id, 'sidebar_status', $_POST['sidebar_status']);
}
}
add_action( 'save_post', 'save_sidebar_status_value' );
endif;
?-->
0 Comments