WordPress.org

Support

Support » Plugins and Hacks » Is too much output buffering bad

Is too much output buffering bad

  • johnb12
    Member

    @johnb12

    Hi everyone. This question goes out to the PHP gurus.

    My question is, is too much output buffering bad?

    I’m not a huge fan of hacking core so it makes me happy when there is an action I can hook into. Unfortunately, there are many parts of wordpress that aren’t customizable. For example, a client doesn’t want the nickname and website fields on the edit-user page. So, I fire up ob_start and use a regular expression to get rid of those items.

    Now, there are a few other tweaks I need to do to the login page, registration page and can do what I need with ob_start and some regular expressions.

    I would assume that output buffering and regular expressions would create some sort of performance hit. Is that the case? Is the hit negligible? Is it better to just hack core?

    thanks in advance

Viewing 2 replies - 1 through 2 (of 2 total)
  • I use output buffering in a few plugins. The key to reducing its impact is to only have it active on the specific pages you need it on.

    For example, one of my plugins alters the Settings->General page. So I use this code:

    function myplugin_alter_settings_general() {
    	// check to see if we're loading the options-general page
    	global $parent_file;
    	if ( $parent_file != 'options-general.php' ) return;
    
    	// turn on the output buffer and attach the callback
    	ob_start('myplugin_general_callback');
    }
    add_action('admin_head','myplugin_alter_settings_general');
    
    function myplugin_general_callback($data) {
    	// alter $data as I see fit
    	return $data;
    }

    So, the output buffer doesn’t get engaged except on that one page where I need it. The callback function never gets called until it has something that it needs to actually do, so there’s no penalty for the regular expression evaluation.

    Hacking core should be avoided at all costs, because upgrades become extremely difficult when you have to migrate core changes. There’s almost always a way to do it without hacking the core code.

    johnb12
    Member

    @johnb12

    Thanks for the reply Otto42. Luckily I was already targeting specific pages. Didn’t know about the $parent_file variable though.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Is too much output buffering bad’ is closed to new replies.