Solved

Modify a wordpress menu plugin to support WP 3.0

Posted on 2010-09-23
15
561 Views
Last Modified: 2012-05-10
I have found a plugin (nrs_folding_tree) that works for what I need in wordpress better than any other plugin I've found thus far.

The plugin creates a "folding tree" menu of your pages in the sidebar.

Unfortunately, the plugin does not support Wordpress 3.0's Custom Menu function, which is the last piece I need to make it work.

Details about the cutom menu are here:
http://templatic.com/news/wordpress-3-0-menu-management

you can view a working copy of my wordpress install here:
http://www.disabilityhousingnetwork.org.php5-13.dfw1-2.websitetestlink.com/

I have tried unsuccessfully to modify this plugin myself. Basically I am looking to make the list generate from the wordpress 3 custom menu.
<?php

/*

Plugin Name: Folding Pages Widget

Plugin URI: http://navyroad.com/wordpress-plugins/nrs-folding-pages-widget/

Description: Provides a hierarchical list of pages for your sidebar.

Author: Chris Carson

Version: 1.0

Author URI: http://navyroad.com

Credits: Thanks to Andy Skelton.  Based on code by Erwin Harte (http://is-here.com/projects/wordpress/pages) Calvin Yu (http://blog.codeeg.com/)

*/



function nrs_folding_pages($args){

    extract($args);

    //var_dump($args);

    $sort_opts = nrs_folding_pages_get_sort_opts();

    $options = get_option("nrs_folding_pages");

    $title = (empty($options["title"])) ? null : $options["title"];

	$sort_column = (array_key_exists($options["sort_column"],$sort_opts)) ? $options["sort_column"] : key($sort_opts);

	

    global $notfound;

	global $post;

	

	//returns a numerically indexed array...

	$pages = get_pages("sort_column=$sort_column");

	//a new associative array indexed by ID... 

	$nodes = array();

	foreach ($pages as $page) $nodes[$page->ID] = $page;

	//an array to hold the parent ids in the current tree...

	$parent_ids = array();

	if (is_page() and ($notfound != '1')) 

	{

		$curr_page_id = $post->ID;

		while ($curr_page_id)

		{

			array_unshift($parent_ids, $curr_page_id );

			$curr_page_id = $nodes[$curr_page_id]->post_parent;

		}

	}

	//add the top-level parent id, which is 0...

	array_unshift($parent_ids, 0 );

	

	//output...

	echo $before_widget;

	echo $after_title;

	nrs_display_tree($parent_ids, $nodes, 0);

	echo $after_widget;

}

function nrs_display_tree($parent_ids, $nodes, $curr_id)

{

	global $post;

	if ($curr_id)

	{

		$node = $nodes[$curr_id];

		$title = wp_specialchars($node->post_title);

		$link = get_page_link($curr_id);

		$class = "page_item";

		if ($curr_id == $post->ID) $class .= " current_page_item";

		echo "<li class=\"$class\"><a href=\"$link\" title=\"$title\">$node->post_title</a>";

	}

	if (in_array($curr_id, $parent_ids)) 

	{

		$num_children = 0;

		foreach ($nodes as $node)

		{

			if ($node->post_parent == $curr_id) 

			{

				if (! $num_children) echo "\n<ul>";

				nrs_display_tree($parent_ids, $nodes, $node->ID);

				$num_children++;

			}

		}

		if ($num_children) echo "</ul>\n";

		

	}

	if ($curr_id) echo "</li>\n";

	

}





function nrs_folding_pages_get_sort_opts() {

	return array( "post_title"=>"Title",

					"menu_order"=>"Page Order");

}

function nrs_folding_pages_control() {

	$sort_opts = nrs_folding_pages_get_sort_opts();

    $options = get_option("nrs_folding_pages");

   

    if ( $_POST['nrs_folding_pages_submit'] ) 

    {

		$options['title'] = strip_tags(stripslashes($_POST['nrs_folding_pages_title']));

		$options['sort_column'] = (array_key_exists($_POST['nrs_folding_pages_sort_column'], $sort_opts)) ? $_POST['nrs_folding_pages_sort_column'] : key($sort_opts);

		update_option('nrs_folding_pages', $options);

    }

    $sort_column = (array_key_exists($options["sort_column"],$sort_opts)) ? $options["sort_column"] : key($sort_opts);

    $title = wp_specialchars($options['title']);



?>

	<input type="hidden" id="nrs_folding_pages_submit" name="nrs_folding_pages_submit" value="1" />

	<p><label for="nrs_folding_pages_title"><?php _e('Title:'); ?> <input style="width: 250px;" id="nrs_folding_pages_title" name="nrs_folding_pages_title" type="text" value="<?php echo $title; ?>" /></label></p>

	<p><label for="nrs_folding_pages_sort_column">

		<?php _e('Sort:'); ?> 

		<select  id="nrs_folding_pages_sort_column" name="nrs_folding_pages_sort_column">

		<?php

		foreach ($sort_opts as $val=>$label){

			echo "<option value=\"$val\"";

			if ($sort_column == $val) echo " selected";

			echo ">$label</option>";

		}

		?>

		</select>

	</label></p>

	

<?php

}

function nrs_folding_pages_init()

{



    register_sidebar_widget("NRS Folding Pages", "nrs_folding_pages");

    register_widget_control("NRS Folding Pages", "nrs_folding_pages_control");

}



