When checking get_defined_vars() on an empty template file my theme loads over 70000 lines of output. Before even including elements on a page all of this is loaded.

  1. Is this correct?
  2. Is there any way to reduce it or remove unnecessary variables?
  3. Do all wp_rewrite rules have to be available for every page load?
  • 1
    If your crazy you can define('SHORTINIT', true); and then load in each bit of WordPress that you want manually. – Wyck Apr 15 '13 at 20:41

Your installation might be … less than optimal.

Using my Mini Theme, no plugins and the following code on the front page right after the opening body tag …

print count( $GLOBALS ) . ' $GLOBALS<br>';
print @count( get_defined_vars(), 1 ) . ' variables<br>';
print count( get_defined_constants( TRUE )['user'] ) . ' constants';

… I get:

9759 variables
  85 constants

Still too much for my taste. Keep in mind many plugin and theme developers forget to keep the global namespace clean. Depending on the quality of the addons you have installed your results might be much higher.

And yes, you need those rewrite rules. Some slugs are translated, and translation can change on each request. So they have to be always available.

| improve this answer | |
  • I get a parse error on that get_defined_constants line. It is triggered at the first [ and $u = get_defined_constants( TRUE ); print count($u['user']); give me an undefined index error. Possibly a PHP version issue? – s_ha_dum Apr 15 '13 at 14:59
  • 1
    Yes, very outdated PHP versions don’t understand that syntax. :P – fuxia Apr 15 '13 at 15:05
  • Yes... 5.3.3... ancient :) I don't know why I get the undefined index error though. – s_ha_dum Apr 15 '13 at 15:09
  • on my install now with only really neccesary plugins: 192 $GLOBALS 13776 variables - still loads of variables – Innate Apr 15 '13 at 15:12
  • 1
    @Innate There are no necessary plugins. :) – fuxia Apr 15 '13 at 15:23
  1. Yes, a lot of information gets loaded. I just put print_r in my header.php on 3.5.1 and got nowhere near 70000 lines though. I got about 2110 and that includes some post body content. Also, print_r or var_dump are formatted representations, remember. Things are not actually stored in memory that way.
  2. I doubt you could remove much this stuff without breaking something or causing other negative effects. You are talking about Core hacks to do it. Some autoloaded items could probably be removed but if you did that you'd get extra database queries when those items are needed. I doubt it would be worth it.
  3. WordPress calculates rewrites on every page load. How is it to find the right page unless all of the rewrite rules are present? Of everything, these would be the most difficult to get rid of, unless you want to use the default permalinks.

Are you actually experiencing a problem? Or is this preemptive?

counts using toscho's code, for the sake of comparison

79  variables
29  constants
| improve this answer | |
  • no this is a problem I am experiencing. There seems to a be a memory leak.. – Innate Apr 15 '13 at 14:43
  • Well, it seems to be something specific to your site/server as I don't get anywhere near 70000 lines of data on a fairly pristine 3.5.1 install. Switch off your plugins and switch to a default theme and see if you still have the issues. – s_ha_dum Apr 15 '13 at 14:46
  • my vanilla install: 163 $GLOBALS 10014 variables - how could this be server related if not theme / plugin related? – Innate Apr 15 '13 at 15:18
  • 1
    It is curious that you and toscho have so many more defined variables than I do. I don't know what to make of that. – s_ha_dum Apr 15 '13 at 20:01
  • 1
    It is possible you have an opcode cache on and it is effecting it? – Wyck Apr 15 '13 at 20:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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