piotrpress / wordpress-plugin
This library is WordPress plugin singleton base class with methods to get data from plugin's header fields.
v2.0.0
2022-02-22 16:57 UTC
Requires
- php: >=7.4
- piotrpress/singleton: ^6.0.0
This package is auto-updated.
Last update: 2024-04-22 21:40:35 UTC
README
This library is WordPress plugin singleton base class with methods to get data from plugin's header fields.
Installation
composer require piotrpress/wordpress-plugin
Usage
/** * Plugin Name: Example Plugin * Plugin URI: https://example.com/plugin/ * Description: Example Plugin description. * Version: 1.0.0 * Requires at least: 5.9 * Requires PHP: 7.4 * Author: John Smith * Author URI: https://example.com/plugin/author/ * License: GPL v3 or later * License URI: https://www.gnu.org/licenses/gpl-3.0.txt * Update URI: https://example.com/plugin/update/ * Text Domain: example-plugin * Domain Path: /languages */ require __DIR__ . '/vendor/autoload.php'; use PiotrPress\WordPress\Plugin; class Example extends Plugin { public function activation() : void {} public function deactivation() : void {} } Example::setInstance( __FILE__ ); echo Example::getName();
NOTE: Plugin's translations are loaded automatically according to Text Domain
and Domain Path
plugin's header fields.
Methods
Basic static methods handling plugin's default header fields
getName()
- returnsstring
with the name of the plugin fromPlugin Name
header fieldgetPluginURI()
- returnsstring
with the home page of the plugin or emptystring
ifPlugin URI
header field is not setgetVersion()
- returnsstring
with the current version number of the plugin or emptystring
ifVersion
header field is not setgetDescription()
- returnsstring
with a short description of the plugin or emptystring
ifDescription
header field is not setgetAuthor()
- returnsstring
with the name of the plugin author or emptystring
ifAuthor
header field is not setgetAuthorURI()
- returnsstring
with the website of the plugin's author or emptystring
ifAuthor URI
header field is not setgetTextDomain()
- returnsstring
with the gettext text domain of the plugin or directory name of the plugin ifText Domain
header field is not setgetDomainPath()
- returnsstring
with the path to translations directory or emptystring
ifDomain Path
header field is not setgetNetwork()
- returnsbool
whether the plugin can only be activated network-wide according toNetwork
header fieldgetRequiresWP()
- returnsstring
with the lowest WordPress version that the plugin will work on or emptystring
ifRequires at least
header field is not setgetRequiresPHP()
- returnsstring
with the minimum required PHP version or emptystring
ifRequires PHP
header field is not setgetUpdateURI()
- returnsstring
with third-party plugin's update server or emptystring
ifUpdate URI
header field is not set
Additional static methods handling plugin's paths
getSlug()
- returnsstring
with the directory name of the plugingetFile()
- returnsstring
with the path to main plugin's filegetDir()
- returnsstring
with the path to plugin's directorygetUrl()
- returnsstring
with the url to plugin's directorygetBaseName()
- returnsstring
with the basename of the plugin
Inherited Singleton's static methods
setInstance()
- executes__construct()
and can be called only once, otherwiseException
will be throwngetInstance()
- returnsnull
beforesetInstance()
successfully callissetInstance()
- returnstrue
if instance exists,false
otherwiseunsetInstance()
- unsets instance
Abstract methods handling plugin's de/activation
activation()
- executed while plugin activationdeactivation()
- executed while plugin deactivation
Handling custom plugin's header fields
- Add WordPress support for extra plugin's header fields using
extra_plugin_headers
filter:
add_filter( 'extra_plugin_headers', function () { return [ 'License', 'License URI' ]; } );
- Add methods to handle extra plugin's header fields:
class Example extends Plugin { public static function getLicenseURI() { return self::get( 'License URI' ); } }
NOTE: get
prefixed methods are automagically created for plugin's header fields that meet valid function name rules. e.g. getLicense()
method.
Requirements
PHP >= 7.4
version.