Title: [Plugin: WP-dTree] Open to selection&#8230;
Last modified: August 20, 2016

---

# [Plugin: WP-dTree] Open to selection…

 *  [Krektor](https://wordpress.org/support/users/ffdltd/)
 * (@ffdltd)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/plugin-wp-dtree-open-to-selection/)
 * Hello,
 * Great plug-in, thank you very much for making it!
 * I have everything working how I want, with a basic main and sub categories setup.
 * However I would like the tree to automatically open itself depending on what 
   post is loaded. So for example if a user views a post in Category A, then the
   Category A main tree opens to display all its sub categoies. Same if a user lands
   on a post in Category B, etc.
 * I believe this is suppose to happen by enabling the ‘Open to selection’ feature
   within the widget option???
 * If so, it doesn’t seem to work on mine for some reason. It will always have open
   the last tree that was open, or just none if its the users first visit to the
   site.
 * I’m almost sure this worked when I first installed it, but I have tried installing
   the plug-in again from scratch with no change (unless settings are stored even
   when the plug-in is deleted?).
 * Any ideas what I should be looking at?
 * Many thanks!
 * [http://wordpress.org/extend/plugins/wp-dtree-30/](http://wordpress.org/extend/plugins/wp-dtree-30/)

Viewing 15 replies - 1 through 15 (of 23 total)

1 [2](https://wordpress.org/support/topic/plugin-wp-dtree-open-to-selection/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/plugin-wp-dtree-open-to-selection/page/2/?output_format=md)

 *  Plugin Author [ulfben](https://wordpress.org/support/users/ulfben/)
 * (@ulfben)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/plugin-wp-dtree-open-to-selection/#post-2526097)
 * Link?
 *  Thread Starter [Krektor](https://wordpress.org/support/users/ffdltd/)
 * (@ffdltd)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/plugin-wp-dtree-open-to-selection/#post-2526106)
 * Hi ulfben, thanks for the reply. It is a local site at the mo for testing. However
   I have tried on another site, and the open to selection DOES work when viewing
   categories, but NOT when viewing the actual post itself.
 * I have replicated this on my test site also so I assume this is how it is supposed
   to work?
 * Many thanks 🙂
 *  Plugin Author [ulfben](https://wordpress.org/support/users/ulfben/)
 * (@ulfben)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/plugin-wp-dtree-open-to-selection/#post-2526225)
 * No that’s not the way it’s supposed to work. Open To Selection = opens to (and
   optionally highlights) the current post, category or archive, respectively.
 * I need access to the site to help. But you can test if there’s a difference when
   using permalinks or not. Make sure you’re not using “force open to”. Try running
   two types of trees; a category and an archive, see if their behaviour differs.
 *  Thread Starter [Krektor](https://wordpress.org/support/users/ffdltd/)
 * (@ffdltd)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/plugin-wp-dtree-open-to-selection/#post-2526226)
 * Ahhh I see!
 * OK, I think the problem is that I have it configured to **not** display posts,
   only categories.
 * I have tried turning ‘list posts’ on and it does open the correct tree to display
   the current post name.
 * Ideally I would like it to open and highlight the current category that the post
   is in, rather than the post itself in the tree. Currently it keeps open whatever
   tree was open before, or if its the first visit, then all trees are closed.
 * Is this option possible at all?
 * If so then I could upload the site to a test domain for you to have a look at?
   🙂
 * Thanks again, your help is appreciated!
 *  Plugin Author [ulfben](https://wordpress.org/support/users/ulfben/)
 * (@ulfben)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/plugin-wp-dtree-open-to-selection/#post-2526292)
 * You have discovered an edge case. I probably never tested without listing posts.
 * I’ve added this this to my list. I’ll let you know when a new release is out.
 *  Plugin Author [ulfben](https://wordpress.org/support/users/ulfben/)
 * (@ulfben)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/plugin-wp-dtree-open-to-selection/#post-2526326)
 * Please [try the development version here](http://wordpress.org/extend/plugins/wp-dtree-30/download/).
   I’ve added a fix for category trees without posts.
 *  [KaFu](https://wordpress.org/support/users/kafu/)
 * (@kafu)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/plugin-wp-dtree-open-to-selection/#post-2526331)
 * Hi Ulf,
 * I’ve found a related issue (still present in the dev version). In the categories
   widget if I open a page (which is not allocated to any category), not a post,
   the selection is opened up to a random category. I would have expected the tree
   to be collapsed instead.
 * [http://funk.eu](http://funk.eu)
    Click on the “Contact” and “Imprint” links 
   at the top, those are pages.
 * Great work, keep it up!
 * Thanks and Best Regards
    Karsten
 *  Plugin Author [ulfben](https://wordpress.org/support/users/ulfben/)
 * (@ulfben)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/plugin-wp-dtree-open-to-selection/#post-2526332)
 * Interresting. We have an unintended ID-collision; your page is id 11 – correctly
   identified by the plugin. But the dtree JavasScript erroneously matches this 
   with category ID ‘-110’.
 * And I thought the negated category IDs was such a clever little hack. 😀
 * Could you edit `wp-dtree.php` so I can run a debugger on the script?
 * Just change
    `define('WPDT_SCRIPT', 'wp-dtree.min.js');` to `define('WPDT_SCRIPT','
   wp-dtree.js');`
 * and post here again.
 *  Plugin Author [ulfben](https://wordpress.org/support/users/ulfben/)
 * (@ulfben)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/plugin-wp-dtree-open-to-selection/#post-2526333)
 * Actually, no. It’s not an ID clobbering. The script simply assumes the ID passed
   to “openTo” is valid. If it doesn’t find it in the tree it will open to the index.
   Your category “Coding” (ID = -110) is actually the 11nth node in the tree. Similarly;
   your “Imprint” (page id = 78) opens the three to index 78 (which happens to be
   a post on Alexa rankings).
 * I need some time to set up and test a fix, but perhaps you can try it yourself?
 * The offending function is `wpdTree.prototype.openTo` in `wp-dtree.js`. I believe
   all we need to do is remove the conditional from the lookup loop to always run
   it, and abort if there’s no matching node.
 * However; I’m not sure what side effects this might cause. Mr. Landrö probably
   had a reason to put the conditional there in the first place.
 *  [KaFu](https://wordpress.org/support/users/kafu/)
 * (@kafu)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/plugin-wp-dtree-open-to-selection/#post-2526334)
 * Hmm, tinkered with it without success, but my JS knowledge is just fundamental
   to say the least :). How about adding all valid IDs to a string during creation
   and check for the IDs existence (with something like instr?) as the first check
   in the function?
 *  Plugin Author [ulfben](https://wordpress.org/support/users/ulfben/)
 * (@ulfben)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/plugin-wp-dtree-open-to-selection/#post-2526337)
 * Checking for the ID is not a problem; that check is being done. But even if the
   ID isn’t found the function progresses, treating the ID as an index. Our problem
   now is to find why it was written this way, or rather what side-effects it might
   cause to change the behaviour.
 * I like your look-up solution though. Remember doing that often in ActionScript
   2; the String-functions were way faster than to iterate-and-compare through an
   Array.
 * All you need to do to test this for me is change this:
 *     ```
       wpdTree.prototype.openTo = function(nId, bSelect, bFirst){
       	if(!bFirst){
       		for (var n=0; n<this.adtNodes.length; n++){
       			if(this.adtNodes[n].id == nId){
       				nId=n;
       				break;
       			}
       		}
       	}
       	var cn=this.adtNodes[nId];
       	if(cn.pid==this.root.id || !cn._p) return;
       	cn._io = true;
       	cn._is = bSelect;
       	if(this.completed && cn._hc) this.nodeStatus(true, cn._ai, cn._ls);
       	if(this.completed && bSelect) this.s(cn._ai);
       	else if(bSelect) this._sn=cn._ai;
       	this.openTo(cn._p._ai, false, true);
       };
       ```
   
 * ([http://pastebin.com/Wy81h3Mm](http://pastebin.com/Wy81h3Mm))
 * To this:
 *     ```
       wpdTree.prototype.openTo = function(nId, bSelect, bFirst){
       	for (var n=0; n<this.adtNodes.length; n++){
       		if(this.adtNodes[n].id == nId){
       			nId=n;
       			var cn=this.adtNodes[nId];
       			if(cn.pid==this.root.id || !cn._p) return;
       			cn._io = true;
       			cn._is = bSelect;
       			if(this.completed && cn._hc) this.nodeStatus(true, cn._ai, cn._ls);
       			if(this.completed && bSelect) this.s(cn._ai);
       			else if(bSelect) this._sn=cn._ai;
       			this.openTo(cn._p._ai, false, true);
       		}
       	}
       };
       ```
   
 * ([http://pastebin.com/0Fn8uNv0](http://pastebin.com/0Fn8uNv0))
 *  [KaFu](https://wordpress.org/support/users/kafu/)
 * (@kafu)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/plugin-wp-dtree-open-to-selection/#post-2526338)
 * wp-dtree.js does not seem to be used but wp-dtree.min.js
 * I changed this
 * `wpdTree.prototype.openTo=function(b,a,c){if(!c){for(var e=0;e<this.adtNodes.
   length;e++){if(this.adtNodes[e].id==b){b=e;break}}}var d=this.adtNodes[b];if(
   d.pid==this.root.id||!d._p){return}d._io=true;d._is=a;if(this.completed&&d._hc){
   this.nodeStatus(true,d._ai,d._ls)}if(this.completed&&a){this.s(d._ai)}else{if(
   a){this._sn=d._ai}}this.openTo(d._p._ai,false,true)};`
 * to this
 * `wpdTree.prototype.openTo=function(b,a,c){for(var e=0;e<this.adtNodes.length;
   e++){if(this.adtNodes[e].id==b){b=e;var d=this.adtNodes[b];if(d.pid==this.root.
   id||!d._p){return}d._io=true;d._is=a;if(this.completed&&d._hc){this.nodeStatus(
   true,d._ai,d._ls)}if(this.completed&&a){this.s(d._ai)}else{if(a){this._sn=d._ai}}
   this.openTo(d._p._ai,false,true)}}};`
 * but that seems to break the opento function and nothing is opened at all.
 *  Plugin Author [ulfben](https://wordpress.org/support/users/ulfben/)
 * (@ulfben)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/plugin-wp-dtree-open-to-selection/#post-2526340)
 * > wp-dtree.js does not seem to be used but wp-dtree.min.js […]
 * … so either [read what I’ve said](http://wordpress.org/support/topic/plugin-wp-dtree-open-to-selection?replies=13#post-2604688),
   or – you know – copy the content of one into the other? Whichever makes you happy.
 *  [KaFu](https://wordpress.org/support/users/kafu/)
 * (@kafu)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/plugin-wp-dtree-open-to-selection/#post-2526349)
 * The code above should be the minified version of what you’ve posted. Nevertheless
   I copied your original code to wp-dtree.js and replaced the wp-dtree.min.js on
   my server with this new version… exactly the same result as for my minified version
   above, opento does not seem to work at all.
 *  Plugin Author [ulfben](https://wordpress.org/support/users/ulfben/)
 * (@ulfben)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/plugin-wp-dtree-open-to-selection/#post-2526350)
 * _Check._ I need to set up a test environment and sort this out. You’ll be notified
   when a new version is out.

Viewing 15 replies - 1 through 15 (of 23 total)

1 [2](https://wordpress.org/support/topic/plugin-wp-dtree-open-to-selection/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/plugin-wp-dtree-open-to-selection/page/2/?output_format=md)

The topic ‘[Plugin: WP-dTree] Open to selection…’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/wp-dtree-30_fcfcf8.svg)
 * [WP-dTree](https://wordpress.org/plugins/wp-dtree-30/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wp-dtree-30/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wp-dtree-30/)
 * [Active Topics](https://wordpress.org/support/plugin/wp-dtree-30/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wp-dtree-30/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wp-dtree-30/reviews/)

## Tags

 * [WP-dTree 3.0](https://wordpress.org/support/topic-tag/wp-dtree-3-0/)

 * 23 replies
 * 4 participants
 * Last reply from: [engelle](https://wordpress.org/support/users/engelle/)
 * Last activity: [14 years, 3 months ago](https://wordpress.org/support/topic/plugin-wp-dtree-open-to-selection/page/2/#post-2526435)
 * Status: not resolved