I'm an experienced dev, but new to WordPress. I'm following along with a course on udemy and they suggested I encapsulate my plugin functions in a class to help avoid namespace conflicts. However, inside one of my action hooks (not sure that's the right term), I try to call add_menu_page() to add some menu items to the admin sidebar and I can't figure out how to reference my function. If I just give it the name of the function inside my class "testing_options_page", then WP can't find the function. If I try to wrap it in an array to declare the class name and the function like I did with the add_action() call, then the WP code horks because it doesn't expect an array for that param array('kenny_plugin6', 'testing_options_page').

How can I reference the function inside the class when calling the add_menu_page() function?

Side question: Is wrapping my code in a class going to be more pain that its worth?

add_action( 'admin_menu', array( 'kenny_plugin6', 'add_menu_items' ) );

class kenny_plugin6 {
   function add_menu_items() {
      add_menu_page( 'Browser Tab Title', 'Menu Item Title', 'manage_options', 
            'testing_menu_page', 'testing_options_page', '', '25' );
      add_submenu_page( 'testing_menu_page', 'Test Links', 'Test Title', 'manage_options', 
            'get_test_links' );


   function testing_options_page() {
      <h3>Testing Options</h3>

      <div>Put more stuff here</div>

   function get_test_links() {
      <h3>Test Links</h3>

      <div> Put more stuff here.</div>
  • 1
    @KrzysiekDróżdż But my problem is that while I understand the array structure used to represent the class/function combo, it doesn't seem to be working with add_menu_page() specifically. It works just fine with add_action() but when I pass an array structure to add_menu_page() then I get a WP error. – Kenny Wyland May 26 '18 at 16:35
  • It works fine. If you get some error, then you’re doing something wrong. Maybe you use wrong hooks or something like this... What does that error say? – Krzysiek Dróżdż May 26 '18 at 16:37
  • @KrzysiekDróżdż Well, now I don't know. I had rewritten the code to remove the class entirely and just have all the functions in the global namespace and prefixed the names to avoid collision, so I duplicated and rewrote the code again back to the class style so I could get the error message... and this time it worked. I don't know what I did wrong yesterday that was causing the problem. :/ – Kenny Wyland May 26 '18 at 16:43
  • Maybe some typo or something like that - it happens... – Krzysiek Dróżdż May 26 '18 at 16:45

add_action must be inside the plugin class and should look like

 add_action( 'admin_menu', array( $this, 'add_menu_items' ) );

Usually this line presents in the __construct() method.

| improve this answer | |
  • 1
    Well, it doesn't have to be in __construct method of the class, but it often is placed in there. – Krzysiek Dróżdż May 25 '18 at 23:42
  • 1
    @KrzysiekDróżdż yes, you are right. I have edited the answer. – KAGG Design May 27 '18 at 6:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.