Instantly share code, notes, and snippets.
Last active
August 29, 2015 14:21
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save cjwd-snippets/2427f77161df4ffe69be to your computer and use it in GitHub Desktop.
Dynamically widgetize wordpress footer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Spring\Extras; | |
?> | |
<footer id="colophon" class="site-footer" role="contentinfo"> | |
<?php Extras\widgetize_footer('footer-top', 4); // See lib/extras.php ?> | |
<?php Extras\widgetize_footer('footer-bottom', 4); // See lib/extras.php ?> | |
</footer> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
register_sidebar([ | |
'name' => __('Footer Top', 'cxc'), | |
'id' => 'footer-top', | |
'before_widget' => '<section class="layout__item widget %1$s %2$s">', | |
'after_widget' => '</section>', | |
'before_title' => '<h3>', | |
'after_title' => '</h3>', | |
]); | |
register_sidebar([ | |
'name' => __('Footer Bottom', 'cxc'), | |
'id' => 'footer-bottom', | |
'before_widget' => '<section class="layout__item widget %1$s %2$s">', | |
'after_widget' => '</section>', | |
'before_title' => '<h3>', | |
'after_title' => '</h3>', | |
]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Dynamically create grids for widget areas | |
* Must register sidebar with the following convention | |
* sidebar_name-[number] | |
*/ | |
function widgetize_area($sidebar_name, $max_widgets) { | |
$grid_class = ''; | |
$num_active_widgets = 0; | |
for ($i=1; $i < $max_widgets; $i++) { | |
if( is_active_sidebar( $sidebar_name . '-' . $i ) ) { | |
$num_active_widgets += 1; | |
} | |
} | |
if ( $num_active_widgets == 2 ) { | |
$grid_class = 'u-1/2'; | |
} elseif ( $num_active_widgets == 3) { | |
$grid_class = 'u-1/3-desk'; | |
} | |
if ( $num_active_widgets == 0) : ?> | |
<div class="site-info"> | |
<?php do_action( 'cxc_credits' ); ?> | |
<a href="http://wordpress.org/" rel="generator"><?php printf( __( 'Proudly powered by %s', 'cxc' ), 'WordPress' ); ?></a> | |
<span class="sep"> | </span> | |
<?php printf( __( 'Theme: %1$s by %2$s.', 'cxc' ), 'CXC', '<a href="http://chinarajames.com" rel="designer">CJ Web Design</a>' ); ?> | |
</div><!-- .site-info --> | |
<?php endif; ?> | |
<?php if( $num_active_widgets >= 1) : ?> | |
<div class="layout"> | |
<!-- | |
<?php for ($i=1; $i <= $num_active_widgets; $i++) : ?> | |
--><div class="layout__item <?php echo $grid_class; ?>"> | |
<?php if( is_active_sidebar( $sidebar_name . '-' . $i) ) : dynamic_sidebar( $sidebar_name . '-' . $i ); endif; ?> | |
</div><!-- | |
<?php endfor; ?>--> | |
</div> | |
<?php endif; ?> | |
<?php | |
} | |
/** | |
* Dynamically create grids based on active widgets | |
* get_option('sidebars_widgets') | |
* | |
*/ | |
function widgetize_footer($sidebar_name, $max_widgets) { | |
global $widget_class; | |
$num_active_widgets = get_option('sidebars_widgets'); | |
if( is_active_sidebar( $sidebar_name ) ) { | |
$num_active_widgets = count($num_active_widgets[$sidebar_name]); | |
} | |
if ( $num_active_widgets == 0) : ?> | |
<div class="site-info"> | |
<?php do_action( 'cxc_credits' ); ?> | |
<a href="http://wordpress.org/" rel="generator"><?php printf( __( 'Proudly powered by %s', 'cxc' ), 'WordPress' ); ?></a> | |
<span class="sep"> | </span> | |
<?php printf( __( 'Theme: %1$s by %2$s.', 'cxc' ), 'CXC', '<a href="http://chinarajames.com" rel="designer">CJ Web Design</a>' ); ?> | |
</div><!-- .site-info --> | |
<?php endif; ?> | |
<?php if( $num_active_widgets >= 1) : ?> | |
<div class="layout"> | |
<?php | |
add_filter( 'dynamic_sidebar_params', __NAMESPACE__ . '\\my_filter_dynamic_sidebar_params', 10); | |
?> | |
<?php if( is_active_sidebar( $sidebar_name ) ) : dynamic_sidebar( $sidebar_name ); endif;?> | |
</div> | |
<?php endif; ?> | |
<?php | |
return $widget_class; | |
} | |
function custom_widget_counter($params) { | |
global $my_widget_num; | |
$num_active_widgets = get_option('sidebars_widgets'); | |
if( is_active_sidebar( 'footer-top' ) ) { | |
$num_active_widgets = count($num_active_widgets['footer-top']); | |
} | |
$my_widget_num++; | |
$class = 'class="widget-' . $my_widget_num . ' '; | |
if( $my_widget_num % 2 ) : | |
$class .= 'u-1/3-lap u-1/4-desk '; | |
elseif( $my_widget_num == 4 ) : | |
$class .= 'u-1/2-lap u-1/4-desk '; | |
elseif ( $my_widget_num == 2 ) : | |
$class .= 'u-1/2-lap-and-up '; | |
else : | |
$class .= 'u-1/3-lap u-2/4-desk '; | |
endif; | |
$params[0]['before_widget'] = str_replace('class="', $class, $params[0]['before_widget']); | |
return $params; | |
} | |
function my_filter_dynamic_sidebar_params($params){ | |
static $sidebar_widget_count = array(); | |
$sidebar_id = $params[0]["id"]; | |
if (! isset($sidebar_widget_count[$sidebar_id])){ | |
$sidebar_widget_count[$sidebar_id] = 1; | |
} | |
$before_widget = $params[0]['before_widget']; | |
$class = $sidebar_widget_count[$sidebar_id] % 2 ? | |
"u-1/3-lap u-1/4-desk" : "u-1/3-lap u-2/4-desk"; | |
$class = $sidebar_widget_count[$sidebar_id] <= 2 && $sidebar_widget_count[$sidebar_id] % 2 == 0 ? | |
"u-1/2-lap-and-up" : "u-1/2-lap u-1/4-desk"; | |
$class .= " widget-index-" . $sidebar_widget_count[$sidebar_id]; | |
$class .= " widget-in-$sidebar_id"; | |
$before_widget = str_replace("class=\"", | |
"class=\"$class ", $before_widget); | |
$params[0]['before_widget'] = $before_widget; | |
$sidebar_widget_count[$sidebar_id]++; | |
return $params; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment