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.

If have hooked into admin-menu like this:

function adminmenu_users_reserves(){    
    add_users_page('Reserver', 'Reserver', 'read', 'reserve_list', 'show_userlist');
}
add_action( 'admin_menu', 'adminmenu_users_reserves' );

and have a function

function show_userlist(show_reserves = true, $title) {
   code...
}

Can I pass variables $show_reserves FROM add_users_page() somehow? (In one case I need $show_reserves to be true, in other case I need it to be false)

Or would I have to solve my issue with two different functions like this? (for almost the same functionality)

function adminmenu_users_reserves(){    
    add_users_page('Reserves', 'Reserves', 'read', 'reserve_list', 'show_reservelist');
}
add_action( 'admin_menu', 'adminmenu_users_reserves' );

function adminmenu_users_registered(){    
    add_users_page('Registered', 'Registered', 'read', 'registered_list', 'show_registeredlist');
}
add_action( 'admin_menu', 'adminmenu_users_registered' );

UPDATE Because I've got such a great answer from toscho (http://wordpress.stackexchange.com/users/73/toscho) I want to share my solution - if someone else has the same issue in the future:

class userList
{
    public $show_reserves = false;
    public $title = 'Reserver';

    //Show a list of all reserves/or registered users in the system ordered by register-date
    function render() {

        $reserves = $this->show_reserves;
        $title = $this->title;        

        echo $title;
        if ($reserves === true) {
            ....code...
        }
     }
}

function adminmenu_users_reserves(){    
    $userlist = new userList();
    add_users_page('Reserver', 'Reserver', 'read', 'reserve_list', array( $userlist, 'render' ));
}
add_action( 'admin_menu', 'adminmenu_users_reserves' );

function adminmenu_users_registered(){    
    $userlist = new userList();
    $userlist->show_reserves = false;
    $userlist->title = 'Anmälda';
    add_users_page('Anmälda', 'Anmälda', 'read', 'registered_list', array( $userlist, 'render' ));
}
add_action( 'admin_menu', 'adminmenu_users_registered' );
share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

The easiest and most clean way to solve that problem is using a specialised object.

First create a class that can hold extra information:

class Menu_Page
{
    public $extra = '';

    public function render()
    {
        print $this->extra;
    }
}

Now create an object from that class …

$page        = new Menu_Page;
$page->extra = 'Hello World!';

… and register its method render() as callback:

add_users_page( 
    'Test', 
    'Test', 
    'manage_options', 
    'test', 
    array( $page, 'render' ) 
);
share|improve this answer
    
Somewhat "complicated" code just for this purpose, but thank you for the elegant solution! :-) –  bestprogrammerintheworld Dec 10 '13 at 8:59
    
@bestprogrammerintheworld You can build the class in such a way that you can reuse it in the same project or in multiple projects. After a while you will find the old way too complicated. :) –  toscho Dec 10 '13 at 9:01
    
I agree that oop is the best way to do programming, but I just meant that such a simple task should have been easier to implement :-) –  bestprogrammerintheworld Dec 10 '13 at 9:05
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.