Remove Woocommerce sidebar from any theme - php

I'm using WordPress 4.9.4 running Twenty Seventeen Child Theme theme with Woocommerce Version 3.3.4. I am trying to remove the sidebar… I have tried using this:
remove_action('woocommerce_sidebar','woocommerce_get_sidebar',10);
But haven't found the right one yet.
How do I remove all sidebars?

The best and simple way that works with all themes is to use the get_sidebar Wordpress action hook this way:
add_action( 'get_sidebar', 'remove_woocommerce_sidebar', 1, 1 );
function remove_woocommerce_sidebar( $name ){
if ( is_woocommerce() && empty( $name ) )
exit();
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
You might need to make some CSS changes on some html related containers
This code works on any theme as all themes use get_sidebar() Wordpress function for sidebars (even for Woocommerce sidebar) and get_sidebar action hook is located inside this function code.

WooCommerce ads the side bar in code directed at this specific theme, in class WC_Twenty_Seventeen
/**
* Close the Twenty Seventeen wrapper.
*/
public static function output_content_wrapper_end() {
echo '</main>';
echo '</div>';
get_sidebar();
echo '</div>';
}
I used this code to replace that function
remove_action( 'woocommerce_after_main_content', array( 'WC_Twenty_Seventeen', 'output_content_wrapper_end' ), 10 );
add_action( 'woocommerce_after_main_content', 'custom_output_content_wrapper_end', 10 );
/**
* Close the Twenty Seventeen wrapper.
*/
function custom_output_content_wrapper_end() {
echo '</main>';
echo '</div>';
echo '</div>';
}

Use the is_active_sidebar hook - this should work in any theme as it's core WordPress functionality:
function remove_wc_sidebar_always( $array ) {
return false;
}
add_filter( 'is_active_sidebar', 'remove_wc_sidebar_always', 10, 2 );
You can also use conditional statements to only hide the sidebar on certain pages, e.g. on Product pages:
function remove_wc_sidebar_conditional( $array ) {
// Hide sidebar on product pages by returning false
if ( is_product() )
return false;
// Otherwise, return the original array parameter to keep the sidebar
return $array;
}
add_filter( 'is_active_sidebar', 'remove_wc_sidebar_conditional', 10, 2 );

Related

Extend WooCommerce Product Filters plugin shortocodes

I am using Woocommerce with WOOF plugin (woocommerce filter). In particular, this plugin can display a filter that will only search in a specific product category using for example [woof taxonomies=product_cat:23] shortcode and display the results using the [woof_products taxonomies=product_cat:23] shortcode, where 23 is the category id of goods.
However, it is not always possible to specify a category in the shortcode itself, and I would like to implement functionality that allows you to use a shortcode like [woof taxonomies=product_cat:auto], which will automatically determine the current category using a specific function, for example, this (the function is tested and works):
function show_product_category_id() {
$cat = get_queried_object();
$catID = $cat->term_id;
if (empty($catID)) {
//
if (strpos($_GET['really_curr_tax'], 'product_cat')) {
$catID=str_replace('-product_cat', '', $_GET['really_curr_tax']);
}
else {}
}
else {}
echo $catID;
}
I can, of course, create a shortcode for this function, and add it to the theme's functions.php:
add_shortcode( 'show_product_category_id', 'show_product_category_id' );
and it will work. But I can't use a construction like:
[woof taxonomies=product_cat:[show_product_category_id]]
since nested shortcodes in Wordpress won't work. Therefore, apparently, I need to add to woocommerce the ability to specify not only product_cat:35, but also product_cat:auto.
How can i realize it? Or, is there also a way to use nested shortcodes in wordpress?
Of course that you can't nest shortcodes one in another, but what you can do is embed a shortcode in a another shortcode as follows:
function woof_current_product_category_id() {
$term = get_queried_object();
$term_id = 0; // Initializing
if ( isset($_GET['really_curr_tax']) && false !== strpos( $_GET['really_curr_tax'], 'product_cat' ) ) {
$term_id = (int) str_replace('-product_cat', '', $_GET['really_curr_tax']);
} elseif ( is_a($term, 'WP_Term') ) {
$term_id = (int) $term->term_id;
}
return do_shortcode("[woof taxonomies=product_cat:$term_id]"); // Always use return for a shortcode
}
add_shortcode( 'show_product_category_id', 'woof_current_product_category_id' );
Code goes in functions.php file of the active child theme (or active theme).
So the usage will simply be: [show_product_category_id]

Change a Wordpress Hook in Functions.php

I'm attempting to replace a hook name so that my breadcrumb is removed from one location and added to another spot in the page. I'm using a child theme and I'd like to accomplish this within the functions.php of my child theme. What happens is that the function is removed from one location, and added to the new location, but it fires twice, so I have two breadcrumbs stacked on top of each other.
The parent theme function looks like this:
/*-----------------------------------------------------------------------------------*/
/* Breadcrumb display */
/*-----------------------------------------------------------------------------------*/
add_action('woo_main_before','woo_display_breadcrumbs',10);
if (!function_exists( 'woo_display_breadcrumbs')) {
function woo_display_breadcrumbs() {
global $woo_options;
if ( isset( $woo_options['woo_breadcrumbs_show'] ) && $woo_options['woo_breadcrumbs_show'] == 'true' && ! (is_home()) ) {
echo '<section id="breadcrumbs">';
woo_breadcrumbs();
echo '</section><!--/#breadcrumbs -->';
}
} // End woo_display_breadcrumbs()
} // End IF Statement
And this is what I have in my functions.php file of the child theme.
add_action( 'woo_main_before', 'remove_woo_display_breadcrumbs', 0 );
function remove_woo_display_breadcrumbs() {
remove_action('woo_main_before','woo_display_breadcrumbs',10);
}
add_action('woo_content_before','woo_display_breadcrumbs',10);
By the way, this is the Mystile theme by Woo Themes. I'm really new to Hooks, so if you do have the answer, a short explanation of why would be really helpful.
OK, so first, to fix the problem you need to do the following:
remove_action('woo_main_before', 'woo_display_breadcrumbs', 10); // Removes from the original location
add_action('woo_content_before', 'woo_display_breadcrumbs', 10); // Adds to the new location
The '10' is the priority that a function runs for a particular action. Think of it like a slot.
So slot 0 is completely independent from slot 10.
All we're doing in the code sample above is taking woo_display_breadcrumbs out of one slot and inserting it into another.
I hope this helps.

Removing some text in main title on product category archives pages

On my WooCommerce web shop, I would like to remove Archive of : from main title on product category archives pages.
Here is a screenshot:
I have tried to use this code based on this answer,
But it doesn’t work:
function changing_category_archive_page_title( $page_title ) {
if(is_product_category()) {
$title_arr = explode(' :', $page_title);
$new_title = $title_arr[2];
if(!empty($new_title)) echo $new_title;
else echo $page_title;
}
}
add_filter( 'woocommerce_page_title', 'changing_category_archive_page_title', 10, 1 );
How can I do to remove Archive of : from title?
Thanks.
It seem that this 'Archive of : ' text is a customization of your theme, as it doesn't exist in classic WooCommerce. So in this case, it's normal that the code you are using doesn't work.
Without any guaranty, as I can't test it my self, you should try to use the WordPress gettex() function, as I think that this is an addition to main title, as some themes use to do:
add_filter( 'gettext', 'removing_specific_text_in_categories_page_titles', 10, 2 );
function removing_specific_text_in_categories_page_titles( $translated_text, $untranslated_text )
{
if ( 'Archive of :' == $untranslated_text ) {
$translated_text = '';
}
return $translated_text;
}
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Remove Sidebar from Product Category Page | Wordpress Woocommerce

My question is, how can i remove the sidebar only for particular Product category "Slug" and not for its child slugs.
If the url is like below - remove the side bar and make the page full width only for slugs "sanitaryware" and "closets"
http://www.example.com/product-category/sanitaryware/
http://www.example.com/product-category/sanitaryware/closets/
I dont want to remove the sidebar for all "Product Category" reason, i want the side bar to show up the grand-child slug"one-piece-closets":
http://www.example.com/product-category/sanitaryware/closets/one-piece-closets
Code: that i am using in function.php - this is removing the side bar in all the product categories of the website.
<?php
/**
* woocommerce_sidebar hook
*
* #hooked woocommerce_get_sidebar - 10
*/
if (!is_product_category('sanitaryware')){
do_action('storefront_sidebar');
}
?>
Based on your desire to hide the sidebar on top-level categories and their immediate children, we will need a system for determining the hierarchical "depth" of any term archive. Similar to how people often get the "top-level" parent term we can do a while loop of getting a term's term object and checking for the term's parent. In our case instead of returning the top-level parent, we'll just keep a count and return that.
/**
* Returns depth level of product category
*
* #param string $catid Product category ID to be checked
* #return string $depth how many categories deep is this term in the hierarchy
*/
function so_32165017_get_product_cat_depth($catid) {
$depth = 0;
while ($catid) {
$cat = get_term_by('id', $catid, 'product_cat'); // get the object for the catid
if( $cat->parent > 0 ){
$depth++;
}
$catid = $cat->parent; // assign parent ID (if exists) to $catid
// the while loop will continue whilst there is a $catid
}
return $depth;
}
Now that we have something we can use as a condition we can conditionally remove the WooCommerce sidebar:
/**
* Hide the sidebar for items 2 levels deep or more
*/
function so_32165017_conditionally_remove_sidebar(){
if( is_product_category()){
$t_id = get_queried_object()->term_id;
if( so_32165017_get_product_cat_depth( $t_id ) < 2 ){
remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10 );
// could be theme specific ex: Storefront
remove_action( 'storefront_sidebar', 'storefront_get_sidebar', 10 );
}
}
}
add_action( 'woocommerce_before_main_content', 'so_32165017_conditionally_remove_sidebar' );
Edit/Update
If you also want to add a custom body class to make the sidebar-less pages easier to style then I believe you can remove the actions in question from the body_class filter at the same time you are actually filtering the body class.
/**
* Hide the sidebar for items 2 levels deep or more
*/
function so_32165017_conditionally_remove_sidebar( $class ){
if( function_exists('is_product_category') && is_product_category() ){
$t_id = get_queried_object()->term_id;
if( so_32165017_get_product_cat_depth( $t_id ) < 2 ){
remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10 );
// could be theme specific ex: Storefront
remove_action( 'storefront_sidebar', 'storefront_get_sidebar', 10 );
// add a custom body class
$class[] = 'full-width';
}
}
return $class;
}
add_action( 'body_class', 'so_32165017_conditionally_remove_sidebar' );
One approach would be to make a custom template and prevent the sidebar from rendering if a Product Category page is being viewed. Following the upgrade-safe suggestions from the Woocommerce docs you should:
1) Go to the wp-content/plugins/woocommerce/templates/ directory and copy the archive-product.php file
2) Create a woocommerce directory in your theme directory (e.g. wp-content/themes/your-theme/woocommerce)
3) Create a copy of wp-content/plugins/woocommerce/templates/archive-product.php and put place it in wp-content/themes/your-theme/woocommerce/ and name the file archive-product.php
4) Look near the bottom of your new custom file and find:
do_action( 'woocommerce_sidebar' );
5) Replace that line of code with:
if (!is_product_category('sanitaryware') && !is_product_category('Faucet')) {
do_action( 'woocommerce_sidebar' );
}
6) To make the Product Category page content be full-width you could add a class called product-category to the <body> of these pages using a filter.
To add this new class, insert this code in your functions.php file:
// Add specific CSS class by filter
add_filter( 'body_class', 'product_categories' );
function product_categories( $classes ) {
// add 'class-name' to the $classes array
if (is_product_category('sanitaryware') && is_product_category('Faucet')) {
$classes[] = 'product-category';
}
// return the $classes array
return $classes;
}
7) Then in your theme's stylesheet you could target the Product Category pages using this CSS selector:
body.product-category { /* place Product Category page specific CSS here */ }
Since I don't know the specifics of your theme, I can't tell you the HTML elements you'd want to target to set 100% width to, but if the page content was in something like <div id="content"> you could set the CSS like so:
body.product-category #content {
width: 100%;
float: none;
}
You can remove the sidebar from category page override archive-product.php or use action hook in function.php in your current theme in WordPress.
Instead of editing these files directly within the plugin (which is a very bad idea because once update the plugin and all of your changes will be lost!), you can copy them into your theme:
In your theme directory, make a new folder called “WooCommerce.”
Navigate to the WooCommerce plugin directory and open the "templates" folder. The templates folder has a lot of subfolders with all of the different templates that WooCommerce uses. Fortunately, the template file structure and naming in WooCommerce is easy to follow.
In your newly created "WooCommerce" folder, copy the template file that you want to edit.
Remember to keep the directory structure the same here. If the
template you want to edit is within a subfolder then remember to
create that subfolder within your theme's directory.
In archive-product.php -
<?php
/**
* woocommerce_sidebar hook
*
* #hooked woocommerce_get_sidebar - 10
*/
do_action('woocommerce_sidebar');?>
this code display sidebar on category page remove this code and save file .
or
To remove sidebar from category page you want to use action hook in
function.php file -
add_filter( 'body_class', 'remove_sidebar' );
function remove_sidebar()
{
if( is_product_category()) {
remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10);
}
}
Next, we have to edit the style.css in your theme folder-
body.product-category #content
{
width: 100%;
}
Here you can get WooCommerce Action and Filter Hook
-https://docs.woothemes.com/wc-apidocs/hook-docs.html

Use "do_shortcode" in a WordPress plugin is not working

I have this plugin installed on my WordPress:
http://wordpress.org/plugins/put/
I’m trying to make a plugin that uses the UI Tabs plugin inside my own plugin.
My plugin code so far:
function load_jquery(){
echo '<link rel=\'stylesheet\' id=\'jquery-ui-tabs-css\' href=\'http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/themes/smoothness/jquery-ui.css?ver=1.9.2\' type=\'text/css\' media=\'all\' />';
}
add_action('wp_head','load_jquery');
function print_tabs(){
echo do_shortcode('[tab name="Tab"]-[/tab]');
echo do_shortcode('[end_tabset]');
}
add_shortcode('print_tabs', 'print_tabs');
Now if I use the [print_tabs] shortcode in a new page, it should look like this:
http://img835.imageshack.us/img835/4905/workingp.png
But it’s not working, and it looks like this:
http://imageshack.us/a/img62/9772/notworkingm.png
What could be the problem here?
The problem, from what I can see in put.php in the Post UI Tabs plugin is that the shortcodes are only added during the "the_content" filter in a function called "on_the_content".
add_filter( 'the_content', array( $this, 'on_the_content' ), 7 ); // Priority 7 - before wpautop
(line 96 of put.php)
And that function looks like:
public function on_the_content( $content ) {
$this->has_tabs = (bool) apply_filters( 'put_decide_has_tabs', $this->has_tabs );
if( !$this->has_tabs )
return $content;
global $shortcode_tags;
// Backup current registered shortcodes and clear them all out
$orig_shortcode_tags = $shortcode_tags;
$shortcode_tags = array();
add_shortcode( 'tab', array( $this, 'on_tab_shortcode' ) );
add_shortcode( 'end_tabset', array( $this, 'on_tab_end_shortcode' ) );
// Do the shortcode(only the tab shortcode is registered at this point)
$content = do_shortcode( $content );
// Put the original shortcodes back
$shortcode_tags = $orig_shortcode_tags;
return $content;
}
(starting at line 118 of put.php)
So, given how this plugin is written by modifying the content with a filter which in turn adds the shortcodes when that filter is run, what you're seeing is probably happening because when you call "do_shortcode" those shortcodes don't actually exist.
What echoing do_shortcode is doing then, is just coughing up the text.
Unfortunately, because of the way the Post UI Tabs plugin is written, you may not be able to do what you're trying to do.

Categories