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 have the following plugin that I'm just testing at the moment.

/*
Plugin Name: Menu Test
Plugin URI: http://codex.wordpress.org/Adding_Administration_Menus
Description: Menu Test
Author: Codex authors
Author URI: http://example.com
*/


include('./customFields.php');

// Hook for adding admin menus
add_action('admin_menu', array('customFields', 'buildOptions'));

// action function for above hook

And the class that goes with it in customFields.php

class customFields {

    private function createMenu () {
        add_media_page('Custom Media Options', 'Add Fields to Media',     'manage_options', 'custom_media_options', $this::createMenuOptionsPage());

    }

    private function createMenuOptionsPage () {
        echo 'test';

    }

    public function buildOptions () {
        $this::createMenu();
    }

}  

I can't see anything wrong with my syntax, but the submenu under media never appears. Can someone help me please?

share|improve this question

1 Answer 1

Whenever you would use a class method within add_action function, then within the array parameter, you have to pass an instance of that class as the first value and then you the method name as the second value of the array. In your code, you did not pass any instance actually. And that's why it did not work.

Try this:

class customFields {

    private function createMenu () {
        add_media_page('Custom Media Options', 'Add Fields to Media',     'manage_options', 'custom_media_options', $this->createMenuOptionsPage());

    }

    private function createMenuOptionsPage () {
        echo 'test';

    }

    public function buildOptions () {
        $this->createMenu();
    }

}  

$customFields = new customFields();
add_action('admin_menu', array($customFields, 'buildOptions'));
share|improve this answer
    
Please explain why this answer is helpful. –  kaiser May 16 at 22:28
    
In an edit please, not a comment. Make your answer easily understandable. –  kaiser May 17 at 10:21

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.