- Create a New Folder:
In the wp-content/plugins directory of your WordPress installation, create a new folder for your plugin. Name it something unique and descriptive. For this example, let’s call it test-plugin.
- Create the Main Plugin File:
In the wp-content/plugins directory of your WordPress installation, create a new folder for your plugin. Name it something unique and descriptive. For this example, let’s call it test-plugin.php.
- Add Plugin Information:
Open the main plugin file (test-plugin.php) and add the following header information at the top:
/*
Plugin Name: Test Plugin
Description: This is a custom WordPress plugin.
Version: 1.0
Author: Your Name
*/
- Define the Plugin Functionality:
Below the header, you can start defining your plugin’s functionality. For this example, we will display a “Hello, World!” message at the top of the website.
function my_hello_world_function() {
echo '<div class="hello-world">Hello, World!</div>';
}
- Hook the Function to WordPress:
To display your “Hello, World!” message, you need to hook your function into WordPress. You can do this by adding the following line to your main plugin file:
add_action('wp_head', 'my_hello_world_function');
- Create an Uninstall Function (Optional):
To remove any data or options created by your plugin when it’s uninstalled, you can define an uninstall function in your main plugin file:
function my_hello_world_plugin_uninstall() {
// Add uninstall actions here, if necessary
}
register_uninstall_hook(__FILE__, 'my_hello_world_plugin_uninstall');
- Activate the Plugin:
Log in to your WordPress dashboard, go to “Plugins,” and you should see your “Test Plugin.” Click the “Activate” button.