This library is WordPress plugin singleton base class with methods to get data from plugin's header fields.
composer require piotrpress/wordpress-plugin/**
* Plugin Name: Example Plugin
* Plugin URI: https://example.com/plugin/
* Description: Example Plugin description.
* Version: 1.0.0
* Requires at least: 6.2.2
* 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::getInstance( __FILE__ );
echo Example::getName();NOTE: Plugin's translations are loaded automatically according to Text Domain and Domain Path plugin's header fields.
getName()- returnsstringwith the name of the plugin fromPlugin Nameheader fieldgetPluginURI()- returnsstringwith the home page of the plugin or emptystringifPlugin URIheader field is not setgetVersion()- returnsstringwith the current version number of the plugin or emptystringifVersionheader field is not setgetDescription()- returnsstringwith a short description of the plugin or emptystringifDescriptionheader field is not setgetAuthor()- returnsstringwith the name of the plugin author or emptystringifAuthorheader field is not setgetAuthorURI()- returnsstringwith the website of the plugin's author or emptystringifAuthor URIheader field is not setgetTextDomain()- returnsstringwith the gettext text domain of the plugin or directory name of the plugin ifText Domainheader field is not setgetDomainPath()- returnsstringwith the path to translations directory or emptystringifDomain Pathheader field is not setgetNetwork()- returnsboolwhether the plugin can only be activated network-wide according toNetworkheader fieldgetRequiresWP()- returnsstringwith the lowest WordPress version that the plugin will work on or emptystringifRequires at leastheader field is not setgetRequiresPHP()- returnsstringwith the minimum required PHP version or emptystringifRequires PHPheader field is not setgetUpdateURI()- returnsstringwith third-party plugin's update server or emptystringifUpdate URIheader field is not setRequiresPlugins()- returnsstringwith the comma-separated list of WordPress.org-formatted slugs for its dependencies or emptystringifRequires Pluginsheader field is not set
getSlug()- returnsstringwith the sanitized name of the plugingetPrefix()- returnsstringwith the prefix for plugin's hooksgetFile()- returnsstringwith the path to main plugin's filegetDir()- returnsstringwith the path to plugin's directorygetUrl()- returnsstringwith the url to plugin's directorygetBaseName()- returnsstringwith the basename of the plugingetDirName()- returnsstringwith the directory name of the plugin
getInstance()- returns the instance of the Plugin class
activation()- executed while plugin activationdeactivation()- executed while plugin deactivation
- Add WordPress support for extra plugin's header fields using
extra_plugin_headersfilter:
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.
PHP >= 7.4 version.