add_action("plugins_loaded", "nrs_folding_pages_init");



?>

Open in new window

0
Comment
Question by:Eroots
  • 6
  • 5
  • 2
15 Comments
 
LVL 23

Expert Comment

by:jeremyjared74
ID: 33746046
I don't know how comfortable you are with hacking your theme files, but here is a tutorial on a cool accordion menu for Wordpress sidebars:
http://net.tutsplus.com/tutorials/wordpress/wordpress-sidebar-turned-apple-flashy-using-jquery-ui/
0
 

Author Comment

by:Eroots
ID: 33746356
The above hack does not work for me, the action and styling are not compatible with my design goals.

I would really like to see the potential of the plugin updated for WP3, it's a little beyond my ability thus I posted here for help.

I am not interested in alternatives, unless they have the exact same action as the demo posted above.
0
 
LVL 14

Expert Comment

by:Dzynit
ID: 33748372
Did you already try the wp 3.0 reference from the site you referenced above?
Adding:

if (function_exists('add_theme_support')) {    add_theme_support('menus');}

to your theme's functions.php file?

Have to ask that because with that addition it works properly with 3.0, so there must be another issue going on if you've already done that.
0
 
LVL 14

Expert Comment

by:Dzynit
ID: 33748407
But to offer a little more, this plugin is identical:
http://wordpress.org/extend/plugins/wp-dtree-30/

All action is the same. It has a settings admin page so you can change the link color and bold them to match your current settings. (default is white with no bolding)
0
 

Author Comment

by:Eroots
ID: 33748480
I added this line:
if (function_exists('add_theme_support')) {    add_theme_support('menus');}
to my functions.php file (was not there), but that still does not make the plugin sort according to my menu. Basically, if I turn the nrs_folding_pages plugin off, the menu is unstylized but sorted correctly in the default custom menu order. The plugin is breaking the sort order.

Also, I was unable to use WP-dTree because it truncates longer link names, if you know a way to make WP-DTree show the full link name (I played with it for hours, did not get it to work), then I would be happy to re-visit that.
0
 
LVL 14

Expert Comment

by:Dzynit
ID: 33748601
Ahh - I think I might have found your problem:

In WP3.0, you have to goto: Appearance > Menus, then create a menu with the pages you want and in the order you want. Then after that is done, you choose that as you're main navigation.

(It's like the page order plugin that you use to have to install separately, but is now built right into wordpress 3.0+.)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Eroots
ID: 33748717
ok.....  still not getting it.

Here's what I have done to create the menu in my sidebar:

1) Created custom menu under "appearance > menus"
2) Installed the nrs_folding_pages plugin, activated, customized.
3) Activated the widget in the sidebar for NRS folding pages.

I see what you probably mean, the "custom menu" widget is NOT part of the sidebar active widgets because the nrs_folding_pages plugin is a separate widget from the custom menu.

So can you suggest a hack for WP-DTree that will NOT truncate the link names? Or can you tell me how my plugin above can be hacked to use the ordering in the custom menu.
0
 
LVL 14

Expert Comment

by:Dzynit
ID: 33748802
To set the characters for wp-dtree, go to Settings > WP Dtree and you should see the following screenshot toward the top.

I just always put a really big number in where you see the 30's.
0
 
LVL 14

Expert Comment

by:Dzynit
ID: 33748807
Sorry, the image didn't attach.

screenshot201.jpg
0
 

Author Comment

by:Eroots
ID: 33757917
One last thing, I've updated the settings, and it displays the characters, however there are still two issues.

#1 - apostrophes are shown as \' - I have a page called "Steve's List" that's displayed as "Steve\'s List"

#2 - the right side of the text is cut-off, IE - the text does not fall over to a second line of the link, it just keeps going off past the cutoff of the Div's width.

Help? almost there!
0
 
LVL 23

Expert Comment

by:jeremyjared74
ID: 33758331
Replace the ' in Steve's to: Stev&#39;s and it will work.
0
 

Author Comment

by:Eroots
ID: 33770044
sorry, that did NOT work, it displays as "Steve&#39;s List" now. I tried &apos; as well, but it does not render it either.

Any ideas anyone on why html-dtree is cutting off the characters instead of falling to a second line?
0
 

Accepted Solution

by:
Eroots earned 0 total points
ID: 33823952
After working with a few colleagues and a lot of trial and error, I've beat EE to the punch and come up with my own solution.

My solution involves modifying my original posted plugin - nrs_folding_pages, and modifying the sort order from an array sort by ID, to using the page order you can set in wordpress.

The caveat of this solution is that it uses page order instead of a custom menu, but since the "my page order" plugin exists, then it's easy to almost duplicate the functionality.

I have attached code for the modified part of the original wordpress plugin. Again, I am disappointed that an expert recommended a solution that was inherently broken (cutting off long titles) and did not followup to offer a better or even feasible solution.
//returns a numerically indexed array...

	//The line below was changed from sort_column=$sort_column to sort_column=menu_order&title_li=

	$pages = get_pages("sort_column=menu_order&title_li=");

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
The purpose of this video is to demonstrate how to exclude a particular blog category from the main blog page. This is can be used when a category already has its own tab, or you simply want certain types of posts not to show up on the main blog. …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now