-
-
Save tommcfarlin/94e25b3c6b304d348b8f4a69b03091fd to your computer and use it in GitHub Desktop.
[WordPress] Tools for Writing Better WordPress Code: Composer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="notice notice-success is-dismissible"> | |
<p>This is a message from the <strong>Sample Plugin</strong></p> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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/" | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ composer install |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment