This is why I created
Howdy<\/a><\/strong>, a WordPress plugin boilerplate that aims to make using modern PHP concepts in WordPress plugin development easier.<\/p>\n
<\/figure>\nScope<\/h4>\nHowdy focuses on essential tools<\/strong> that improve productivity without overcomplicating the workflow. Rather than forcing every modern PHP practice into it, it prioritizes two foundational features:<\/p>\n
\n- Namespacing<\/a><\/li>\n
- Autoloading<\/a> (with Composer<\/a>)<\/li>\n<\/ul>\n
Namespacing<\/h5>\nNamespacing in PHP organizes classes, functions, and constants into logical groups, similar to how folders structure files, to prevent naming collisions. For example, two plugins defining a Security<\/code> class won\u2019t conflict if each uses a unique namespace.<\/p>\n
Traditionally, WordPress relies on prefixes for isolation. Suppose your plugin is named \u201cSimple Security by Acme.\u201d You\u2019d typically prefix functions and classes with your organization name Acme_<\/code> or acme_<\/code>:<\/p>\n
\r\n\/\/ Prefix in a function name.\r\nfunction acme_check_security() {\r\n \/\/ Add security check logic here\r\n}\r\n\r\n\/\/ Prefix in a class name.\r\nclass Acme_Security {\r\n public function check() {\r\n \/\/ Add class-specific logic here\r\n }\r\n}\r\n<\/pre>\nWhile namespaces can be used in WordPress plugins, adoption remains rare. This is because you can\u2019t use namespaces to their full potential without autoloading.<\/p>\n
Autoloading<\/h5>\nAutoloading classes in WordPress has two key limitations.<\/p>\n
First, you can\u2019t autoload third-party libraries, like openai-php\/client<\/a><\/strong>, without prefixing their namespaces. If two plugins load the same library, conflicting definitions will crash the site.<\/p>\n
These are the two main issues that Howdy<\/strong> aims to tackle.<\/p>\n
Once we have these two features properly set up, adopting other advanced PHP patterns, such as
dependency injection<\/a> or facades<\/a>, becomes much easier.<\/p>\n
So let\u2019s install Howdy<\/strong> and see it in action.<\/p>\n
Installation<\/h4>\nWe can install Howdy with the Composer create-project<\/code> command:<\/p>\n
\r\ncomposer create-project syntatis\/howdy -s dev\r\n<\/pre>\n
This command will create a new directory, howdy<\/code>, pull all the project files, and install the dependencies from
Packagist<\/a>.<\/p>\n
\r\ncomposer create-project syntatis\/howdy -s dev acme-plugin\r\n<\/pre>\nIt will then ask you to input the plugin slug. The plugin slug is required and should be unique. If you plan to publish your plugin on WordPress.org, this slug will be used in the plugin URL, e.g., https:\/\/wordpress.org\/plugins\/{slug}\/<\/code>. For this example, we\u2019ll use acme-plugin<\/code>.<\/p>\n
<\/figure>\nThe plugin slug will also be used to determine the default plugin name, the namespace prefix, and more. As we can see below, it\u2019s smart enough to transform the slug into the appropriate format. For this example, we\u2019ll keep the default plugin name while changing the namespace to Acme<\/code> instead of AcmePlugin<\/code>.<\/p>\n
<\/figure>\nOnce the inputs are completed, the necessary updates are made to the project files. For example, the file in app\/Plugin.php<\/code> will include the namespace and the prefix for the dependencies\u2019 namespace.<\/p>\n
<\/figure>\n
What\u2019s Included?<\/h4>\nHowdy comes pre-configured with these tools to streamline development:<\/p>\n
\n- PHP-Scoper<\/a><\/strong>: It allows us to add prefixes for dependencies installed with Composer to prevent conflicts when using the same libraries.<\/li>\n
- PHPCS<\/a><\/strong>: It includes PHPCS, but instead of using the WordPress Coding Standard<\/a>, the library applies modern coding standards that are well-established in the PHP ecosystem, such as PSR-12<\/a>, Doctrine<\/a>, and Slevomat<\/a>.<\/li>\n
- Kubrick<\/a><\/strong>: A collection of React.js components to build applications like the settings page in the WordPress admin.<\/li>\n
- @wordpress\/scripts<\/a><\/strong>: To compile JavaScript and stylesheets. You can run the following command to start watching files and automatically compile changes:\n
\r\nnpm run start\r\n<\/pre>\n<\/li>\n<\/ul>\nDirectory Structure<\/h4>\n
Howdy<\/strong> uses a slightly unconventional structure for a WordPress plugin. However, if you\u2019re familiar with frameworks like Laravel<\/a> or Symfony<\/a>, you\u2019ll adapt quickly.<\/p>\n
There are three main directories:<\/p>\n
\n- app<\/strong>: This directory should house your plugin\u2019s core classes and business logic. Classes in this directory are autoloaded automatically if they follow the PSR-4 standard<\/a>.<\/li>\n
- inc<\/strong>: This directory contains configuration files, utilities, and HTML templates.<\/li>\n
- src<\/strong>: This directory contains the uncompiled source for JavaScript and stylesheet files.<\/li>\n<\/ol>\n
Installing External Dependencies<\/h4>\nNow that we have the plugin boilerplate set up, we can easily install additional packages with Composer. For example, if we want to build a plugin with OpenAI integration, we can include the openai-php\/client<\/code> package using the following command:<\/p>\n
\r\ncomposer require openai-php\/client\r\n<\/pre>\nHowdy will automatically add a prefix to the namespaces of all classes in this package after it\u2019s installed.<\/p>\n
You can also install packages specifically for development. For example, to install symfony\/var-dumper<\/strong><\/a>, a popular PHP package for debugging, you can run:<\/p>\n
\r\ncomposer require symfony\/var-dumper --dev\r\n<\/pre>\nThis package provides a more user-friendly debugging experience compared to PHP\u2019s native var_dump<\/code> function.<\/p>\n
<\/figure>\nPreparing for Production<\/h4>\n
Lastly, Howdy provides several commands to prepare your plugin for release:<\/p>\n
npm run build<\/code>: Builds all asset files, including JavaScript and stylesheets, in the src<\/code> directory. These files are optimized and minified for production.<\/p>\n
composer run build<\/code>: Re-compiles the project and removes packages installed for development.<\/p>\n
composer run plugin:zip<\/code>: Creates an installable ZIP file for the plugin. Unnecessary files like dotfiles, src<\/code> directories, and node_modules<\/code> are excluded from the final archive.<\/p>\n
Wrapping Up<\/h4>\nIn this article, we\u2019ve explored Howdy and its benefits for WordPress plugin development.<\/p>\n
Howdy is designed to modernize plugin development without overwhelming you. It avoids bloating your workflow with every trendy tool (e.g., PHPUnit, PHPStan, or TypeScript are not included by default), but you can add them later as needed.<\/p>\n
By solving dependency conflicts and enabling Composer, Howdy bridges WordPress development with the broader PHP ecosystem, unlocking countless possibilities for building maintainable and scalable plugins.<\/p>\n
The post Howdy: Modern WordPress Plugin Boilerplate<\/a> appeared first on Hongkiat<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"