Using Global PHP Variables
Written By mark@uipress.co
Last updated 11 months ago
Overview
SnipVault allows you to create and manage code snippets for your WordPress site. One powerful feature is the ability to define global PHP variables that can be accessed across all your snippets, making your code more modular and easier to maintain.
Setting Up Global PHP Variables
Accessing the Global Variables Settings
Navigate to your SnipVault dashboard
Click on the Settings option in the left sidebar
Select the PHP Global Variables section

Adding New Global Variables
In the PHP Global Variables section, you'll see a form with two fields:
Variable name: Enter the name of your global variable
Variable value: Enter the value you want to assign to this variable
Click the Add button to save your new global variable
Managing Existing Variables
The interface displays a table with all your defined global variables:
Variable Name: The name of your global variable
Value: The current value assigned to the variable
Action: Contains a delete button (ποΈ) to remove unwanted variables
Using Global Variables in Your Snippets
Once defined, your global variables will be available across all PHP snippets. Here's how to use them:
Basic Usage
In any PHP snippet, you can directly reference your global variables using their defined names:
// If you defined a variable named WELCOME_MESSAGE
$notice_message = WELCOME_MESSAGE; Example Usage Scenario
As shown in the screenshots, you might define global variables like:
WELCOME_MESSAGEwith a value of "Hello world!"SECRET_KEYwith a secure key value
Then in your snippets, you can use these variables for consistent messaging or secure operations:
function custom_admin_notice() {
$notice_type = "notice-info";
// use a custom global var
$notice_message = WELCOME_MESSAGE;
// Display the notice
echo '<div class="' . $notice_type . '"><p>' . $notice_message . '</p></div>'; } Best Practices
Use Descriptive Names: Choose clear, uppercase variable names (like
WELCOME_MESSAGE) to distinguish them from local variablesSecurity Considerations:
Don't store sensitive information in global variables if not necessary
Naming Conventions:
Use uppercase for constant-like global variables
Use underscores to separate words (e.g.,
WELCOME_MESSAGE,SECRET_KEY)
Documentation: Add comments in your snippets to identify where global variables are being used
Troubleshooting
Variable Not Available: Ensure the variable was properly saved in the settings
Case Sensitivity: Remember that PHP variables are case-sensitive;
WELCOME_MESSAGEis different fromwelcome_messageScope Issues: Global variables are available in all script execution contexts but may need proper global scope declaration in certain functions
Technical Notes
The plugin automatically makes these variables available globally in all PHP script execution contexts
No need to use
globalkeyword or$GLOBALSarray in most casesVariables are defined at runtime before your snippets execute