Site logo

Archived topic

How do I add a custom sidebar?

19 replies · Started by Peter Islin Nielsen on September 20, 2015

Viewing posts 1–15 of 20

Hi Tom!

First of all: It's an absolute pleasure to work with this awesome theme! Everything is so smooth, tidy and well crafted. Keep up the good work :-)

There is one thing, though, that I can't wrap my head around: How do I add a custom sidebar to my WooCommerce pages? I don't want to use one of the standard sidebars that comes with GP, so I'm afraid this will not work in my case. I've come as far as to register the custom sidebar in my functions.php, and it also shows up nicely in the WP backend. But what do I do next? I've looked a bit at the do_action('generate_sidebars'), but I think I need some guidance...

Thanks in advance :-)

Hi Tom,

Yeah, it's by far the best theme I've worked with. It beats them all... Better throw you a good review when I get the time :-)

I've already tried out Display Widgets, Widget Context and other plugins of that sort – and it works, but it doesn't quite get me all the way. It works great with the display/hide widgets on my other pages and post, but on my WooCommerce pages I need a fresh sidebar. Is it possible? It will make it so much easier for my client to maintain that way.

Thanks!

What exactly do you mean by "fresh sidebar"? Using the plugins you listed is probably the easiest way.

This plugin is cool, but I'm not sure if it's integrated with WooCommerce: https://wordpress.org/plugins/custom-sidebars/

In the WP backend I have a lot of standard GP widgets like "Right Sidebar", "Left Sidebar" and so on. I would like an extra sidebar callede "WooCommerce Sidebar" where I can put all my WooCommerce related stuff to show on all WooCommerce pages. Normally I would register a new widget in my functions.php and display it on a template with <?php dynamic_sidebar( 'woocommerce_sidebar' ); ?>, but I can't quite get it to work in GP.

Ah, so you would have to duplicate the sidebar.php or sidebar-left.php files found in the parent theme.

Call it sidebar-woocommerce.php or something. This file would be the same as the others, but would call your custom widget area.

Then duplicate the woocommerce.php file found in the parent theme, and add it to your child theme.

In there, find do_action('generate_sidebars') and replace it with: do_action('generate_woocommerce_sidebars')

Then you would add a function like this:

add_action('generate_woocommerce_sidebars','generate_construct_sidebars');
function generate_construct_sidebars()
{
	get_sidebar('woocommerce'); 
	
}

That should get you on your way :)

Yes, that's exactly what I want :-)

But it doesn't seem to work – I've added this to my functions.php:

function arphabet_widgets_init() {

	register_sidebar( array(
		'name'          => 'WooCommerce Sidebar',
		'id'            => 'woocommerce_sidebar',
		'before_widget' => '<div>',
		'after_widget'  => '</div>',
		'before_title'  => '<h4>',
		'after_title'   => '</h4>',
	) );

}
add_action( 'widgets_init', 'arphabet_widgets_init' );

add_action('generate_woocommerce_sidebars','generate_contsruct_sidebars');
function generate_contsruct_sidebars()
{
	get_sidebar('woocommerce'); 
	
}

Then I copied the sidebar.php file from GP and copied it to my child theme folder. I renamed it to sidebar-woocommerce.php. After that I copied the woocommerce.php from GP and placed it in my child theme folder as well. I found the do_action('generate_sidebars') and changed it to do_action('generate_woocommerce_sidebars') in woocommerce.php.

In the WP backed the widget shows up – and I can interact with it and drag things onto it. But it doesn't show on the front page.

Inside your sidebar-woocommerce.php file did you edit the call to the correct registered sidebar?

Auch, I totally forgot to do that. Thanks, Tom. Premium support once again :-)

Glad I could help :)

Tom,

I am trying to do the same thing as Peter was here a year ago, but it looks like the woocommerce.php file has changed since 1.3.22 and there is no do_action('generate_sidebars'). How to proceed?

Alternatively, I've tried the DisplayWidgets plugin you mentioned and several other custom sidebar plugins without any luck on the WooCommerce pages. I just need a right-sidebar that displays shop-related widgets instead of the blog-related widgets.

Thanks for any help.

Ah yes, GP no longer has a woocommerce.php file and uses hooks instead.

Let's try something like this...

add_action( 'wp','tu_woocommerce_sidebar' );
function tu_woocommerce_sidebar()
{
    if ( function_exists( 'is_woocommerce' ) && is_woocommerce() ) {
        remove_action( 'generate_sidebars','generate_construct_sidebars' );
        add_action( 'generate_sidebars','tu_construct_woocommerce_sidebar' );
    }
}

function tu_construct_woocommerce_sidebar() {
    get_sidebar( 'woocommerce' );
}

You'll still need to register the sidebar etc.. However this should replace the need for changing the woocommerce.php file.

Let me know :)

Hello,

I have a similar problem, i know that it's possible with widget but for other reason i search an other way.

In a child theme i create single-newpage.php, content-newpage.php and sidebar-newpage.php

In single-newpage.php i replace

<?php 
do_action('generate_sidebars');
get_footer();

by

<?php 
do_action('generate_newpage_sidebars');
get_footer();

It's correct ?

other thing, what is the code for functions.php ?

thanck you

but why Woocommerce ? i don't use woo for this website

This archived topic is closed to new replies.