How to create a WordPress plugin from scratch?
Four steps:
- Make a folder in
wp-content/plugins, likemy-plugin. - Add a PHP file with the same name. Write the header comment with at least
Plugin Name. - Write your code with hooks (actions and filters), a unique prefix and an
ABSPATHcheck. - Activate it from the Plugins screen (or
wp plugin activate my-plugin).
<?php
/**
* Plugin Name: TC Hello Plugin
* Description: Adds a small text in the footer.
* Version: 1.0.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
add_action( 'wp_footer', function () {
echo '<p class="tchp">Made with WordPress</p>';
} );
Terminal output
$ wp plugin activate tc-hello-plugin
Plugin 'tc-hello-plugin' activated.
Success: Activated 1 of 1 plugins.
$ curl -s http://example.com/ | grep -o '<p class="tchp">[^<]*</p>'
<p class="tchp">Made with WordPress</p>
A full tested example with activation and uninstall is in Create a WordPress Plugin.
Learn more about the similar topics:
| Tutorials |
|---|
| WordPress Plugins |
| WordPress Top Plugins List |
| WordPress SEO with Yoast SEO Plugin |
| Create a WordPress Plugin from Scratch |
| WordPress Plugin Settings Page (Settings API) |
| Exercises & Assignments |
|---|
| Write a plugin which adds a text to the footer |