
How to Connect Your PHP Website to a MySQL Database
June 12, 2024
Part 2: Enhancing Your WordPress Plugin to Search a MySQL Database
June 12, 2024How to Create a WordPress Plugin: A Step-by-Step Guide
Creating a WordPress plugin can seem daunting, but it’s a powerful way to extend the functionality of your WordPress site. Whether you’re adding custom features, integrating third-party services, or modifying existing behavior, a plugin is the way to go. In this guide, we’ll walk you through the process of creating a simple WordPress plugin from scratch.
Step 1: Setting Up Your Development Environment
Before we start coding, make sure you have a local WordPress development environment set up. You can use tools like XAMPP, MAMP, or Local by Flywheel to create a local WordPress installation.
Step 2: Creating the Plugin Folder and File
- Navigate to the Plugins Directory: Go to the
wp-content/pluginsdirectory in your WordPress installation. - Create a Plugin Folder: Create a new folder for your plugin. Let’s call it
my-first-plugin. - Create the Main Plugin File: Inside your plugin folder, create a PHP file. Name it
my-first-plugin.php.
Step 3: Adding Plugin Header Information
Open my-first-plugin.php and add the following header information. This tells WordPress about your plugin.
phpCopy code<?php
/**
* Plugin Name: My First Plugin
* Plugin URI: http://example.com/my-first-plugin
* Description: This is my first WordPress plugin.
* Version: 1.0
* Author: Your Name
* Author URI: http://example.com
* License: GPL2
*/
Step 4: Creating a Simple Shortcode
A shortcode is an easy way to add custom content to your WordPress posts and pages. Let’s create a simple shortcode that displays a greeting message.
phpCopy code// my-first-plugin.php
// Prevent direct access to the file
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Create a function that returns a greeting message
function my_first_plugin_greeting() {
return "Hello, welcome to my WordPress site!";
}
// Register the shortcode
function my_first_plugin_register_shortcodes() {
add_shortcode('greeting', 'my_first_plugin_greeting');
}
add_action('init', 'my_first_plugin_register_shortcodes');
You can now use the [greeting] shortcode in any post or page, and it will display the greeting message.
Step 5: Adding an Admin Page
Let’s add an admin page to manage the settings of your plugin.
- Create an Admin Menu: Add the following code to create a menu item in the WordPress admin sidebar.
phpCopy code// my-first-plugin.php
// Create an admin menu
function my_first_plugin_menu() {
add_menu_page(
'My First Plugin Settings',
'My First Plugin',
'manage_options',
'my-first-plugin',
'my_first_plugin_settings_page',
'',
100
);
}
add_action('admin_menu', 'my_first_plugin_menu');
- Create the Settings Page: Add the function that generates the content of the settings page.
phpCopy code// my-first-plugin.php
// Settings page content
function my_first_plugin_settings_page() {
?>
<div class="wrap">
<h1>My First Plugin Settings</h1>
<form method="post" action="options.php">
<?php
settings_fields('my_first_plugin_settings_group');
do_settings_sections('my-first-plugin');
submit_button();
?>
</form>
</div>
<?php
}
Step 6: Adding Settings to Your Plugin
Now, let’s add a setting to our plugin and display it on the settings page.
- Register the Setting: Add the following code to register a setting.
phpCopy code// my-first-plugin.php
// Register a setting
function my_first_plugin_register_settings() {
register_setting('my_first_plugin_settings_group', 'my_first_plugin_setting');
add_settings_section('my_first_plugin_settings_section', 'General Settings', null, 'my-first-plugin');
add_settings_field('my_first_plugin_setting_field', 'Greeting Message', 'my_first_plugin_setting_field_callback', 'my-first-plugin', 'my_first_plugin_settings_section');
}
add_action('admin_init', 'my_first_plugin_register_settings');
- Create the Setting Field Callback: Add the callback function that displays the setting field.
phpCopy code// my-first-plugin.php
// Setting field callback
function my_first_plugin_setting_field_callback() {
$setting = get_option('my_first_plugin_setting');
?>
<input type="text" name="my_first_plugin_setting" value="<?php echo isset($setting) ? esc_attr($setting) : ''; ?>" />
<?php
}
- Modify the Shortcode to Use the Setting: Update the shortcode function to use the greeting message from the plugin settings.
phpCopy code// my-first-plugin.php
// Update the shortcode function
function my_first_plugin_greeting() {
$greeting_message = get_option('my_first_plugin_setting', 'Hello, welcome to my WordPress site!');
return $greeting_message;
}
Conclusion
Congratulations! You’ve created a simple WordPress plugin that includes a shortcode and an admin settings page. You learned how to structure your plugin, create shortcodes, add admin menus, and manage plugin settings. This is just the beginning. From here, you can start adding more features and functionality to your plugin.
Happy coding!

