How do you create a custom WordPress plugin?

How do you create a custom WordPress plugin?

To create a custom WordPress plugin in minimal steps:

1. Create Plugin Folder and File

  • Go to the wp-content/plugins/ directory.
  • Create a new folder for your plugin, e.g., my-custom-plugin.
  • Inside the folder, create a PHP file, e.g., my-custom-plugin.php.

2. Add Plugin Header Information

In your PHP file, add the plugin header to provide basic information:

Example

<?php
/**
 * Plugin Name: My Custom Plugin
 * Description: A simple custom plugin.
 * Version: 1.0
 * Author: Your Name
 * License: GPL2
 */

3. Write Your Plugin Code

Add the functionality you want for your plugin. For example, a simple function that adds a message to the WordPress footer:

Example

<?php
function my_custom_footer_message() {
    echo '<p>My custom footer message.</p>';
}

add_action('wp_footer', 'my_custom_footer_message');
?>

4. Activate the Plugin

  • Go to the WordPress admin dashboard.
  • Navigate to Plugins > Installed Plugins.
  • Find your custom plugin and click Activate.

5. Optional: Add Additional Files (CSS, JS)

If you need styles or scripts, create additional files (e.g., style.css, script.js) and enqueue them in your plugin:

Example

<?php
function my_custom_plugin_assets() {
    wp_enqueue_style('my-plugin-style', plugin_dir_url(__FILE__) . 'style.css');
    wp_enqueue_script('my-plugin-script', plugin_dir_url(__FILE__) . 'script.js', array('jquery'), null, true);
}

add_action('wp_enqueue_scripts', 'my_custom_plugin_assets');
?>
This is a basic process to get your custom plugin up and running.

Related Questions & Topics