wp-toolkit
WordPress Toolkit
Work with common WordPress Plugin and Theme APIs in a
modern, object-oriented way.
By Max Melzer
The WordPress Toolkit is a Composer
module that offers easy access to common WordPress functionality,
inspired by modern PHP frameworks like Symfony.
It’s aim is to provide consistent, object-oriented methods
for WordPress plugin and theme development.
Installation
To use the toolkit, add it to your theme or plugin via
Composer.
cd wp-content/themes/your-awesome-theme composer init composer require moehrenzahn/wp-toolkit
Then, you can initialize the Client class in your PHP script. This is
the entry point to all functionality of the toolkit.
<?php $client = new MoehrenzahnToolkitApiClient();
Requirements
- Composer
- PHP >= 7.1
- WordPress >= 5.0 (older versions may work but are untested)
Features
- convenient API around WordPress action and filter management
- object manager with simple, automatic dependency injection
- template rendering with Model-View-Controller architecture
-
add and manage
- JavaScript files
- CSS files
- image sizes
- shortcodes
- virtual user accounts
- Transients
- Post Types
- Post Terms and Meta
- Post Meta boxes
- Post filters
- comment Meta
- comment Meta boxes
- settings pages
- admin pages
- admin notices
- AJAX actions
- POST actions
Usage example
Add a CSS file to your theme:
<?php // Entry point for all actions is the Client object $client = new MoehrenzahnToolkitApiClient(); $stylesheets = $client->getStylesheetManager(); $stylesheets->add('eule-stylesheet', 'src/css/style.css', '1.0.0');
Add a shortcode with a custom template:
<?php $client = new MoehrenzahnToolkitApiClient(); $shortcodes = $client->getShortcodeManager(); $shortcodes->add( 'my-shortcode', $client->getViewFactory()->create('shortcode-template.phtml') );
Create a custom settings page with pre-built input type templates.
<?php $client = new MoehrenzahnToolkitApiClient(); $client->getAdminPageManager()->addSettingsPage( 'Sample settings page', 'sample-settings-page', getSections($client) ); /** * @param MoehrenzahnToolkitApiClient $client * @return MoehrenzahnToolkitViewSettingsSection[] */ function getSections(MoehrenzahnToolkitApiClient $client): array { $sectionBuilder = $client->getSettingsSectionBuilder(); $sectionBuilder->addSetting( 'my-sample-setting', 'A sample setting title', MoehrenzahnToolkitAdminPageSettingsSectionBuilder::SETTING_TYPE_BOOLEAN, 'A sample setting description.' ); $sectionBuilder->addSetting( 'my-sample-select', 'Select your thing', MoehrenzahnToolkitAdminPageSettingsSectionBuilder::SETTING_TYPE_SELECT, 'You can also do select inputs!', [ 'sample-option-value' => 'Sample option label', 'another-option-value' => 'Another option!', ] ); return [$sectionBuilder->create('sample-section', 'A sample settings section')]; }
Use the built-in View
class to render your templates and get
access to features like lazy-loading images and partials:
<?php /** @var MoehrenzahnToolkitView $view */ ?> <h2>Here comes a lazy-loaded partial that is only loaded when it's scrolled into view:</h2> <?php $view->renderLazyPartial( 'your-template-to-load.phtml', 'your-static-placeholder-template.phtml' ) ?>
Use the object manager to initalize objects with automatic dependency resolution.
<?php $client = new MoehrenzahnToolkitApiClient(); $objectManager = $client->getObjectManager(); /** * The object manager will try to automatically and recursively * resolve all dependencies of the given class. * Don't use this for very large projects since it can impact performance. */ $customObject = $objectManager->getSingleton(YourCustomClass::class);
Release History
-
1.0.0
- Initial release
Meta
Max Melzer – @_maxmelzer –
hi@moehrenzahn.de
Distributed under the GNU General Public License. See LICENSE.md
for more information.
https://github.com/moehrenzahn
Contributing
- Fork it (https://github.com/moehrenzahn/wp-toolkit/fork)
-
Create your feature branch (
git checkout -b feature/fooBar
) -
Commit your changes (
git commit -am 'Add some fooBar'
) -
Push to the branch (
git push origin feature/fooBar
) - Create a new Pull Request