WordPress Plugin Tutorial: How to Create a WordPress Plugin

WordPress Plugin Tutorial: How to Create a WordPress Plugin

WordPress Plugin Tutorial: How to Create a WordPress Plugin

WordPress plugin ” is a  set of code that  extends the functionality of WordPress. A plugin can add new features to any part of your website, including the WP-Admin-Panel. You can change the default behavior of WordPress, or remove the functionality / functions completely. Plugins allow you to customize the WordPress site without touch your actual Code .

Because WordPress plugins are standalone, they do not physically alter any of the WordPress core code. They can be copied and installed on any WordPress installation. An alternate (and strongly discouraged) way of making changes to WordPress is to write new functions in the WordPress functions.php file, which is stored in the /wp-includes/ folder, or the functions.php file that is part of a theme. This comes with a number of potential problems.

WordPress themes receive regular updates. And unless you are using WordPress child theme, when functions.php is overwrite by an update your new code will be deleted and you’ll have to write it all over again. If you write a lot of functions and one of them has an error that you are unable to debug, you may have to replace the entire file with the original one, deleting all changes you have made. If your functions are removed from the file, your website may be covered in php errors.

Plugins are never automatically overwritten or deleted when installing WordPress updates. If your plugin has coding errors, you can usually just deactivate it in the Admin Control Panel while you fix it. If your plugin has a serious error, WordPress will sometimes automatically deactivate it for you, allowing your site to continue functioning.

Before Start to Create Plugin We need to have knowledge about WordPress Hooks

 

What are Hooks?

WordPress Plugins interact with core code using hooks. There are two different types of hooks.

  1. Action hooks (To add/remove functions)
  2. Filter hooks (To modify data that is produced by functions

Using the action hooks provided by WordPress, you can add your own functions to the list of actions that run when any action hook is called, and you can also remove existing functions from any action hook.

Action hooks dictate when the actions are called. Before the closing </head> tag of any page, the wp_head() action hook is called, and the actions that are hooked to wp_head() are run.

Action hooks are contextual – some are called on frontend of your website, others are only called on Admin Control Panel, and so on. A full list of actions hooks and the context in which they are called can be found on the WordPress Plugin API/Action Reference page.

 Action Hook Using add_action()

To add a function to any action hook, your plugin must call the WordPress function named add_action(), with at least two parameters.


/ / Hook to the 'init' action, which is called after WordPress is finished loading the core code.

add_action( 'init', 'add_Cookie' );

// Set a cookie with the current time of day

setcookie("last_visit_time", date("r"), time()+60*60*24*30, "/");

}

Some Explanations –

  • The first parameter is the name of the action hook 
  • The second parameter is the name of the function 
  • The third parameter  is the priority of the function
  • The fourth parameter is the number of arguments,

Example plugin code to display text after the footer of every page

<?php
/*
Plugin Name: Add Text To Footer
*/
 
// Hook the 'wp_footer' action hook, add the function named 'mfp_Add_Text' to it
add_action("wp_footer", "mfp_Add_Text");
 
// Define 'mfp_Add_Text'
function mfp_Add_Text()
{
  echo "

After the footer is loaded, my text is added!

";
}

Removing Functions From an Action Hook Using remove_action()


/ Hook the 'init' action, which is called after WordPress is finished loading the core code, add the function 'remove_My_Meta_Tags'
add_action( 'init', 'remove_My_Meta_Tags' );

// Remove the 'add_My_Meta_Tags' function from the wp_head action hook
function remove_My_Meta_Tags()
{
remove_action( 'wp_head', 'add_My_Meta_Tags');
}

Step 1 – Create Plugin Folder

The first step to creating your WordPress plugin is making a folder at ( /wp-content/plugins/my-first-plugin) to store all your files. The folder you create needs a unique and descriptive name to ensure it doesn’t clash with any other plugin.

Step 2 – Creating the First File

In your my-first-plugin folder, create a new php file named my-first-plugin.php. You need to create same file name. The first comment you must add to my-first-plugin.php is the name of your plugin.

Set your plugin name Plugin Name: My First Plugin 


<?php
/*
Plugin Name: My First Plugin
*/

 

Save the file and upload it to the my-first-plugin folder. You will see plugin would see on plugins you can activate and deactivate your plugin . If you want to show your plugin description then please write this in your comment file.

<?php 
/*
Plugin Name: My First Plugin
Description: This is my first plugin! It makes a new admin menu link!
Author: Your Name
*/

