Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save trajche/5bc8db306988ed587b6a to your computer and use it in GitHub Desktop.

Select an option

Save trajche/5bc8db306988ed587b6a to your computer and use it in GitHub Desktop.

Revisions

  1. @aahan aahan revised this gist Nov 13, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion WordPress Custom Global Variables.md
    Original 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):
    Then, to call the variable elsewhere (another file), you need to manually initialize the function before you can use the variable:

    ``` php
    <?php
  2. @aahan aahan revised this gist Nov 13, 2013. 1 changed file with 40 additions and 1 deletion.
    41 changes: 40 additions & 1 deletion WordPress Custom Global Variables.md
    Original 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

    }
    ```
  3. @aahan aahan revised this gist Nov 13, 2013. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion WordPress Custom Global Variables.md
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,9 @@ function wtnerd_global_vars() {
    add_action( 'parse_query', 'wtnerd_global_vars' );
    ```

    The same can also be done like this:
    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
  4. @aahan aahan revised this gist Nov 13, 2013. 1 changed file with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions WordPress Custom Global Variables.md
    Original 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

    }
    ```
  5. @aahan aahan revised this gist Nov 13, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions WordPress Custom Global Variables.md
    Original 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'),
    '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');
    $wtnerd['tag'] = get_query_var('tag');

    }
    add_action( 'parse_query', 'wtnerd_global_vars' );
  6. @aahan aahan revised this gist Nov 13, 2013. 1 changed file with 7 additions and 9 deletions.
    16 changes: 7 additions & 9 deletions WordPress Custom Global Variables.md
    Original 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'),
    'category' => get_query_var('category_name'),
    'tag' => 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 = array(

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

    );
    $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' );
  7. @aahan aahan revised this gist Nov 13, 2013. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion WordPress Custom Global Variables.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,8 @@
    First create global variables:
    First create global variables (in functions.php or as a mu-plugin):

    ``` php
    <?php

    /*
    * CUSTOM GLOBAL VARIABLES
    */
  8. @aahan aahan revised this gist Nov 13, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions WordPress Custom Global Variables.md
    Original 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
    */
  9. @aahan aahan created this gist Nov 13, 2013.
    41 changes: 41 additions & 0 deletions WordPress Custom Global Variables.md
    Original 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' );
    ```