Skip to content

Instantly share code, notes, and snippets.

@robneu
Last active March 2, 2016 17:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save robneu/4531458 to your computer and use it in GitHub Desktop.
Save robneu/4531458 to your computer and use it in GitHub Desktop.
Switch up the sidebars for various WooCommerce pages in a Genesis theme
<?php
add_filter( 'genesis_attr_sidebar-primary', 'prefix_store_sidebar_attr' );
/**
* Callback for filtering the Genesis sidebar on store pages.
*
* Add custom attributes for the custom filter.
*
* @param array $attributes The existing element attributes.
* @return array $attributes The updated element attributes.
*/
function prefix_store_sidebar_attr( $attributes ) {
if ( is_woocommerce() || is_cart() || is_checkout() || is_account_page() ) {
$attributes['class'] .= ' shop-sidebar';
}
if ( is_cart() ) {
$attributes['class'] .= ' cart-sidebar';
}
if ( is_checkout() ) {
$attributes['class'] .= ' checkout-sidebar';
}
if ( is_account_page() ) {
$attributes['class'] .= ' account-sidebar';
}
return $attributes;
}
add_action( 'genesis_sidebar', 'prefix_remove_store_sidebar', 0 );
/**
* Load a custom sidebar for WooCommerce store pages.
*
* @return void
*/
function prefix_remove_store_sidebar() {
if ( is_active_sidebar( 'store-sidebar' ) && ( is_woocommerce() || is_cart() || is_checkout() ) ) {
remove_action( 'genesis_sidebar', 'genesis_do_sidebar' );
add_action( 'genesis_sidebar', 'prefix_do_store_sidebar' );
}
}
/**
* Output a custom sidebar for WooCommerce store pages.
*
* @return void
*/
function prefix_do_store_sidebar() {
dynamic_sidebar( 'store-sidebar' );
}
add_action( 'genesis_sidebar', 'prefix_remove_account_sidebar', 0 );
/**
* Load a custom sidebar for WooCommerce account pages.
*
* @return void
*/
function prefix_remove_account_sidebar() {
if ( is_active_sidebar( 'account-sidebar' ) && is_account_page() ) {
remove_action( 'genesis_sidebar', 'genesis_do_sidebar' );
add_action( 'genesis_sidebar', 'prefix_do_account_sidebar' );
}
}
/**
* Output a custom sidebar for WooCommerce account pages.
*
* @return void
*/
function prefix_do_account_sidebar() {
dynamic_sidebar( 'account-sidebar' );
}
add_action( 'genesis_setup', 'prefix_register_store_sidebars', 25 );
/**
* Register custom sidebars for WooCommerce store pages.
*
* @return void
*/
function prefix_register_store_sidebars() {
genesis_register_sidebar( array(
'id' => 'store-sidebar',
'name' => __( 'Store Sidebar', 'themename' ),
'description' => __( 'This is the primary sidebar for the store.', 'themename' ),
) );
genesis_register_sidebar( array(
'id' => 'account-sidebar',
'name' => __( 'Account Sidebar', 'themename' ),
'description' => __( 'This is the primary sidebar for the account pages.', 'themename' ),
) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment