Hey Experts,
I've got a bug in my theme that's got me stumped. My lead developer is unavailable and I'm much more of a "designer that codes" so It's very likely that the solution is staring me right in the face. This is where you come in :)
My theme uses a hard-coded version of the "Sidebar Generator" plugin which was developed in 2009 and no longer available on the repository. The plugin essentially adds a new admin page located under 'Appearance > Sidebars' and enables a user to create a new sidebar on the fly.
Here's the default code of that script: http://swte.ch/tmp/sidebar_generator
----------------
Here's my bug:
----------------
I slightly modified the script to check if woocommerce is installed. If woocommerce is installed, then the "sidebar ID" changes. My customization is broken. Here's a sample of the breakage:
- User creates a new sidebar under 'Appearance > Sidebars'
- They add a Widget to their new Sidebar under 'Appearance > Widgets' (lets say text widget)
- User then activates or de-activates WooCommerce and their Widget data (their text widget) gets lost completely and/or moved into a different Sidebar
- If WooCommerce is then re-activated the Widget appears back in it's proper place.
It seems I'm missing a step which ensures that the "Widget / Widget Data" always gets moved around correctly when WooCommcerce gets activated / deactivated.
------------------------------------------------
Customization info / Download files
------------------------------------------------
- Download theme files here: https://dl.dropboxusercontent.com/u/7008416/karma.zip
- my customization with the woocommerce check happens between lines 74 and 97 of "multiple_sidebars.php"
- "multiple_sidebars.php" is located in: ../themes/karma/framework/extended/multiple_sidebars.php
-----------------------------------
A huge thank you and happy holidays!
-----------------------------------
Bob answers:
I am confused about why you want to change id there?
Can you please let me know final result or output you want to achieve?
WP Answers comments:
To be honest I think it was a mistake changing the ID of the sidebar and should have never been done in the first place, not sure why my dev made this happen.
The code is there though, so now we need to ensure a seamless upgrade so that existing users don't lose their Widgets.
The desired end-result is that Widgets always stay in their correct sidebars, whether or not WooCommerce is activated or not.
------------------------------------
Here are some more notes:
------------------------------------
- On line 326 of functions.php the theme defines 13 sidebars
- On line 457 of functions.php the theme checks for WooCommerce, and registers 2 more sidebars if WooCommerce is active.
- "multiple_sidebars.php" was recently pointed out to have some security flaws so we upgraded to the more secure version.
- The old version can be seen here: http://snippi.com/s/18m6t0x
- And the new version (which is now included in the theme) can be found in the theme as per my original post
- Sidebars created with the "old version" must play nicely with the "new version" and again the desired end-result is that Widgets always remain in their correct location.
The culprit seems to be related to the sidebar ID
Thank you :)
Bob comments:
What is happening here is while we are de-activating woocommerce two sidebars are removed and the other sidebars created by multiple sidebar plugin are added after it.
so previously widgets which are attached to sidebar 14 is not visible on new sidebar(created by multiple sidebar plugin) only if the widget is not from woocommerce itself.
I think almost all widgets are set when the woo was active so simplest idea is to don't do conditional check for woo!
let the woocommerce widget area be there, if we don't need it we can avoid using it. just don't touch it.
so in functions.php remove woocommerce conditional check for widget.
In multiple sidebar plugin remove/comment all conditional check and $tt_i=15; instead of it. so next sidebar created by plugin start with 16 and widget assigned to 16 stays there.
/* Instead of removing condition check I commented it out
if (class_exists('woocommerce')){
$tt_i = 15;
}
else {
$tt_i = 13;
}//end woo
*/
$tt_i = 15;
if(is_array($sidebars)){
WP Answers comments:
That did it, thank you so much my friend!
Arnav Joy answers:
when you activate woocommerce and create a sidebar then it assigns the text box or any widget to that sidebar. but when you deactivate it the sidebar gets lost as id gets changed as per your code .
so what needs to be happen ?
WP Answers comments:
Hi,
Please see my response to Bob above for more notes on this issue.
The desired end-result is that Widgets always remain in their correct sidebar, whether or not WooCommerce is activated or de-activated. Users should be able to upgrade their theme without losing any of their Widgets.
Thank you :)