Skip to content

Instantly share code, notes, and snippets.

@aahan
Last active March 28, 2024 19:26
Show Gist options
  • Star 41 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save aahan/7444046 to your computer and use it in GitHub Desktop.
Save aahan/7444046 to your computer and use it in GitHub Desktop.
Creating and using custom global variables in wordpress.

First create global variables (in functions.php or as a mu-plugin):

<?php

/*
 * CUSTOM GLOBAL VARIABLES
 */
function wtnerd_global_vars() {

	global $wtnerd;
	$wtnerd = array(

		'edition'  => get_query_var('category_name'),
		'channel'  => get_query_var('channel'),
		'tag'      => get_query_var('tag'),

	);

}
add_action( 'parse_query', 'wtnerd_global_vars' );

Why are we using an associative array variable $wtnerd? Because global variables need to be unique, and by keeping $wtnerd unique we can have simpler names for all the variables in its array.

By the way, the same can also be done like this:

<?php

/*
 * CUSTOM GLOBAL VARIABLES
 */
function wtnerd_global_vars() {

	global $wtnerd;
	$wtnerd['edition'] = get_query_var('category_name');
	$wtnerd['channel'] = get_query_var('channel');
	$wtnerd['tag']     = get_query_var('tag');

}
add_action( 'parse_query', 'wtnerd_global_vars' );

Then use $GLOBALS[]; to call the variable elsewhere (another file):

<?php

if( $GLOBALS['wtnerd']['edition'] == uk ) {

	// Do something

}

If the function in which you are defining the global variables is not hooked into a filter or action, e.g. add_action( 'parse_query', 'wtnerd_global_vars' ); as we are doing above, then you should do it as shown below.

In functions.php or mu-plugin:

<?php

/*
 * CUSTOM GLOBAL VARIABLES
 */
function wtnerd_global_vars() {

	global $wtnerd;
	$wtnerd = array(

		'edition'  => get_query_var('category_name'),
		'channel'  => get_query_var('channel'),
		'tag'      => get_query_var('tag'),

	);

}

Then, to call the variable elsewhere (another file), you need to manually initialize the function before you can use the variable:

<?php

wtnerd_global_vars();
if( $GLOBALS['wtnerd']['edition'] == uk ) {

	// Do something

}
@sododesign
Copy link

Hi @aahan may I ask you one thing? I use ACF as plugins included in my theme to create custom fields as Page Options. Since we do not want to call them every time one by one, I would ask you consider this as my approach starting from your example? You find it correct?

<?php 
/* * GLOBAL CUSTOM FIELD * */
function wedding_gloabal_cf() {
    // DEFINE GALLERY
    global $weddingopt;
    $weddingopt = array(
        // GALLERY ACF
        'images_slide'      => get_field('gallery_wedding','option'),
        // DEFINE GROOM & BRIDE
        'name_groom'        => get_field('name_groom','option'),
        'surname_groom'     => get_field('surname_groom','option'),
        'name_bride'        => get_field('name_bride','option'),
        'surname_bride'     => get_field('surname_bride','option'),
        //FULL NAME COUPLE
        'groom_full'        => $name_groom . ' ' . $surname_groom,
        'bride_full'        => $name_bride . ' ' . $surname_bride,
        //SOCIAL GROOM
        'facebook_groom'    => get_field('facebook_groom','option'),
        'twitter_groom'     => get_field('twitter_groom','option'),
        'instagram_groom'   => get_field('instagram_groom','option'),
        //SOCIAL BRIDE
        'facebook_bride'    => get_field('facebook_bride','option'),
        'twitter_bride'     => get_field('twitter_bride','option'),
        'instagram_bride'   => get_field('instagram_bride','option'),
        //EXAMPLE DATA
        'mike'              => __('Mike','italian-wedding-day'),
        'mike_full'         => __('Mike Callagher','italian-wedding-day'),
        'lisa'              => __('Lisa','italian-wedding-day'),
        'lisa_full'         => __('Lisa Connor','italian-wedding-day'),
        // DEFINE PHOTO GROOM & BRIDE
        'photo_groom'       => get_field('photo_groom','option'),
        'photo_bride'       => get_field('photo_bride','option'),
        // DEFINE BIOGRAPHY
        'biography_groom'   => get_field('biography_groom','option'),
        'biography_bride'   => get_field('biography_bride','option'),
    );
}
add_action( 'init', 'wedding_gloabal_cf' );

Thank you very much!
Davide

@jeffyarias
Copy link

hello there do you guys know how can I grab a variable from a plugin form and make a session to add the data to another form

Copy link

ghost commented Apr 15, 2017

Thanks. Worth noting is the global must be defined before it's referenced.

@kevin25
Copy link

kevin25 commented Sep 11, 2017

i want to set the attribute like Style as global Attribute. How can I do it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment