Title: WordPress global variables
Last modified: August 19, 2016

---

# WordPress global variables

 *  [fterh](https://wordpress.org/support/users/fterh/)
 * (@fterh)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/wordpress-global-variables/)
 * Hello,
 * Is there a page listing WordPress’s global variables like $comments (which I 
   use in a foreach statement)?

Viewing 11 replies - 1 through 11 (of 11 total)

 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/wordpress-global-variables/#post-1234217)
 * There’s a list here i find quite useful.
    ifacethoughts.net/2006/02/25/wordpress-
   global-variables/
 * You can also print the defined varaiables in PHP by doing something like the 
   following..
 *     ```
       <pre>
       <?php print_r( get_defined_vars() ); ?>
       </pre>
       ```
   
 *  Thread Starter [fterh](https://wordpress.org/support/users/fterh/)
 * (@fterh)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/wordpress-global-variables/#post-1234220)
 * Thanks for the speedy reply. Here’s another question.
 * I want to make my own comments structure like <author>, on <date & time> says:
 * I know I have to use the foreach statement: `<?php foreach($comments as $comment):?
   >` which works perfectly but I’m just curious: In the link you sent me, $comment
   is a global variable but $comments is not. However in the PHP foreach statement
   if I replace $comment with $comment2 it works fine, but not if I replace $comments
   with $comments2.
 * So I’m just kind of confused as to which is the real global variable.
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/wordpress-global-variables/#post-1234232)
 * Print them out, if the variable is global it’ll contain data, if not then it 
   won’t contain anything unless you’ve placed data into the given variable, be 
   it $foo, $bar, or whatever variable..
 *  Thread Starter [fterh](https://wordpress.org/support/users/fterh/)
 * (@fterh)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/wordpress-global-variables/#post-1234236)
 * I tried using the print_r function, I realised that $comment is not a global 
   variable but $comments is. Also they have to be in the loop (I think) because
   I made a new php file in the same directory with the code:
 * >     ```
   >     <?php print_r($comments); ?>
   >     ```
   > 
 * which doesn’t work.
 * Are my deductions correct about $comments and not $comment being the global variable
   and they have to be in the loop?
 * Edit: Also how do you use for foreach loop to display x number of comments on
   each comments page, where x is the number defined under Settings>Discussion
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/wordpress-global-variables/#post-1234240)
 * See here.
    [http://codex.wordpress.org/Template_Tags/wp_list_comments#Comments_Only_With_A_Custom_Comment_Display](http://codex.wordpress.org/Template_Tags/wp_list_comments#Comments_Only_With_A_Custom_Comment_Display)
 * More about variables and their scope.
    [http://uk3.php.net/manual/en/language.variables.scope.php](http://uk3.php.net/manual/en/language.variables.scope.php)
 * As i said before, you can print out all the defined variables with `get_defined_vars()`,
   it’ll be a fairly big list, so be prepared to scroll…
 *  Thread Starter [fterh](https://wordpress.org/support/users/fterh/)
 * (@fterh)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/wordpress-global-variables/#post-1234254)
 * I printed them out but the list was very short.
 * Wait, I printed it on another php file – maybe that’s why. Yep, it worked when
   I used it inside the loop.
 * Last question. I noticed my functions.php file is totally redundant. I deleted
   everything and saved, and my page looks perfectly fine. I pasted back everything
   and refreshed, and there is no change.
 * So should I just remove everything inside functions.php file and make my own 
   function (one only)?
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/wordpress-global-variables/#post-1234258)
 * It’s not a required file, you only need to have one if you wish to run additional
   or custom functions.
 * I tend to do most of my customizations that way, save having a plugin for every
   little thing.
 *  Thread Starter [fterh](https://wordpress.org/support/users/fterh/)
 * (@fterh)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/wordpress-global-variables/#post-1234259)
 * Mm so you mean normally the functions file has a set of functions, and theme 
   designers only call those functions when needed?
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/wordpress-global-variables/#post-1234268)
 * The file does not need to exist, but you may want it if you wish to add functions,
   such as a comments callback.
 * It’s there for when and if you want to add functions, it’s not a requirement.
   
   [http://codex.wordpress.org/Theme_Development#Theme_Functions_File](http://codex.wordpress.org/Theme_Development#Theme_Functions_File)
 * Take a look at the default theme, or pretty much any other theme, there’s lots
   of things you can do.
 *  Thread Starter [fterh](https://wordpress.org/support/users/fterh/)
 * (@fterh)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/wordpress-global-variables/#post-1234424)
 * I did a test. In my comments-template.php:
 * > <?php foreach($comments as $comment) : ?>
   >  <?php wp_list_comments(“callback
   > =listComments”); ?> <?php endforeach; ?>
 * And in the functions.php:
 * > function listComments($commentz) {
   >  echo “
   >     ```
   >     ";
   >     print_r(get_defined_vars());
   >     echo "
   >     ```
   > 
   > “;
   >  echo ““; comment_text(); }
 * How come the output is two identical arrays, yet 2 different comment_text()?
 *  Thread Starter [fterh](https://wordpress.org/support/users/fterh/)
 * (@fterh)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/wordpress-global-variables/#post-1234430)
 * Bump

Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘WordPress global variables’ is closed to new replies.

## Tags

 * [foreach](https://wordpress.org/support/topic-tag/foreach/)
 * [global](https://wordpress.org/support/topic-tag/global/)
 * [variables](https://wordpress.org/support/topic-tag/variables/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 11 replies
 * 2 participants
 * Last reply from: [fterh](https://wordpress.org/support/users/fterh/)
 * Last activity: [16 years, 10 months ago](https://wordpress.org/support/topic/wordpress-global-variables/#post-1234430)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
