How to create WordPress custom plugin, WordPress Plugin Tutorial

How to create WordPress custom plugin

Sending Emails using PHP
April 28, 2018
SQL Joins Tutorial
May 24, 2018

In this WordPress plugin tutorial, you’ll learn how to create a WordPress custom plugin from scratch. The most important reason to create a custom plugin is that it allows you to separate your own code from the WordPress core code.
You can write code for alter existing functionality provided by theme or wordpress plugin or for develop new functionality for your website.

You need to follow below steps to create a work your own custom plugin.

1. Creating Our First Plugin

First, create a folder in plugins directory and then create a single php file in that folder.

Here in this article I am creating plugin with name “myplugin”. So first I’ll create a folder with name myplugin in wp-content/plugins.
Inside this new folder, create a file with name myplugin.php. Open the file in a php editor, and paste the following code in this file:

<?php
function myplugin_custom_scripts()
{
    wp_register_style('custom_css', plugins_url('style.css',__FILE__ ));
    wp_enqueue_style('custom_css');
    wp_register_script( 'custom_js', plugins_url('my_script.js',__FILE__ ));
    wp_enqueue_script('custom_js');
}
add_action( 'wp_enqueue_scripts', 'myplugin_custom_scripts' );
?>

Here in this code snippets only Plugin Name is required, other information other informations are optional.

Now you can activate plugin from WordPress admin by going to plugins page. Now you can write your PHP code in php file.

2. Structuring PlugIns

For manage complex functionality, we can create multiple folders and files.

If your plugin focuses on one main class, put that class in the main plugin file, and then add one or more separate files for other functionality.

You can also include custom JS and CSS files in the plugin main file to enhance the functionality of custom plugin. For include custom JS and CSS you can use below WordPress function.

0 0 votes
Article Rating
Manoj Patial
Manoj Patial
I am Manoj Patial, a website developer from India, I have more than 10 years of experience in website development. I developed more than 200+ website using Drupal, WordPress, PHP, Codeigniter, shopify, HubSpot and other CMS/PHP frameworks.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Pin It on Pinterest

0
Would love your thoughts, please comment.x
()
x