Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[WordPress] Tools for Writing Better WordPress Code: Composer
<?php
/**
* The plugin bootstrap file
*
* This file is read by WordPress to generate the plugin information in the plugin
* admin area. This file also includes all of the dependencies used by the plugin,
* registers the activation and deactivation functions, and defines a function
* that starts the plugin.
*
* @since 0.1.0
* @package SamplePlugin
*
* @wordpress-plugin
* Plugin Name: Sample Plugin
* Description: A sample plugin used for a blog post.
* Version: 0.1.0
* Author: Tom McFarlin
* Author URI: https://tommcfarlin.com
* License: GPL-3.0+
* License URI: http://www.gnu.org/licenses/gpl-3.0.txt
*/
namespace SamplePlugin;
use SamplePlugin\Messenger;
// This file called directly.
defined('WPINC') || die;
// Include the autoloader.
require_once __DIR__ . '/vendor/autoload.php';
// Start the machine.
(new Messenger())->start();
<?php
namespace SamplePlugin;
class Messenger
{
/**
* Subscribers the `adminNotices` function to the admin_notices hook in WordPress.
*/
public function start()
{
add_action('admin_notices', [$this, 'adminNotices']);
}
/**
* Displays the contents of the `message.html` file into the adminitrative notices
* area on the dashboard whenever the plugin is active.
*/
public function adminNotices()
{
if ('dashboard' !== get_current_screen()->id) {
return;
}
include_once dirname(__FILE__) . '/Admin/Views/message.html';
}
}
<div class="notice notice-success is-dismissible">
<p>This is a message from the <strong>Sample Plugin</strong></p>
</div>
{
"name": "tommcfarlin/sample-plugin",
"description": "A sample plugin used for a blog post.",
"type": "wordpress-plugin",
"license": "GPL-3.0-or-later",
"authors": [
{
"name": "Tom McFarlin",
"email": "tom@tommcfarlin.com",
"homepage": "https://tommcfarlin.com"
}
],
"autoload": {
"psr-4": {
"SamplePlugin\\": "src/"
}
}
}
$ composer install
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.