Access wordpress functions inside a plugin class

In WordPress, most of the functions won’t get loaded until certain points in the event stack. In the Event Driven Architecture [EDA] of WordPress, you have to put your classes into actions or filters.

class MyGreatClass{

    public function __construct(){
        //I can use WP functions here
    }

    public function myGreatMethod(){
        return "I don't do much, but the WP functions are available to me!";
    }
}

//This line instantiates MyGreatClass and calls myGreatMethod during the "init" event.
add_action('init', array(new MyGreatClass, myGreatFunction'));

Leave a Comment