1

I asked this earlier on StackOverflow.com before realizing there was a StackExchange specifically for WordPress. Here is the link to the old question (for moderator deletion):

https://stackoverflow.com/questions/6862887/replacing-wordpress-core-functionality-with-a-plugin

I'm creating a WordPress plugin for a custom menu layout. I am well aware that I could just as easily implement this menu directly into the theme and I've read up quite thoroughly on the features and limitations of wp_nav_menu(), plus I have already tried and tested every plugin already created for replacing the default WordPress menu.

I wish to use a plugin since my client will be implementing this on several different WordPress sites, many of which run on different themes - and most of those are themes which I did not create and I do not wish to re-write their code in case they update the theme in the future.

When I've looked into a way to implement the menu into the theme I found that there are only two good methods since there is no hook or filter called at menu display time. The first is to change the theme to look for the plugin (this is similar to the method used by PixoPoint and many other menu plugins):

header.php:

if(function_exists('pixopoint_menu')){
    pixopoint_menu();
} else {
    wp_nav_menu();
}

The second method is a walker class:

plugin.php:

class my_walker_class Extends Walker_Nav_Menu {
    function start_el(&$output, $item, $depth, $args) {
        /*
        *  Etc. Etc.
        */
    }
}

header.php:

wp_nav_menu( Array( 'walker' => 'my_walker_class' ) );

However as you'll note both of these methods require a modification to the standard header.php file.

Ideally I would like to simply replace the wp_nav_menu() function if my plugin is loaded, as this would give my plugin support for the majority of themes without having to edit any of the theme files. Is there a good way to do this? Or is there a better way to write a menu plugin which I am not seeing?

1 Answer 1

2

Err... There's plenty of filters on the menu system.

The 'wp_nav_menu' filter is called on the output of the wp_nav_menu() function call. So if you need to modify the menu output, you can use that.

The 'wp_nav_menu_items' filter is called on the resulting array of items (basically all the LI items).

If you need to modify the arguments passed into wp_nav_menu, such as to dynamically add your own walker, you can use the 'wp_nav_menu_args' filter for that.

There's a lot more filters which are more specific and can probably be used to change things in more subtle ways. These are sorta the main club-like filters that change the whole thing.

5
  • I saw the wp_nav_menu filter in some (but not all) lists of WordPress filters when I was trying to solve this myself, however I tried the following code: add_filter('wp_nav_menu', 'my_menu_func'); function my_menu_func(){print "Testing 123";} and it never displayed Testing 123. Am I doing something wrong? I don't work with WordPress much
    – stevendesu
    Commented Jul 28, 2011 at 21:34
  • With such code, the menu should have disappeared and the testing 123 would have appeared in the place of the menu. Are you using super-cache or similar plugins that would have hid the resulting changes from you?
    – Otto
    Commented Jul 28, 2011 at 21:36
  • The only plugin on the entire site is my custom plugin.
    – stevendesu
    Commented Jul 28, 2011 at 21:37
  • 1
    I just tried it on a test site. It works as expected. Note that you actually have to HAVE a menu for it to ever make it this far. If no nav menu is defined, then it calls the fallback function instead which defaults to wp_page_menu(). This can be defined with the fallback_cb parameter.
    – Otto
    Commented Jul 28, 2011 at 21:42
  • Ah. I didn't have a menu defined. That actually resolved the entire issue. I can't believe I spent the entire morning looking up walker classes and how to override WordPress functions when I had it right from the start. Thank you.
    – stevendesu
    Commented Jul 28, 2011 at 21:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.