Create Wordpress Plugin

Create your first wordpress pluginCreate your first custom wordpress plugin

What is a plugin, and how do you create a WordPress plugin?

In WordPress, a plugin is a piece of software containing a group of functions that can be added to a WordPress website. They extend the functionality of WordPress without altering the core system.
Creating a plugin in Wordpress involoves a few basic steps. Here's a straightforward guide to get started:

Setup your plugin directory:

Create a new directory in 'wp-content/plugins' directory of your wordpress installation. Name it something unique and relevant to your plugin.

Create the main plugin file: 

In your plugin directory, create a PHP file. This file will serve as the main entry point for your plugin.
Name this file something unique, usually following the format 'your-plugin-name.php' or 'index.php'.

Add plugin header information:

At the top of your main plugin file, add a comment block with informtion about your plugin. This includes the plugin name, description, version, author, and other relevant details. Here's an example:
<?php
/*
 * Plugin Name: My Plugin
 * Description: This is my first wordpress plugin
 * Version: 1.1
 * Author: Buggerspot
 * Author URI: https://buggerspot.com/
 */
My first plugin in wordpress

Write Plugin Code:

Echo your 'Hello, world!' text in that file; it will be printed on all pages after activating your plugin.
echo 'Hello, world!';
Begin writing code for your plugin. This can include functions to modify wordpress behaviour, add new features, or interact with databases.

You can use Wordpress action hooks and filters to integrate your plugin with wordpress core functionality. For example, you might use add_action() to execute a function when a certain event occurs, like when Wordpress initializes or a post is saved.

Here is the example code to enqueue script for plugin
function plugin_enqueue_script() {
    wp_enqueue_script('jquery', 'https://code.jquery.com/jquery-3.6.0.min.js', array(), '3.6.0', true);    
}
add_action('wp_enqueue_scripts', 'plugin_enqueue_script');

Add settings button for your plugin:

To add a settings button for your plugin in the installed plugins list, you typically need to hook into the action links of your plugin entry in the plugins list. Here's how you can achieve this:
function my_plugin_settings_link($links) {
    $settings_link = '<a href="options-general.php?page=my-plugin-settings">Settings</a>';
    array_unshift($links, $settings_link);
    return $links;
}
add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'my_plugin_settings_link');

Replace 'my-plugin-settings' with the slug of your settings page.

Create settings button in wordpress plugin

Define the Settings Page Function:

Inside settings-page.php, define a function that will render the settings page HTML. You can use the WordPress Settings API to handle form submission and validation. Here's a basic example:

function my_plugin_settings_page() {
    ?>
    <div class="wrap">
        <h2>My Plugin Settings</h2>
        <form method="post" action="options.php">
            <?php settings_fields('my_plugin_settings'); ?>
            <?php do_settings_sections('my_plugin_settings'); ?>
            <?php submit_button(); ?>
        </form>
    </div>
    <?php
}

Register Settings:

In your main plugin file, use the register_setting() function to register the settings for your plugin. This function defines the settings that will be available on the settings page and how they should be sanitized.
function my_plugin_register_settings() {
    register_setting('my_plugin_settings', 'my_plugin_option_name', 'sanitize_callback');
}
add_action('admin_init', 'my_plugin_register_settings');

Add Settings Sections and Fields:

Use the add_settings_section() and add_settings_field() functions to define sections and fields on your settings page. Sections group related settings together, while fields represent individual settings.
function my_plugin_settings_init() {
    add_settings_section('my_plugin_main_section', 'Main Settings', 'my_plugin_section_callback', 'my_plugin_settings');
    add_settings_field('my_plugin_text_field', 'Text Field', 'my_plugin_text_field_callback', 'my_plugin_settings', 'my_plugin_main_section');
}
add_action('admin_init', 'my_plugin_settings_init');

function my_plugin_section_callback() {
    echo '<p>Enter your settings here:</p>';
}

function my_plugin_text_field_callback() {
    $value = get_option('my_plugin_option_name');
    echo '<input type="text" name="my_plugin_option_name" value="' . esc_attr($value) . '" />';
}

Hook into WordPress Admin Menu:

Finally, hook your settings page function into the WordPress admin menu using the add_menu_page() or add_submenu_page() function.
function my_plugin_add_settings_page() {
    add_submenu_page('options-general.php', 'My Plugin Settings', 'My Plugin', 'manage_options', 'my-plugin-settings', 'my_plugin_settings_page');
}
add_action('admin_menu', 'my_plugin_add_settings_page');
Once you've completed these steps, you should have a functioning settings page for your plugin in the WordPress admin area. Users can access and configure the settings from there.

Craete custom settings in wordpress

Wordpress plugin settings page

Here is the complete code of the plugin file
<?php
/*
 * Plugin Name: My Plugin
 * Description: This is my first wordpress plugin
 * Version: 1.1
 * Author: Buggerspot
 * Author URI: https://buggerspot.com/
 */
 
echo 'Hello, world!';
function my_plugin_settings_link($links) {
    $settings_link = '<a href="options-general.php?page=my-plugin-settings">Settings</a>';
    array_unshift($links, $settings_link);
    return $links;
}
add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'my_plugin_settings_link');

function my_plugin_settings_page() {
    ?>
    <div class="wrap">
        <h2>My Plugin Settings</h2>
        <form method="post" action="options.php">
            <?php settings_fields('my_plugin_settings'); ?>
            <?php do_settings_sections('my_plugin_settings'); ?>
            <?php submit_button(); ?>
        </form>
    </div>
    <?php
}

function my_plugin_register_settings() {
    register_setting('my_plugin_settings', 'my_plugin_option_name', 'sanitize_callback');
}
add_action('admin_init', 'my_plugin_register_settings');

function my_plugin_settings_init() {
    add_settings_section('my_plugin_main_section', 'Main Settings', 'my_plugin_section_callback', 'my_plugin_settings');
    add_settings_field('my_plugin_text_field', 'Text Field', 'my_plugin_text_field_callback', 'my_plugin_settings', 'my_plugin_main_section');
}
add_action('admin_init', 'my_plugin_settings_init');

function my_plugin_section_callback() {
    echo '<p>Enter your settings here:</p>';
}

function my_plugin_text_field_callback() {
    $value = get_option('my_plugin_option_name');
    echo '<input type="text" name="my_plugin_option_name" value="' . esc_attr($value) . '" />';
}

function my_plugin_add_settings_page() {
    add_submenu_page('options-general.php', 'My Plugin Settings', 'My Plugin', 'manage_options', 'my-plugin-settings', 'my_plugin_settings_page');
}
add_action('admin_menu', 'my_plugin_add_settings_page');

I have created only a sample piece of code demonstrating how to create a plugin. You can modify it to suit your needs.

Thank You!