Solved

Modify a wordpress menu plugin to support WP 3.0

Posted on 2010-09-23
15
557 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How do I edit this Drupal page? 9 30
resizeing PHP image 2 24
html input clean up 3 32
Help cleaning out CSS 2 32
Since I am currently running into this issue right now as I type this, I though I would share my experiences with moving a ModX Revolution site from one server to another.  It is not an easy task, but can be accomplished rather easily. The first …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…
The viewer will learn how to count occurrences of each item in an array.

746 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

9 Experts available now in Live!

Get 1:1 Help Now