WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. edtiley
    Member
    Posted 1 year 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?

    TIA
    Ed

  2. ianhaycox
    Member
    Posted 1 year ago #

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

    e.g. within the plugin class,

    $this->foo();

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

    $x = new MyPluginClass();
    $x->foo();

    or make foo static,

    MyPluginClass::foo();

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

    foo();

    would work.

    Ian.

  3. edtiley
    Member
    Posted 1 year ago #

    Putting it in functions.php makes it globally available.

    $this->foo();
    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.

    Thanks.

    Ed

Topic Closed

This topic has been closed to new replies.

About this Topic