WordPress - How to create simple plugin with shortcode
Creating a simple WordPress plugin involves a few steps. Below is an example of a minimalistic WordPress plugin that adds a custom shortcode to display a greeting message.
Step 1: Set Up Plugin Structure
Create a new folder for your plugin in the "wp-content/plugins" directory. Let's call it "simple-greeting-plugin".
Step 2: Create the Main Plugin File
Inside your plugin folder (simple-greeting-plugin), create a main PHP file, e.g., "simple-greeting-plugin.php".
Step 3: Add Plugin Information
In your main PHP file, add the plugin information in the comment header.
<?php
/*
Plugin Name: Simple Greeting Plugin
Description: A simple WordPress plugin that adds a greeting shortcode.
Version: 1.0
Author: Your Name
*/
Step 4: Add Greeting Shortcode Functionality
Now, let's add the functionality to display a greeting message using a shortcode. Update your "simple-greeting-plugin.php" file as follows:
<?php
/*
Plugin Name: Simple Greeting Plugin
Description: A simple WordPress plugin that adds a greeting shortcode.
Version: 1.0
Author: Your Name
*/
// Define the shortcode function
function simple_greeting_shortcode() {
return 'Hello, welcome to our website!';
}
// Register the shortcode
add_shortcode('greeting', 'simple_greeting_shortcode');
In this example, we've created a function "simple_greeting_shortcode" that returns the greeting message. The "add_shortcode" function is used to register the shortcode "[greeting]" and associate it with our function.
Step 5: Activate the Plugin
Go to your WordPress admin panel, navigate to "Plugins," and activate the "Simple Greeting Plugin."
Step 6: Use the Shortcode
Now you can use the "[greeting]" shortcode in your WordPress posts or pages to display the greeting message.
When you view the post or page containing the shortcode, it should display the greeting message.
This is a very basic example, but it illustrates the fundamental steps of creating a WordPress plugin. As you become more familiar with plugin development, you can explore additional features, options, and WordPress APIs to enhance your plugins.
Comments