Horje
How To Build Your Very Own WordPress Plugin?

In WordPress, a plugin is a small piece of software that adds specific features or functionalities to a WordPress website. Plugins allow users to extend and enhance the capabilities of their WordPress site without having to write code from scratch. They can be used to add a wide variety of features, such as contact forms, SEO tools, security enhancements, social media integrations, and more.

Prerequisites

Steps for creating your own WordPress Plugin

Step 1: Setting Up Your Environment

Begin by setting up your local development environment. This allows you to develop and test your plugin safely before deploying it to a live site. I am using the XAMPP as my local development environment, which has the following interface after starting it.

Screenshot-2024-06-30-121012

Step 2: Login to WordPress

After setup go to the WordPress login panel and enter the Username and Password to login into the wordpress admin Dashboard. You can access by entering the /wp-login.php at the end of url.

Example: http://localhost/z/wp-login.php

Screenshot-2024-07-06-213804

After login into WordPress, you will be able to see a dashboard like this:

Screenshot-2024-07-06-215229-compressed

Step 3: Navigate to Your WordPress Directory

Generally its located at C:\xampp\htdocs\wordpress-folder-name

Locate your Plugin directory

C:\xampp\htdocs\wordpress-folder-name\wp-content\plugins

Step 4: Create Your Plugin Folder

After setting up the wordpress go to the following directory C:\xampp\htdocs\z\wp-content\plugins and in this plugin folder create the folder with the name which you want to give to your plugin. we have given the name as Custom-Plugin as you can se in the below image.

Screenshot-2024-06-30-122039

Step 5: Create the Main Plugin File

Inside your new plugin folder, create a PHP file. This file will serve as the main entry point for your plugin. For our example, create a file named custom-plugin.php.

Screenshot-2024-07-06-215710

Step 6: Add the Plugin Header

At the top of your main plugin file(custom-plugin.php), add a plugin header comment. This comment block provides WordPress with essential information about your plugin:

/*
Plugin Name: You own first Plugin
Plugin URI: http://yourwebsite.com/
Description: A brief description of what your plugin does.
Version: 1.0
Author: Your Name
Author URI: http://yourwebsite.com/
License: GPL2
*/

Step 7: Writing Your Plugin Code

With the basics in place, it’s time to add functionality to your plugin. Here’s an example of a simple plugin that adds a shortcode to display “Hello, World!”:
Add the below code inside the custom-plugin.php file below the plugin header.

function my_custom_shortcode() {
return "Hello, World!";
}
add_shortcode('hello_world', 'my_custom_shortcode');

This code snippet creates a shortcode [hello_world] that outputs “Hello, World!” when used in a post or page.

Step 8: Activate Your Plugin

To see your plugin in action, you need to activate it. Go to your WordPress admin dashboard, navigate to the Plugins menu, find your plugin in the list, and click “Activate.”

Screenshot-2024-06-30-125715

Step 9: Extending Your Plugin

Your plugin can do much more than just display text. You can extend its functionality in numerous ways:

  • Hooks and Filters: Use WordPress hooks to interact with the core functionality.
  • Custom Post Types: Register custom post types to manage different types of content.
  • Widgets: Create custom widgets to add new features to your sidebars.
  • Settings Pages: Add options pages to allow users to configure your plugin.

Example: Adding a Settings Page, Here’s a more advanced example showing how to add a settings page to your plugin.

// Add a menu item
function my_custom_plugin_menu() {
add_options_page(
'My Custom Plugin Settings',
'My Custom Plugin',
'manage_options',
'my-custom-plugin',
'my_custom_plugin_settings_page'
);
}
add_action('admin_menu', 'my_custom_plugin_menu');

// Display the settings page
function my_custom_plugin_settings_page() {
?>
<div class="wrap">
<h1>My Custom Plugin Settings</h1>
<form method="post" action="options.php">
<?php
settings_fields('my_custom_plugin_settings_group');
do_settings_sections('my-custom-plugin');
submit_button();
?>
</form>
</div>
<?php
}

// Register settings
function my_custom_plugin_settings() {
register_setting('my_custom_plugin_settings_group', 'my_custom_option');

add_settings_section(
'my_custom_plugin_settings_section',
'Settings',
null,
'my-custom-plugin'
);

add_settings_field(
'my_custom_option',
'Custom Option',
'my_custom_option_callback',
'my-custom-plugin',
'my_custom_plugin_settings_section'
);
}
add_action('admin_init', 'my_custom_plugin_settings');

// Callback function to display the custom option field
function my_custom_option_callback() {
$option = get_option('my_custom_option');
echo "<input type='text' name='my_custom_option' value='" . esc_attr($option) . "' />";
}

The above code will add the setting page in the admin bar as you can see in the below image.

Screenshot-2024-06-30-130525

Conclusion

Building a WordPress plugin involves creating a new plugin folder, writing your plugin code, adding functionality, and testing thoroughly. Whether you’re developing a simple shortcode or a complex application, the principles are the same.




Reffered: https://www.geeksforgeeks.org


Web Technologies

Related
How to Access Request Parameters in Postman? How to Access Request Parameters in Postman?
How to Create a Custom WordPress Login Page? How to Create a Custom WordPress Login Page?
E-commerce Websites : Shopping Cart E-commerce Websites : Shopping Cart
How to fix &quot;Could not reliably determine the server’s fully qualified domain name&quot; warning in Apache? How to fix &quot;Could not reliably determine the server’s fully qualified domain name&quot; warning in Apache?
E-commerce Websites : Product Detail Page E-commerce Websites : Product Detail Page

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
16