Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Created April 4, 2019 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommcfarlin/94e25b3c6b304d348b8f4a69b03091fd to your computer and use it in GitHub Desktop.
Save tommcfarlin/94e25b3c6b304d348b8f4a69b03091fd to your computer and use it in GitHub Desktop.
[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