Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

is there a file where wordpress defines it's $GLOBALS?

i'm just curious as to what wordpress uses it for and for what purpose.

That's all!

share|improve this question

3 Answers 3

up vote 5 down vote accepted

I'm not sure if all of these are WordPress globals, but i did a quick grep type search of the WordPress files and tried to extract all the globals i could..

This is a list i compiled for you, that may not be perfect, but should *hopefully* represent alot of the $GLOBALS keys that WordPress uses, it won't account for globised variables that aren't explicitly defined as $GLOBAL, but still have global scope.

$GLOBALS['_menu_item_sort_prop']
$GLOBALS['_wp_sidebars_widgets']
$GLOBALS['blog_id']
$GLOBALS['body_id']
$GLOBALS['comment']
$GLOBALS['comment_depth']
$GLOBALS['content_width']
$GLOBALS['current_site']
$GLOBALS['current_user']
$GLOBALS['custom_background']
$GLOBALS['custom_image_header']
$GLOBALS['debug_bar']
$GLOBALS['editor_styles']
$GLOBALS['is_winIE']
$GLOBALS['link']
$GLOBALS['login_grace_period']
$GLOBALS['month']
$GLOBALS['month_abbrev']
$GLOBALS['more']
$GLOBALS['post']
$GLOBALS['post_type']
$GLOBALS['posts']
$GLOBALS['query_string']
$GLOBALS['request']
$GLOBALS['single']
$GLOBALS['submenu']
$GLOBALS['tab']
$GLOBALS['type']
$GLOBALS['weekday']
$GLOBALS['weekday_abbrev']
$GLOBALS['weekday_initial']
$GLOBALS['wp_admin_bar']
$GLOBALS['wp_filter']
$GLOBALS['wp_object_cache']
$GLOBALS['wp_post_types']
$GLOBALS['wp_query']
$GLOBALS['wp_styles']
$GLOBALS['wp_taxonomies']
$GLOBALS['wp_the_query']
$GLOBALS['wp_version']

If you wanted to get a better idea of everything inside the global array you could run something like the following to get a print out, because the above approach was obviously flawed(globals are defined in more than one way).

add_action( 'shutdown', 'print_them_globals' );

function print_them_globals() {

    ksort( $GLOBALS );
    echo '<ol>';
    echo '<li>'. implode( '</li><li>', array_keys( $GLOBALS ) ) . '</li>';
    echo '</ol>';
}

Which should give you a more comprehensive list of variables in the global scope..

Hope that's helpful.. :)

share|improve this answer
    
++++++ wp_filesystem, wp_rewrite, wp_registered_widgets, wp_registered_sidebars, wpdb, current_screen, pagenow, is_IE, is_gecko, is_opera, is_iphone etc..., authordata, wp_roles, wp_scripts, _wp_using_ext_object_cache.. There are probably hundreds out there. Code is Poetry :) –  onetrickpony Jul 29 '11 at 15:47
    
Well sure, there are plenty more than end up in the global scope, but they aren't defined in the same way, so impossible to factor into my regex pattern. –  t31os Jul 29 '11 at 16:05
2  
Added a function to my answer that will give a more comprehensive list of globals... :) –  t31os Jul 29 '11 at 16:13

Unfortunately, no.

Globals definitions are scattered throught the codebase.

There's no documentation for most of them, either.

share|improve this answer
    
I think it should also be noted that if you declare ANY variable in the global scope it is automatically added to the $GLOBALS array without ever typing $GLOBALS[, so a grep search wouldn't possibly return every global variable in WordPress. –  stevendesu Jul 29 '11 at 13:59
    
@steven_desu: I think you meant to post that to t31os' answer. –  scribu Jul 29 '11 at 14:30
    
It wasn't grep, technically speaking it was a regex pattern match across all files in the WordPress installation directory using Notepad++, my search went along the lines $GLOBALS([^, ]+)([, ]+) ... which gave me a bundle of results, which i then copy and pasted, and stripped away all the irrelevant data. –  t31os Jul 29 '11 at 15:47

'$GLOBALS' is an associative array containing references to all variables which are currently defined in the global scope. This is a PHP language tool.

Global variables can be defined simply by creating a new item in the $GLOBALS array like this:

$GLOBALS['foo'] = 'foo content';

WordPress Globals are used to share data across files. They are not defined in any specific place but you can find some of the most important ones here: http://codex.wordpress.org/Global_Variables

PHP makes it even easier to use $GLOBALS by allowing you to access the items by simply declaring it using the globals key word.

global $foo;
$foo = 'foo new content';

is the same as:

$GLOBALS['foo'] = 'foo new content';

Please note, If you didn't define $foo as global it will not be linked to the global variable.

Some further reading on this: http://www.php.net/manual/en/reserved.variables.globals.php

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.