WordPress.org

Ready to get started?Download WordPress

Forums

Is too much output buffering bad (3 posts)

  1. johnb12
    Member
    Posted 6 years ago #

    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

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    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.

  3. johnb12
    Member
    Posted 6 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic