The framework provides basic code structure for Wordpress Plugins as well as necessary features such as configuration and logging.
It supports the following plugin functionality:
- admin pages
- rest endpoints
- actions
- filters
Additionally:
- provides config loader
- centralised logging via Monolog logger
- templating
The framework is based on a couple of base classes that define type of an Wordpress extension the plugin adds (admin page, rest endpoint, etc) and all extensions are expected to:
- Extend the base class
- Register the extension in PluginRegistry.
Base classes are:
- PFAdminPage.php
- PFRestEndpoint.php
- PFAction.php
- PFFilter.php
They all extend PFExtension.php that loads log and creates Monologue logger to be reused via getLogger() and getConfig() of the classes inheriting it.
- Create a new class in your plugin that extends PFAdminPage.
- Implement methods getMenuItem() and getPageContent()
- At your plugin .php file register your class with PluginRegistry::register()
Here's how it will look in practice:
# src/MyAdminPage.php
use WPPluginFramework/PFAdminPage;
class MyAdminPage extends PFAdminPage {
public function getMenu() {
return (new PFAdminMenuItem())
->setPageTitle("My menu")
->setMenuTitle("My menu")
->setMenuSlug("my_menu")
->setCapability("list_users");
}
public function getPageContent() {
return 'Hello from my amdin page!';
}
}- Extends PFRestEndpoint.
- Implement methods getMenuItem() and getPageContent()
- At your plugin .php file register your class with PluginRegistry::register()
Here's how it will look in practice:
# src/MyRestEndpoint.php
use WPPluginFramework/PFRestEndpoint;
class MyRestEndpoint extends PFRestEndpoint {
public function getNamespace(): string { return 'pf/v1'; }
public function getRoute(): string { return '/hello'; }
public function run(\WP_REST_Request $request): \WP_REST_Response {
return new \WP_REST_Response(['result' => 'hello here']);
}
}- Extends PFAction or PFFilter.
- Implement methods getName() and run()
- At your plugin .php file register your class with PluginRegistry::register()
Here's how it will look in practice:
# src/MyAction.php
use WPPluginFramework/PFAction;
class MyAction extends PFAction {
public function getHookName(): string { return 'init'; }
public function run($args) {
// act!
}
}# src/MyPage.php
use WPPluginFramework/PFPage;
class AboutUsPage extends PFPage {
public function getTitle(): string { return 'About Us'; }
public function getSlug(): string { return 'about'; }
public function getContent(): string {
return '<h1>About</h1><p>Our story…</p>';
}
}It doesn't really matter for the framework where files are located as long as they are imported in your plugin file, but probably something based on extension type would make sense. For example:
- src
- pages
- admin
- user
- filters
- actions
To properly load extension you need to:
- Import all extension classes in your plugin .php file, so all those that extend PFPage, PFAction, etc.
- Include WPPluginFramework/Loader
- Call
Loader::autoload(). This will automatically search for all loaded classes that extend PFExtension and call methodregister()for each of them. You can also load them manually my simply creating an instance of each class and callingregister()for it.
Simple example with autoload:
/*
* Plugin Name: My awesome plugin
*/
use Pages/Admin/MyAdminPage;
use Pages/User/AboutUsPage;
use WPPluginFramework/Loader;
Loader::autoload();