How to create, manage, use and destroy variables and constants in a WordPress plugin

Blog Tutorial

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:

define('PLUGIN_WPE_BASENAME', plugin_basename(__FILE__) );
// PLUGIN_WPE_BASENAME will be 'wp-basic-plugin/wp-basic-plugin.php'
define('PLUGIN_WPE_PATH', plugin_dir_path( __DIR__ ) );
// PLUGIN_WPE_PATH will be  /home/user/www/wp-content/wp-basic-plugin
define('PLUGIN_WPE_URL', plugin_dir_url( __DIR__ ) );
// PLUGIN_WPE_URL will be http://www.website.com/wp-content/wp-basic-plugin

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

add_filter('plugin_action_links_' . PLUGIN_WPE_BASENAME, 'wp_basic_plugin_settings_link');
// this hook creates an action link with name plugin_action_links_wp_basic_plugin based on the function wp_basic_plugin_settings_link
include_once( PLUGIN_WPE_PATH . 'views/admin-form.php' );
// this function includes the file admin-form.php which is in the directory /wp-content/wp-basic-plugin/view/admin-form.php
wp_enqueue_script('wp-plugin-esempio-admin', PLUGIN_WPE_URL . 'js/admin.min.js' );
// this function includes the JS file called admin.min.js which is in the URL http://www.website.com/wp-content/wp-basic-plugin/js/

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:

// copy the value of $phone in wp-basic-plugin-phone-number
update_option( 'wp-basic-plugin-phone-number', $phone );
// copy the value of $prefix in wp-basic-plugin-phone-prefix ecc...
update_option( 'wp-basic-plugin-phone-prefix', $prefix );
update_option( 'wp-basic-plugin-full-phone-number', $fullPhoneNumber );
update_option( 'wp-basic-plugin-title', $title );
update_option( 'wp-basic-plugin-text', 'text in here!!!' );
update_option( 'wp-basic-plugin-button', $button );

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:

// display the value of wp-basic-plugin-phone-prefix inside input text
<input type="text" value="<?php echo get_option('wp-basic-plugin-phone-prefix'); ?>" class="regular-text" />
// display the value of wp-basic-plugin-form-message on screen
echo get_option('wp-basic-plugin-form-message');

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:

// check if this file is ran by WordPress
if (!defined('WP_UNINSTALL_PLUGIN')) {
    die;
}

// destroy the variables below
delete_option( 'wp-basic-plugin-phone-number' );
delete_option( 'wp-basic-plugin-phone-prefix' );
delete_option( 'wp-basic-plugin-full-phone-number' );
delete_option( 'wp-basic-plugin-title' );
delete_option( 'wp-basic-plugin-text' );
delete_option( 'wp-basic-plugin-button' );
delete_option( 'wp-basic-plugin-form-message' );

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.

Click to Leave a Comment

You May Like

How to animate HTML elements and slide them in on mouse scroll using CSS and Javascript Today I want to animate a bunch of HTML elements and create an animation that moves them inside the same parent DIV once the elements are in the viewport, so only when the elements are visible to the users. […]

August 29, 2023
Read More...

This tutorial will show you how to change your website cursor to any CSS shape and change the shape on hover on certain HTML elements. I have seen a lot of tutorials online about changing the HTML cursor to an SVG icon or to a PGN icon. In my case, I would like to change […]

October 15, 2022
Read More...
woocommerce-min-max-checkout-plugin

Woocommerce Min Max Checkout is a plugin that extends your Woocommerce e-commerce in order to give you the possibility to set the minimum or maximum checkout amount or in order to set a minimum or maximum order quantity on the cart or checkout pages. With Woocommerce Min Max Checkout you can also set e customizable […]

February 12, 2022
Read More...