pcbway




Add Basic Functions in WordPress Plugin, wordpress plugin creations, create wordpress plugin, add functions in wordpress pluginHello friends, I hope you all are doing great. In today’s tutorial, I am going to show you How to Add Basic Function in WordPress Plugin. In the previous tutorial, we have seen How to Create a Simple WordPress Plugin, and today we are gonna add some basic functions in it, which are kind of compulsory. You may not find them useful right now but they will come in handy when we will work on complex plugins.

It’s the second tutorial in WordPress plugin creation series. If you got into any trouble then ask in comments and I will try my best to resolve them all. So, let’s get started with these basic functions and add then in our newly created WordPress plugin:

Add Basic Functions in WordPress Plugin

  • As we have already created the class for our wordpress plugin so now its time to add some functions in it.
  • The first function we are going to add is the construct function.
  • Construct is the default function, which is called by the class on initialization.
  • So in simple words, when our plugin will start then construct function will be automatically called.
  • Here’s our code for the construct function:

  • We have already seen How to initialize the class NamePluginTEP.
  • Inside this class, I have created a new variable named pluginName.
  • After that I have added a construct function and this function will be called by default, it’s simple object oriented programming.
  • Within this construct function, I have used plugin_basename, which will give us the name of our plugin.
  • I have saved the name in our variable pluginName.
  • We are not gonna use this variable rite now but soon in the coming tutorials.
  • The next two functions we are gonna add are Activate and Deactivate.
  • These functions will be executed, when you will activate or deactivate your plugin in the WordPress plugin section.
  • Here’s the code for these two functions:

  • As you can see in the above code that we have added both of these functions activate and deactivate in our Main Plugin class.
  • Now in order to call these functions, we have to use built-in WordPress hooks.
  • We have used two hooks, which are:
    • register_activation_hook.
    • register_deactivation_hook.
  • These registration hooks will check the __File__ and then will find the activate/deactivate function in our class.
  • Instead of adding all codes in a single PHP file, it’s always a better approach to create separate PHP files for each method or routine.
  • So, now we are gonna create two more PHP files for Activation and Deactivation and then will call them in our main PHP file.




Creating Activation & Deactivation PHP Files

  • Till now, we have just created one Folder named NamePluginTEP and then we have created a php file of the same name, which is actually name of our plugin.
  • So, now in this Main Plugin Folder, create a new folder and rename it to inc (short for include).
  • In this inc folder, you have to create a two php files, named as:
    • Activate.php
    • Deactivate.php
  • In the Activate.php file, place the below code:

  • In the Deactivate.php file, place the below code:

  • In both of these files, we have created a new class and then added a public static function.
  • We have just used the method flush_rewrite_rules() , which is kind of a refresh and I am not adding any more codes in it.
  • Don’t worry in coming tutorials, these all are gonna come in handy, rite now we are just building the foundation.
  • Now we have to call these functions in our Main PHP File.
  • So our main PHP class will be something like that:

  • We have just added two lines in both of our activate and deactivate functions.
  • This method require_once plugin_dir_path is used to include the Activate.php file for once and we have passed its path.
  • In the second line, we have simply called our function Activate::activatePlugin() , static functions can easily be called that way.
  • Now activate or deactivate your plugin in WordPress Plugin section but it won’t do anything as still we haven’t added any remarkable code in it, we are just building the foundation. 😛
  • But the good thing is it’s not crashing or giving any error, that’s a good sign. 🙂
  • Here’s a screenshot of our WordPress plugin:

Add Basic Functions in WordPress Plugin, wordpress plugin creations, create wordpress plugin, add functions in wordpress plugin

  • I have activated the plugin but still its not doing anything but it will do something soon. 😛

So, that was all for today. I hope you will easily Add these Basic Functions in WordPress Plugin. In the coming tutorial, we are gonna have a look at How to Enqueue Scripts Files in WordPress Plugin. Till then take care and have fun !!! 🙂

Leave a Reply

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

Post comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.