Page call using Class in Wordpress

Asked

Viewed 22 times

0

I am working on a plugin and created a class to display the option in the side menu and the content of the page related to this plugin, however I do not know how to display the content within it.

I used to do 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() { ...

This way the callback would search for a function with the same name passed and run, but these 2 functions are now within 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 made;

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

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

1 answer

0


Whenever you are referencing a method within a class you just 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 forms. Just be an array or string that can be interpreted by call_user_func() or call_user_func_array()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.