To write a simple WordPress plugin, follow these steps:
- Create a new folder in the “wp-content/plugins” directory of your WordPress installation. Name the folder after your plugin, using all lowercase letters and separating words with dashes (e.g. “my-simple-plugin”).
- Create a new PHP file with the same name as your plugin folder (e.g. “my-simple-plugin.php”) and open it in a text editor.
- Add the plugin header to the top of the PHP file. The plugin header is a block of comment lines that contain information about your plugin, such as the plugin name, author, version, and description. The header should look like this:
<?php
/*
Plugin Name: My Simple Plugin
Plugin URI: https://example.com/my-simple-plugin
Description: A simple plugin that does something cool.
Version: 1.0
Author: Your Name
Author URI: https://example.com
License: GPLv2 or later
*/
- Write the main function of your plugin. This is the core functionality of your plugin, and it should be wrapped in a PHP function with a unique name. For example:
function my_simple_plugin_function() {
// Plugin functionality goes here
}
- Register your plugin function with WordPress. You can do this by using the “add_action” function, which tells WordPress to execute your plugin function when a specific event occurs. For example:
add_action('wp_head', 'my_simple_plugin_function');
This code tells WordPress to execute the “my_simple_plugin_function” function when the “wp_head” event occurs (which is when the head section of a WordPress page is being generated).
- Save the PHP file and upload it to the plugin folder you created in step 1.
- Log in to your WordPress dashboard and go to the Plugins page. You should see your plugin listed in the plugin list.
- Activate your plugin by clicking on the “Activate”