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 accomplish the simple task of adding a new page to the admin area but it seems that I just cannot wrap my mind around this simple function.

Via codex, this is how I'm trying to add the page:

public function __construct(){
    add_action('admin_menu', array( &$this, 'register_menu_page'));
}

public function register_menu_page () {
    $hookname = add_menu_page (
            'My Slider',
            'My Slider',
            'manage_options',
            'my_slider',
            My_Slider::render_admin_menu()
        );
}

When pressing the newly added button named My Slider I would expect the My_Slider::render_admin_menu() to be called and populate the right side of the page with some HTML (while keeping the left menu intact).

However this is what I'm getting:

text that always appears

First of all, you can see that the rendering function is always called (I'm currently on the Dashboard page) and it is weirdly inserted on the top of the page.

Second of all, when the button is pressed, wordpress is trying to access http://localhost/wordpress/wp-admin/my_slider.

What am I missing? Thank you!

share|improve this question
    
Use array( $this, 'callback_to_method' ) instead. And please add the callback method to your question. –  kaiser Aug 13 '13 at 18:16
    
@kaiser Ok this is unbelievable for me. It works with the array(&$this, 'callback_to_method' ). Well this was a fun, well spent afternoon:) Thank you! –  Ionut Aug 13 '13 at 18:38
2  
Please don't use &$this. Since PHP 5 (and WP needs a min. of 5.2.7), every object is passed as reference. So just use $this. And about your problem: You should read about static, as this is what you tried to - calling a static method. –  kaiser Aug 13 '13 at 20:13

1 Answer 1

up vote 1 down vote accepted

as the 5 parameter to add_menu_page() you send the resultat of running the function, insted of sending a callable reference to the function, as you did in the add_action.

$hookname = add_menu_page (
        'My Slider',
        'My Slider',
        'manage_options',
        'my_slider',
        array( 'My_Slider', 'render_admin_menu')
    );
share|improve this answer

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.