Forked from aahan/WordPress Custom Global Variables.md
Created
October 27, 2015 05:27
-
-
Save trajche/5bc8db306988ed587b6a to your computer and use it in GitHub Desktop.
Revisions
-
aahan revised this gist
Nov 13, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -80,7 +80,7 @@ function wtnerd_global_vars() { } ``` Then, to call the variable elsewhere (another file), you need to manually initialize the function before you can use the variable: ``` php <?php -
aahan revised this gist
Nov 13, 2013 . 1 changed file with 40 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -52,4 +52,43 @@ 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 <?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): ``` php <?php wtnerd_global_vars(); if( $GLOBALS['wtnerd']['edition'] == uk ) { // Do something } ``` -
aahan revised this gist
Nov 13, 2013 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -21,7 +21,9 @@ function wtnerd_global_vars() { 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 <?php -
aahan revised this gist
Nov 13, 2013 . 1 changed file with 12 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -38,4 +38,16 @@ function wtnerd_global_vars() { } add_action( 'parse_query', 'wtnerd_global_vars' ); ``` Then use `$GLOBALS[];` to call the variable elsewhere (another file): ``` php <?php if( $GLOBALS['wtnerd']['edition'] == uk ) { // Do something } ``` -
aahan revised this gist
Nov 13, 2013 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -12,7 +12,7 @@ function wtnerd_global_vars() { $wtnerd = array( 'edition' => get_query_var('category_name'), 'channel' => get_query_var('channel'), 'tag' => get_query_var('tag'), ); @@ -34,7 +34,7 @@ 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' ); -
aahan revised this gist
Nov 13, 2013 . 1 changed file with 7 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -12,8 +12,8 @@ function wtnerd_global_vars() { $wtnerd = array( 'edition' => get_query_var('category_name'), 'channel' => get_query_var('channel'), 'tag' => get_query_var('tag'), ); @@ -24,19 +24,17 @@ add_action( 'parse_query', 'wtnerd_global_vars' ); The same can also be done like this: ``` php <?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' ); -
aahan revised this gist
Nov 13, 2013 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,8 @@ First create global variables (in functions.php or as a mu-plugin): ``` php <?php /* * CUSTOM GLOBAL VARIABLES */ -
aahan revised this gist
Nov 13, 2013 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ First create global variables: ``` php /* * CUSTOM GLOBAL VARIABLES */ @@ -21,7 +21,7 @@ add_action( 'parse_query', 'wtnerd_global_vars' ); The same can also be done like this: ``` php /* * CUSTOM GLOBAL VARIABLES */ -
aahan created this gist
Nov 13, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,41 @@ First create global variables: ``` /* * CUSTOM GLOBAL VARIABLES */ function wtnerd_global_vars() { global $wtnerd; $wtnerd = array( 'edition' => get_query_var('category_name'), 'category' => get_query_var('category_name'), 'tag' => get_query_var('category_name'), ); } add_action( 'parse_query', 'wtnerd_global_vars' ); ``` The same can also be done like this: ``` /* * CUSTOM GLOBAL VARIABLES */ function wtnerd_global_vars() { global $wtnerd; $wtnerd = array( 'edition' => get_query_var('category_name'), 'category' => get_query_var('category_name'), 'tag' => get_query_var('category_name'), ); } add_action( 'parse_query', 'wtnerd_global_vars' ); ```