Writing an Addofy module
A module is a class that Addofy boots for you. It can be switched on and off in Addofy > Modules, gets its own settings card, and only runs on the screens it names. If you have not built one yet, start with the quick start.
A complete module
use AddoBack\Core\Screen;
use AddoBack\Modules\AbstractModule;
final class My_Reports_Module extends AbstractModule {
public function id(): string {
return 'myplugin_reports'; // Prefix it with your plugin slug.
}
public function label(): string {
return __( 'Reports', 'myplugin' );
}
public function description(): string {
return __( 'Adds a report card to the customer screen.', 'myplugin' );
}
public function default_enabled(): bool {
return true;
}
public function screens(): array {
return array( 'woocommerce_page_adba-customers' ); // Empty array: every screen.
}
public function settings_schema(): array {
return array(
'limit' => array(
'type' => 'number',
'label' => __( 'Rows', 'myplugin' ),
'default' => 5,
),
);
}
public function register(): void {
// Hooks that must exist on every request (REST routes, admin_post handlers, filters).
}
public function on_screen( Screen $screen ): void {
$limit = (int) $this->setting( 'limit', 5 );
adba( 'assets' )->enqueue_external( 'myplugin-reports', plugins_url( 'reports.js', __FILE__ ), array( 'adba-core' ) );
}
}
Register it from your main plugin file. The class extends an Addofy class, so keep it in its own file and load that file only when Addofy is active: a plugin that loads before Addofy would otherwise fail.
add_action( 'plugins_loaded', static function () {
if ( function_exists( 'adba_register_module' ) ) {
require_once __DIR__ . '/includes/class-my-reports-module.php';
adba_register_module( My_Reports_Module::class );
}
}, 5 );
What each part does
register()runs on every request. Use it for hooks that must always exist, such as REST routes,admin_posthandlers and filters.on_screen()runs only on the screens listed inscreens(). Use it for assets, notices and data for that screen.- Settings of your module are stored under
module_settings.{module id}in the Addofy settings, so they travel with Addofy's export and import. Read them with$this->setting( $key, $default ). - Field types for the settings schema:
toggle,select,radio,text,number,role_matrix,choice_map, plus your own (seeadba_register_fieldsin the settings guide). requires()lists module ids that must boot first.conditions()lists environment checks:woocommerce,acf, or your own throughadba_module_condition.- A field in the settings schema with
'experimental' => truegets the label Experimental on the Modules tab.
Errors never break the admin
A module that throws in register() or on_screen() is skipped for that request and admins see one notice. The rest of the admin keeps working.
Timing
| Moment | Hook | Use it for |
|---|---|---|
plugins_loaded priority 20 | adba_modules | Addofy collects the module classes (use adba_register_module() before this). |
After each module's register() | adba_module_booted | Wait for another module. |
| After boot | adba_loaded | Anything that needs the container. |
current_screen, Addofy active | adba_screen | Screen-specific work: assets, notices, data. |
admin_bar_menu priority 1, 100 and PHP_INT_MAX | native | Addofy adds the sidebar toggle at 1, its nodes at 100 and tidies the account menu (Log Out last) at PHP_INT_MAX. Hook below or above 100 to place your nodes before or after. |