Which variables are available in your template files?

When you are coding a theme you usually edit standard files such as index.php, single.php, page.php and so on.

In these files you might have noticed that some variables are available even though you have not declared them. Such a variable would be $post for example.

But which other variables are available?

To find out, take a peek in the function load_template in wp-includes/theme.php. In this function alot of different global variables are declared. Normally, theme files are loaded through template-loader.php and to find out exactly which variables are available in that variable scope might be tricky, but the global variables in this function gives us an idea.

  • $posts
  • $post
  • $wp_did_header
  • $wp_did_template_redirect
  • $wp_query
  • $wp_rewrite
  • $wpdb
  • $wp_version
  • $wp
  • $id
  • $comment
  • $user_ID

Also, all the variables in the query_vars array are extracted and made available.

A way to make a custom variable available in your files would be to simply declare it in your theme’s functions.php file.


Web Developer @ Oakwood Creative - www.oakwood.se

Tagged with: , , ,
Posted in Advanced

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


WordPress Quick Tips is a blog supplying great tips about WordPress.

We hope to create a great knowledge resource for WordPress developers as well as serving a reminder for all the forgetful ones.

The blog is created and run by Vincent of Oakwood Creative

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 121 other followers

%d bloggers like this: