[resolved] Undefined Function Fatal Error In Plugin Class (3 posts)

  1. edtiley
    Posted 2 years ago #

    Building a plugin using Class structure.

    Want to display the result of a formula, so I created a public function so it could be done as needed. Something like:

    public function foo($x,$y) {
       return strval(($x+$y)*2.2) ;

    I keep getting an undefined function. Finally I put the function in functions.php and it works like a champ. So it's obvious the function isn't getting loaded.

    I've tried using init and plugins_loaded in add_action calls, but the function is still not available.

    Surely there's a fairly simple way of making the function available inside the plugin without having to hook it. Isn't there?


  2. ianhaycox
    Posted 2 years ago #

    If foo() is in a class are you calling it correctly ?

    e.g. within the plugin class,


    or from outside the class, you need a class instance,

    $x = new MyPluginClass();

    or make foo static,


    Once foo() in is functions.php, depedning how it's declared, foo() is probably in global scope, so just calling


    would work.


  3. edtiley
    Posted 2 years ago #

    Putting it in functions.php makes it globally available.

    Bingo! I searched unsuccessfully for that for hours. In all the tuts I've read on building class based plugins I don't think that little construct was explicitly mentioned. Or at least failed to penetrate my grayRAM.



Topic Closed

This topic has been closed to new replies.

About this Topic