the last couple of weeks i have been working on my own WordPress plugin. I am trying to do a class based codebase.
My plugin works correctly, but i don't really know if this is the correct way to develop. I would like to learn the best practices.
To demonstrate, here is my base plugin file:
<?php
/**
* Blub Hamerhaai plugin
*
* This is the main blub plugin for all sites
*
* @link {github link}
* @since 0.0.2
* @package Blub Hamerhaai
*
* @wordpress-plugin
* Plugin Name: {plugin name}
* Plugin URI: {github link}
* Description: This is the main blub plugin for all sites
* Version: 0.0.4
* Author: Blub
* Author URI: {link}
* License: Copyright Blub media, all rights reserved
* License URI: {link}
* Text Domain: blub-hamerhaai
* Domain Path: /languages
*
* Created by: Me
*/
namespace Blub;
require 'includes/includes.php';
define(__NAMESPACE__ . '\PLUGINROOT', __FILE__);
define(__NAMESPACE__ . '\PLUGINDIR', __DIR__);
if (!class_exists('Hamerhaai')) {
class Hamerhaai
{
public function __construct()
{
register_activation_hook(__FILE__, [$this, 'activation']);
register_deactivation_hook(__FILE__, [$this, 'deactivation']);
add_action('init', [$this, 'init']);
}
public function activation()
{
BlubRole::add();
PluginUpdate::add();
}
public function deactivation()
{
BlubRole::remove();
PluginUpdate::remove();
}
public function init()
{
//Preventing removals in other pages
global $pagenow;
load_textdomain('blub-hamerhaai', PLUGINDIR . '/languages/blub-hamerhaai-nl_NL.mo');
//Things to do for all users
new OptionsPage();
new BlubColors();
new CustomLoginPage();
$widget = new DashboardWidget();
// Things to do for blub user
if (BlubRole::isBlubUser()) {
new AdminText();
new RemoveMenuItems();
new GutenbergEdits();
//Only for dashboard
if ($pagenow === 'index.php') {
$widget->forceShow();
$widget->removeDefault();
}
new LoadFrontendStyle();
}
}
}
}
new Hamerhaai();
The main thing i am insecure about is instantiating all the classes on the plugin init function. I don't think this is the best practice. All the classes are based on a specific functionality.
Can someone explain me if this is the way to go? Or how to improve?
Thanks in advance.