Calling page using Class in Wordpress

0

I'm working on a plugin and I created a class to display the option in the side menu and the content of the page related to this plugin, but I do not know how I can do to display the content inside it.

Before I did it this way;

function add_birthday_to_menu() {
    add_menu_page('Clientes',
        'Clientes',
        'manage_options',
        'birthday-celebrate',
        'birthday_celebrate_page', // A função de callback 
        'dashicons-universal-access',
        6);
}

function birthday_celebrate_page() { ...

In this way the callback would look for a function with the same name passed and would execute, however these 2 functions are now inside a class ...

class Birthday_celebrate
{
   public function add_birthday_to_menu()
   {
       add_menu_page('Clientes',
        'Clientes',
        'manage_options',
        'birthday-celebrate',
        'birthday_celebrate_page', // (?)
        'dashicons-universal-access',
        6);
   }

   public function birthday_celebrate_page() { ...

To add the menu using the class I did;

add_action('admin_menu', array('Birthday_celebrate', 'add_birthday_to_menu'));

But how do I make the add_menu_page function look for the callback within the class itself?

    
asked by anonymous 13.12.2016 / 01:03

1 answer

0

Whenever you are referencing a method within a class you simply pass the callback as array where the first element is the object itself:

class Birthday_celebrate
{
   public function add_birthday_to_menu()
   {
       add_menu_page('Clientes',
        'Clientes',
        'manage_options',
        'birthday-celebrate',
        array( $this, 'birthday_celebrate_page' ),
        'dashicons-universal-access',
        6);
   }

   public function birthday_celebrate_page() { ...

other options that work:

// para métodos estáticos
array( 'Birthday_celebrate::birthday_celebrate_page' )

// referenciando o namespace
array( __NAMESPACE__.'::Birthday_celebrate', 'birthday_celebrate_page' )

And there are other ways. Just be an array or string that can be interpreted by call_user_func () or call_user_func_array ()

    
13.12.2016 / 04:56