Skip to main content
Dublin Library

The Publishing Project

Additional considerations for OOP plugin development

 

Rather than writing plugins (or any PHP code) procedurally, we can leverage Object-Oriented Programming (OOP) to write our plugins. OOP is easier to maintain and extend as the needs of the plugin increase. The code below is a good starting point for class-based plugins. It doesn't have the full plugin comment but the code should still work (even if it does nothing yet). ```php setup_actions(); } /** * Setting up Hooks */ public function setup_actions() { //Main plugin hooks register_activation_hook( __FILE__, array( 'AwesomePlugin', 'activate' ) ); register_deactivation_hook( __FILE__, array( 'AwesomePlugin', 'deactivate' ) ); } /** * Activate callback */ public static function activate() { //Activation code in here } /** * Deactivate callback */ public static function deactivate() { //Deactivation code in here } } // instantiate the plugin class $wp_plugin_template = new AwesomePlugin(); } ``` ## Constructor differences One thing that tripped me several times is the difference when working with class methods inside the class. Rather than referencing the methods directly, you must use an array syntax: ```php Edit on Github