1

I am trying to add some PHP code before my WooCommerce product page sidebar content and can't for the life of me find where to do that. I have tried editing sidebar.php, using the 'woocommerce_sidebar' hook but cannot make this work.

I simply want some PHP code to be visible above the widget content in <div class="sidebar-content"

Anybody have a simple solution for this? get_sidebar( 'shop' ); shop is being called, but how do I get some extra code in there?

1

WooCommerce allows you to replicate/replace it's template files by using the exact same file name and placing them in your theme directory using the same path structure they have. So in your theme directory, you add a folder called woocommerce and then within that, you place folder and template files the same way they have the organized.

So you would be creating the following: yourtheme/woocommerce/global/sidebar.php

You can read all about the whole process here: https://docs.woocommerce.com/document/template-structure/

Now, in that file, we have the following line: get_sidebar( 'shop' );

You'll want to replace that with: get_sidebar( 'shop-yourtheme' );

Not in the WooCommerce one, but in the duplicate you created in your theme.

Now, in your theme directory you want to create a new file called: sidebar-shop-yourtheme.php.

In your functions.php you have to register a new sidebar:

<?php if ( function_exists ('register_sidebar')) { 
    register_sidebar ('shop-yourtheme'); 
} ?>

And NOW we get to the point where you can add your own PHP code into the sidebar. Open your new sidebar-shop-yourtheme.php and add the following:

<?php
/**
* The shop sidebar widget area
* @package yourtheme
*/
if( !is_active_sidebar( 'shop-yourtheme' ) ) {
    return;
}
?>
<aside id="secondary" class="widget-area">
<?php
    // Start Adding your PHP here.
    dynamic_sidebar( 'shop-yourtheme' );
?>
</aside><!-- #secondary -->

As indicated in the code snippet, if you want your custom PHP appearing before the widgets start adding it above the dynamic_sidebar().

| improve this answer | |
1

Tony offers a good solution, another would be to add the PHP Code Widget (https://wordpress.org/plugins/php-code-widget/) or PHP Everywhere plugin, which lets you add PHP to a text widget (https://wordpress.org/plugins/php-everywhere/).

Note that the first one I recommend is tested only up to WP 5.2.5 but seems to work okay in my 5.3 installation - ymmv.

Personally I prefer to do as Tony recommends and modify my template files, however if you're not working with a Child Theme then you need to be wary of Theme updates overwriting your mods.....a plugin will avoid that.

| improve this answer | |
  • Good call Trisha, I’m not familiar with that plugin but it seems like a good way to ease the pain for a lot of users that may not want to start coding their own child themes outright but instead just leverage some PHP know how into extending what their sites can do. – Tony Djukic Apr 29 at 1:00
  • Exactly, I actually prefer your solution, but I agree a lot of users aren't at that stage yet. I've used the first plugin for many years on a few sites where a Child Theme isn't an option and it's great, I'm hoping it will get updated soon! – Trisha Apr 30 at 21:06
  • For me that plugin was 'Widget Context' - that was literally the ONLY certainty that would go on every site I built when I first started. – Tony Djukic May 1 at 3:38
  • @TonyDjukic wow - that looks like a great plugin, I'll have to give it a spin.... :-) – Trisha May 2 at 5:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.