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.

In my functions.php I have the following code:

add_action( 'admin_menu', 'jp_create_admin_pages' );

function jp_create_admin_pages()
{
    add_menu_page(
        'Members',
        'Members',
        'manage_options',
        'members',
        'admin-members.php');
}

In the same folder, I have another file called admin-members.php with the following code:

<span>Hello Admin World!</span>

However, I am getting a PHP error saying that this is an invalid callback (it works when I replace the 'admin-members.php' parameter with the name of a function in functions.php).

How can I reference an external file as a callback so that this will work?

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

You can’t. Create a function that loads that file:

function load_admin_page_file()
{
    require 'admin-members.php';
}

Then use that function name as callback argument.

In PHP 5.3 you can use a lambda:

add_menu_page(
    'Members',
    'Members',
    'manage_options',
    'members',
    function() { require 'admin-members.php'; }
);
share|improve this answer
1  
Could you please wait until I finished my answer before posting? Thanks ;) –  kaiser May 3 '13 at 13:17
2  
@kaiser Nope. I knew you would write a novel where a simple snippet can solve the problem. So I wrote that snippet. –  toscho May 3 '13 at 13:19
add comment

Programming and machines

As machines are "stupid", they need to be programmed. And programming languages don't work like languages spoken by humans as programs/machines as they can't interpret what the human thinks. They need a clear and unique advice what and when to something we order them. If you for e.g. define the function getBlock() twice, the machine would throw and Exception to handle that failure and notify the human about his error. The machine will not even question if it should return you a building block or a solid square sided something. Therefore this isn't possible (in this case) and you have to stick to the rule that the program needs a clear advice that gives it some argument/parameter that is within the boundaries of what it expects.

The needed argument within the boundaries

As the program told you, it needs a callback, which is either a function or a method of array( 'ObjectName', 'methodName' ). So you have to feed it with one or the other.

A work around

As you can do nearly everything within a function or method, you can as well advice it to load your file. Depending on the way you load it, you can either just require or include that file or redirect to it:

function wpse98197_redirect_admin_page()
{
    # You can simply load them...
    # (Both are equal in what they do, but not in how they handle errors)
    // require_once 'admin-members.php';
    // include_once 'admin-members.php';

    # Or replace the current page with the desired file
    exit( wp_redirect( get_stylesheet_uri().'admin-members.php' ) );
}
share|improve this answer
    
Nice answer - one followup - is there such thing as lambda statements in PHP? With C# if a method takes a function as a parameter (i.e. a delegate), you could do this: add_action('admin_menu', () => { require 'admin-members.php'; }. (or something like that - I can't remember the syntax off of the top of my head). –  William May 3 '13 at 14:07
1  
@William There are anonymous functions in PHP - php.net for the rescue. But I wouldn't recommend using them on hooks or filters. They are extremely hard to remove and impossible to debug. Aside from that, you have to notice the difference between lambda functions and closures, where closures are not yet available with the default PHP version WP requires: 5.2.1. –  kaiser May 3 '13 at 15:22
    
Sweet! Thanks kaiser! –  William May 6 '13 at 2:21
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.