Add Basic Functions in WordPress Plugin

Hello 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:
/* Main class code starts here */

if ( !class_exists( 'NamePluginTEP' ) )
{ 

	class NamePluginTEP
	{

		public $pluginName;

		function __construct()
		{

			$this -> pluginName = plugin_basename(__FILE__);

		}
	
	}

	$namePluginTEP = new NamePluginTEP();

}

/* Main class code ends here */
  • 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:
/* Main class code starts here */

if ( !class_exists( 'NamePluginTEP' ) )
{ 

	class NamePluginTEP
	{

		public $pluginName;

		function __construct(){
			$this -> pluginName = plugin_basename(__FILE__);
		}

		function activate(){
		}

		function deactivate(){
		}

	}

	$namePluginTEP = new NamePluginTEP();

}

/* Main class code ends here */

register_activation_hook( __File__, array( $namePluginTEP, 'activate') );

register_deactivation_hook( __File__, array( $namePluginTEP, 'deactivate') );

  • 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:
<?php

/*
* @package NamePluginTEP
*/

class Activate
{
	public static function activatePlugin()
	{
		flush_rewrite_rules();
	}
}
  • In the Deactivate.php file, place the below code:
<?php

/*
* @package NamePluginTEP
*/

class Deactivate
{
	public static function deactivatePlugin()
	{
		flush_rewrite_rules();
	}
}
  • 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:
<?php

/**
 *
 * @package NamePluginTEP
 *
 */

/*

Plugin Name: NamePluginTEP
Plugin URI: https://www.theengineeringprojects.com/
Author: Syed Zain Nasir
Author URI: https://twitter.com/syedzainnasir
Description: It's our First Simple WordPress Plugin.
Version: 1.0.0
License: GPLv2

*/

/*

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.

*/

defined( 'ABSPATH' ) or die( 'We are having an error.' );

/* Main class code starts here */

if ( !class_exists( 'NamePluginTEP' ) )
{

	class NamePluginTEP
	{

		public $pluginName;
		function __construct(){
			$this -> pluginName = plugin_basename(__FILE__);
		}
		
		function activate()
		{

			require_once plugin_dir_path( __FILE__ ).'inc/Activate.php';
			Activate::activatePlugin();

		}

		function deactivate()
		{

			require_once plugin_dir_path( __FILE__ ).'inc/Deactivate.php';
			Deactivate::deactivatePlugin();

		}

	}

	$namePluginTEP = new NamePluginTEP();
}

/* Main class code ends here */

register_activation_hook( __File__, array( $namePluginTEP, 'activate') );

register_deactivation_hook( __File__, array( $namePluginTEP, 'deactivate') );
  • 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. :P
  • 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:
  • I have activated the plugin but still its not doing anything but it will do something soon. :P
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 !!! :)
Syed Zain Nasir

I am Syed Zain Nasir, the founder of <a href=https://www.TheEngineeringProjects.com/>The Engineering Projects</a> (TEP). I am a programmer since 2009 before that I just search things, make small projects and now I am sharing my knowledge through this platform.I also work as a freelancer and did many projects related to programming and electrical circuitry. <a href=https://plus.google.com/+SyedZainNasir/>My Google Profile+</a>

Share
Published by
Syed Zain Nasir