<p><img class=”alignnone wp-image-172 size-full” src=”https://www.misterwebhub.com/wp-content/uploads/2019/07/My-First-Plugin-at-admin-area.jpg” alt=”” width=”900″ height=”321″ />

Step 3 – Writing Your Plugin’s Functions

In your plugin’s main folder, create a new folder named includes. Any file that is ‘included’ by another file will go in this folder. Create a new php file in the includes folder and save it as mfp-functions.php. Give it the opening <?php tag on the first line. This new file is where all your plugin’s functions will be stored.

Now go back to my-first-plugin.php in your plugin’s main folder. We need to make it include the functions.php file so we can actually use the new functions. Since this is the main plugin file, including functions.php here makes the functions available to any other file in your plugin. Use require_once to ensure the plugin only works if the functions file is available. The easiest way to include files from your plugin folder is by using the WordPress function plugin_dir_path(__FILE__), which gives the full path to the directory where our plugin is stored, then use a (period) to append the name of the subfolder we created earlier (includes), followed by the name of the file we created (functions.php).

Edit my-first-plugin.php as shown below then save and upload it once again, overwriting the previous version when asked.


<?php
/*
Plugin Name: My First Plugin
Description: This is my first plugin! It makes a new admin menu link!
Author: Your Name
*/

// Include mfp-functions.php, use require_once to stop the script if mfp-functions.php is not found
require_once plugin_dir_path(__FILE__) . 'includes/functions.php';

 

In Short Summary

writing a new function involves the following steps:

  • Write a comment describing the function
  • Name the function
  • Write the function
<?php
/*
* Add my new menu to the Admin Control Panel
*/

// Add a new top level menu link to the ACP
function mfp_Add_My_Admin_Link()
{
// My code goes here
}

Add Custom Page in WordPress Admin

Inside our function, we need to use  WordPress function add_menu_page() to give our menu a name, a title and dictate who is allowed to see it. Then we tell it what to display when you go to the page. You can also give the menu link an icon and set its position in the admin control panel navigation menu – these are both optional, so we will leave them out for this tutorial. The default cog icon will be shown on the link to our page. Our link will appear at the bottom of the admin control panel navigation menu. All this information is entered as parameters of add_menu_page().

Edit functions.php, remove // My code goes here, replace it with add_menu_page() and give it parameters as shown below:

 


<?php
/*
* Add my new menu to the Admin Control Panel
*/

// Add a new top level menu link to the ACP
function mfp_Add_My_Admin_Link()
{
add_menu_page(
'My First Page', // Title of the page
'My First Plugin', // Text to show on the menu link
'manage_options', // Capability requirement to see the link
'includes/first-acp-page.php' // The 'slug' - file to display when clicking the link
);
}

 

To make this function actually run, we need to use the WordPress function named add_action()with two parameters, as described in the ‘Adding Functions To An Action Hook’ section of this tutorial. You may want to read over that section again before continuing.

  • The first parameter is the action hook you want to target. In our case the action hook is admin_menu – this means our function is run when the Admin Menu is being generated.
  • The second parameter is just the name of the function to run. The function we wrote is named mfp_Add_My_Admin_Link. Note that the parentheses are NOT used here. Remember that php evaluates the entire script before running it, allowing you to use add_action() before defining the function named in parameter 2.

Complete Code Which Run – :


<?php
/*
* Add my new menu to the Admin Control Panel
*/

// Hook the 'admin_menu' action hook, run the function named 'mfp_Add_My_Admin_Link()'
add_action( 'admin_menu', 'mfp_Add_My_Admin_Link' );

// Add a new top level menu link to the ACP
function mfp_Add_My_Admin_Link()
{
add_menu_page(
'My First Page', // Title of the page
'My First Plugin', // Text to show on the menu link
'manage_options', // Capability requirement to see the link
'includes/first-acp-page.php' // The 'slug' - file to display when clicking the link
);
}

Upload functions.php to the includes folder and overwrite the old one.

Step 4 – Creating The New Custom Admin Page

Now we can create the page to be displayed when you click on your admin control panel link. Go back to the includes subfolder and create a new php file named first-acp-page.php. php opening tags aren’t required in this file as we are only using HTML. Write the HTML below, and upload the file.


<div class="wrap">
Hello! This is my first plugin</div>;

Congrats You have successfully create your first WordPress Plugin Which create a Custom Menu link with custom page at admin area.

If you face any problem please deactivate your plugin then activate it again .
Feel Free to ask for technical issues  at www.misterwebhub.com or misterwebhub@gmail.com

Thanks Have A Good Code  !!

Leave a Reply

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