Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to create a class that allows me to generate a navigation menu. Here's a simplified version of what I'm using:

class PJ_Menu {
    function make_menu() {
        $args = array(
            'theme_location' => 'location',
            'fallback_cb' => array( $this, 'pj_page_menu' ),
        );
        wp_nav_menu( $args );
    }
    function pj_page_menu() {
        // a bunch of code here
    }

}
new PJ_Menu();

I can't seem to work out what I need in the fallback_cb parameter. array( $this, 'pj_page_menu' ); didn't work, and neither did 'self::pj_page_menu'. Am I missing something obvious, or is it not possible to do this?

share|improve this question
    
Is your class static? –  s_ha_dum Dec 30 '13 at 20:31
    
Sure? (I'm far from expert in object-oriented programming.) At the end of the day I declare new PJ_Menu(); and call it done. –  Pat J Dec 30 '13 at 20:42
add comment

2 Answers 2

up vote 1 down vote accepted

The fallback_cb argument is used when the menu doesn't exist-- that is, when there is no menu of the right name. I won't be used otherwise. Check the Codex. By default the fallback is wp_page_menu.

Your code looks right to me for using a class method as the callback, but I suspect that what you really want is the walker argument, not the fallback_cb one. The walker argument will be used any time that your menu is created, not just as a fallback if something goes wrong.

share|improve this answer
    
The issue seems to be that fallback_cb expects a string and I'm trying to hand it an array. But you're probably right -- I'm likely abusing the fallback_cb concept entirely. –  Pat J Dec 30 '13 at 20:55
    
That is the correct way to use an object method though I have never tried exactly what you are trying. Do you get any errors? –  s_ha_dum Dec 30 '13 at 20:56
    
No errors, but when I do var_dump( $args ); the fallback_cb parameter is empty. –  Pat J Dec 30 '13 at 20:58
add comment

wp_nav_menu calls PHP's call_user_func on fallback_cb. To pass in a class method as that argument, the method needs to be static and you need to pass in the class and method name.

static function pj_page_menu() {
    // a bunch of code here
}

From PHP's is_callable documentation, fallback_cb should be in the form:

'fallback_cb' => array('PJ_Menu', 'pj_page_menu')

or for PHP >= 5.2.3

`fallback_cb` => 'PJ_Menu::pj_page_menu'
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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