This tutorial shows how to create, use, manage and destroy variables and constants for a Wordpress plugin using PHP.

Today we will look at how to create, manage, use and destroy in a WordPress plugin as well as everything we need to manage a WordPress plugin.

This tutorial is not concerned with creating and managing a WordPress plugin, but to make the content of this article more understandable, and to make the following easier, we first need to define the structure and the base of a basic WordPress plugin. For example, if we take a generic WordPress plugin called wp-basic-plugin, this plugin must have its own folder called /wp-basic-plugin, which must have the same plugin name. Inside this folder there will be a mandatory file called wp-basic-plugin.php which defines the attributes and the nature of the plugin.

Define and manage constants in WordPress

In WordPress (and in any standard system or CMS) it is a good habit to define constants that will be used to access the folders, path and plugin name. This will avoid the use of a static path or static folder which will create bugs when the plugin is moved from site to site. WordPress provides a series of functions to do that. Please see below for these functions and the related description:

  • plugin_basename( string $file ) this function returns the plugin name, this can be used to make the code more standardised instead of using normal strings. In our example this function will return “wp-basic-plugin/wp-basic-plugin.php
  • plugin_dir_path( string $file ) this function returns the filesystem path of the variable $file, in our example (this actually depends on the server configuration) the function returns “/home/user/var/www/wordpress/wp-content/plugins/wp-basic-plugin“. This can be used to import JS files or CSS files etc.
  • plugin_dir_url ( string $file ) this function is similar to the previous one, but it returns the URL of the current plugin.

The following examples show how the previous functions can be used to generate constants:

Let’s see how to use the constants that we have just created above:

Define, manage and destroy variables in a WordPress plugin

Now we will look at how to create variables in WordPress. In this case the operation is very simple, due to the flexibility of PHP, which doesn’t require any type declaration. The only necessary function to do is update_option( string $option, mixed $value, string|bool $autoload = null ). Please click this link to the WordPress documentation for more information. This function is used to create variables and to assign a value to variables inside a Wordress plugin. The variables will be saved in the database without using any query.

We need to keep in mind that any variables in any WordPress plugin must have a unique name in order to avoid the same variable being called accidentally by multiple plugins, so do not use easy names such as A, B, C or index or number1, number2, always use the plugin name as the beginning of the variable name. Here are few examples:

To use the variables created above and get the values, we need to use the function get_option( string $option, mixed $default = false ). Please have a look at this link to read more information on this function:

It’s good practice for any developer to keep the memory of their plugin clean and tidy and to destroy the variables and constants when the plugin is not active or when the plugin has been deleted. To do that we just need to create a file called uninstall.php on the plugin folder /wp-basic-plugin. WordPress will run this file once the plugin is deleted. The file will then destroy the variables previously created using the WordPress function delete_option( string $option ). Here is how to use this function:

The part on the top of the code above “defined(‘WP_UNINSTALL_PLUGIN’)” will check if the file uninstall.php is ran by WordPress, to avoid this file being ran through the browser and accidentally destroying our plugin variables.