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

  1. Navigate to your SnipVault dashboard

  2. Click on the Settings option in the left sidebar

  3. Select the PHP Global Variables section

Adding New Global Variables

  1. 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

  2. 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_MESSAGE with a value of "Hello world!"

  • SECRET_KEY with 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

  1. Use Descriptive Names: Choose clear, uppercase variable names (like WELCOME_MESSAGE) to distinguish them from local variables

  2. Security Considerations:

    • Don't store sensitive information in global variables if not necessary

  3. Naming Conventions:

    • Use uppercase for constant-like global variables

    • Use underscores to separate words (e.g., WELCOME_MESSAGE, SECRET_KEY)

  4. 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_MESSAGE is different from welcome_message

  • Scope 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 global keyword or $GLOBALS array in most cases

  • Variables are defined at runtime before your snippets execute