Skip to content

Instantly share code, notes, and snippets.

@bradpotter bradpotter/functions.php
Last active Aug 29, 2015

Embed
What would you like to do?
Register a sidebar for a page that has a page template selected
add_action('widgets_init', 'gpb_register_sidebar');
/**
* Dynamically register sidebar for pages that use specified template
*
*/
function gpb_register_sidebar() {
global $post;
global $wp_registered_sidebars;
// Find all pages that use template with dynamic sidebar
$query = new WP_Query(array(
'meta_key' => '_wp_page_template',
'meta_value' => '^widget-template',
'meta_compare' => 'REGEXP',
'post_type' => 'page',
'nopaging' => true,
'orderby' => 'title',
'order' => 'ASC',
));
while ($query->have_posts()) {
$query->the_post();
$id = "gpb-{$post->ID}-sidebar";
if (!isset($wp_registered_sidebars[$id])) {
register_sidebar( array(
'name' => $post->post_title . ' Sidebar',
'id' => $id,
'description' => 'Sidebar widgets for ' . $post->post_title,
'before_widget' => '<div class="content-widget widget-area"><div class="wrap"><div id="%1$s" class="%2$s">',
'after_widget' => '</div></div></div>',
'before_title' => '<h4 class="widget-title" itemprop="headline">',
'after_title' => '</h4>',
) );
}
}
wp_reset_postdata();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.