• Resolved oldalmuhely_team

    (@oldalmuhely_team)


    I tried to write a class to set up and handle my Theme Options panels. There is more option panels, and I try to arrange them by a class with making one representative of the class to each theme option panel. Eg.:
    $firstpanel = new ownPanel();
    $secontpanel = new ownPanel();

    I have done the script and style loading with a member function, but I had an error while I tried to use add_theme_page() function in a member function of my class (this member function would call add_theme_page to create a panel, and it’d handle the saving and reset), BUT I got “Call undefined function add_theme_page()…”error
    Please HELP ME , how to use add_theme_page()inside a member function of my own class!!! Thx!

Viewing 14 replies - 1 through 14 (of 14 total)
  • Does add_theme_page() work if its not in your class? Or does it fail then too?

    Thread Starter oldalmuhely_team

    (@oldalmuhely_team)

    Yes, it does! It works very well, if it is in a simple function but whenever I place it inside my class member function I get the error.
    How can I fix it??

    Interesting. Can you post the line with the function call?

    Also, add_theme_page looks like a wrapper to add_submenu_page with ‘themes.php’ as the first parameter. Does add_submenu_page work?

    Thread Starter oldalmuhely_team

    (@oldalmuhely_team)

    My class is in a separate php file in mytheme/functions/includes folder.
    I include this php and my class in functions.php with include.
    My class looks like this:

    class myowntheme {

    public function addThePage() {
    add_theme_page($this->tname, $this->tname, ‘administrator’,’functions.php’, ‘mytheme_admin’);
    }

    ..
    }

    And in functions.php I try to call it with an action:
    add_action(‘admin_menu’,$classRepresentative->addThePage());

    and after that I get the error…

    What do I have to do?? Thanks for further help!

    Thread Starter oldalmuhely_team

    (@oldalmuhely_team)

    And I didn’t mention, that using add_submenu_page() throw same error…Any of these kind of functions didn’t work!

    Moderator keesiemeijer

    (@keesiemeijer)

    if the function is a method of a class within the plugin it should be referenced as array( $this, ‘function_name’ ). Try with this:

    add_theme_page($this->tname, $this->tname, 'administrator','functions.php', array(&$this, 'mytheme_admin'));

    keesiemeijer is right. From inside a class you are going to have to pass the callback function as an array. I think that’s the cause of your problem.

    Thread Starter oldalmuhely_team

    (@oldalmuhely_team)

    Many-many thanks for help! I’ll try it as soon as it’s possible and I’ll tell you if it works! Thanks once more!

    Thread Starter oldalmuhely_team

    (@oldalmuhely_team)

    I’ve tried your idea, but it didn’t work. In my example code below, you can see I reference the callback function as an array (as you recommended), but nothing happend…just the same error, what I got! The function themeUi is a public member function of my class as the addAdmin() function. Is there any solution?? Please help!

    public function addAdmin() {
    add_theme_page($this->tname, $this->tname, ‘administrator’,’functions.php’, array(&$this,’themeUi’));
    }

    Moderator keesiemeijer

    (@keesiemeijer)

    Can you put the class in a pastebin, submit it and give us a link to it here.

    Thread Starter oldalmuhely_team

    (@oldalmuhely_team)

    Now, I’ve solved the original trouble I think with using this:

    add_theme_page($this->tname, $this->tname, ‘administrator’,’functions.php’, $this->themeUi());

    and using the add_action() function inside the class constructor. In this function both array(&$this,’addAdmin’) and $this->addAdmin() work well!
    BUT a new misery occured while the theme option menu appears, because it is pointing to directly wp-admin/functions.php(it doesn’t exist in fact!!), and not to ?page=functions.php! Why? Can you help me?

    And I paste the shortened code:
    https://pastebin.com/7uQvdGMh

    Moderator keesiemeijer

    (@keesiemeijer)

    If I try this on my testserver with:

    public function addAdmin() {
            add_theme_page($this->tname, $this->tname, 'administrator','functions.php', array(&$this,'themeUi'));
        }

    I see “This will be the panel” on wp-admin/themes.php?page=functions.php

    Thread Starter oldalmuhely_team

    (@oldalmuhely_team)

    Ohh..finally!!! I was successfull with your solution! Thank you for your constant and enduring help! My final code looks like this:
    https://pastebin.com/i5VLvrxF
    I’ve used $this->addAdmin() before , but I changed it to your array(&$this,’addAdmin’)…I’ve tried it earlier, but it works only now…I don’t know why?! Maybe I missed something before, or I was just thoughtless.
    Many Thanks once again!!!!!

    Moderator keesiemeijer

    (@keesiemeijer)

    You’re welcome, glad you got it resolved.

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Use add_theme_page() func. in a class member function’ is closed to new replies.