In WordPress, a plugin is a small software application that extends the features and functions of a WordPress website. Plugins play a major role in building custom websites using WordPress.

Folder Location

In a typical WordPress installation, plugins are stored in the /wp-content/plugins/ directory.

Create a folder my-plugin at that location, inside the my-plugin folder, create the plugin’s main PHP file, example โ€” my-plugin.php.

Plugin Header Information

This section is required for WordPress to recognize that the file is a plugin and activate the plugin.

/*
 * Plugin Name:       My Plugin
 * Plugin URI:        https://learn.rtcamp.com
 * Description:       Handle the basics with this plugin.
 * Version:           1.0.0
 * Requires at least: 5.0
 * Requires PHP:      7.2
 * Author:            Lakshyajeet
 * Author URI:        https://learn.rtcamp.com
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Update URI:        https://learn.rtcamp.com
 * Domain Path:       /languages
 */
  • Plugin Name: The name of the plugin that will appear in the WordPress dashboard. This is the only compulsory parameter here.
  • Plugin URI: The URL where users can learn more about the plugin (website or a documentation page).
  • Description: A short description of what the plugin does.
  • Version: The version number of the plugin.
  • Requires at least: The minimum WordPress version required for the plugin.
  • Requires PHP: The minimum PHP version required for the plugin.
  • Author: The name of the plugin author.
  • Author URI: A URL to the authorโ€™s website or profile.
  • License: The type of license (in this case, GPL v2).
  • License URI: A URL to the full license details.
  • Update URI: A URL where users can find updates or new versions.
  • Domain Path: Path to the language files for translations (if any).

Activation and Deactivation Hooks

In WordPress, we can define functions that run when the plugin is activated, deactivated, or uninstalled.
These functions are hooked into specific WordPress actions.

  • Activation Hook: Runs when the plugin is activated.
  • Deactivation Hook: Runs when the plugin is deactivated.
  • Uninstall Hook: Runs when the plugin is uninstalled.

Activation Hook

This hook runs when the plugin is activated. In the function we can perform actions like creating custom database tables or adding options to the WordPress settings.

function mlb_activation() {
    // Code to execute during activation
}
register_activation_hook(
    __FILE__,
    'mlb_activation'
);

Deactivation Hook

This hook runs when the plugin is deactivated. We can use it to remove or clean up any resources that were added by the plugin (egโ€” removing custom post types).

function mlb_deactivation() {
    // Code to execute during deactivation
}
register_deactivation_hook(
    __FILE__,
    'mlb_deactivation'
);

Uninstall Hook

This hook runs when the plugin is uninstalled. We can use it for clean-up tasks like deleting plugin-specific options from the database.

function mlb_uninstall() {
    // Code to execute during uninstall
}
register_uninstall_hook(
    __FILE__,
    'mlb_uninstall'
);

Further Development

Once we have the basic structure, we can start adding other functionality to our plugin.

Adding Custom Post Types

Custom post types help organize content in a structured way.

function mlb_custom_post_type() {
    register_post_type('mlb_event', array(
        'labels' => array(
            'name' => 'Events',
            'singular_name' => 'Event',
        ),
        'public' => true,
    ));
}
add_action('init', 'mlb_custom_post_type');

Creating Shortcodes

A shortcode lets us add custom content to posts and pages.

function mlb_shortcode() {
    return "<p>Hello, this is a shortcode!</p>";
}
add_shortcode('say_hello', 'mlb_shortcode');

Additional Resources

One response to “WordPress Plugin Development Basics”

  1. […] the previous blog we learnt about the basics of how to create a plugin in WordPress, this blog dives into the next […]

Leave a Reply

Your email address will not be published. Required fields are marked *