• Resolved Rajarshi Bose

    (@truthsearcher83)


    I was reading the code of a plugin which adds a facebook link in the footer of each posts.The plugin has a settings page. The callback for the options page is given below :

    // Create Options Page Content
    function ffl_options_content(){
    
    	// Init Options Global
    	global $ffl_options;
    
    	ob_start(); ?>
    		<div class="wrap">
    			<h2><?php _e('Facebook Footer Link Settings', 'ffl_domain'); ?></h2>
    			<p><?php _e('Settings for the Facebook Footer Link plugin', 'ffl_domain'); ?></p>
    			<form method="post" action="options.php">
    				<?php settings_fields('ffl_settings_group'); ?>
    				<table class="form-table">
    					<tbody>
    						<tr>
    							<th scope="row"><label for="ffl_settings[enable]"><?php _e('Enable','ffl_domain'); ?></label></th>
    							<td><input name="ffl_settings[enable]" type="checkbox" id="ffl_settings[enable]" value="1" <?php checked('1', $ffl_options['enable']); ?></td>
    						</tr>
    						<tr>
    							<th scope="row"><label for="ffl_settings[facebook_url]"><?php _e('Facebook Profile URL','ffl_domain'); ?></label></th>
    							<td><input name="ffl_settings[facebook_url]" type="text" id="ffl_settings[facebook_url]" value="<?php echo $ffl_options['facebook_url']; ?>" class="regular-text">
    								<p class="description"><?php _e('Enter your Facebook profile URL', 'ffl_domain'); ?></p></td>
    						</tr>
    
    						<tr>
    							<th scope="row"><label for="ffl_settings[link_color]"><?php _e('Link Color','ffl_domain'); ?></label></th>
    							<td><input name="ffl_settings[link_color]" type="text" id="ffl_settings[link_color]" value="<?php echo $ffl_options['link_color']; ?>" class="regular-text">
    								<p class="description"><?php _e('Enter a color or HEX value with a #', 'ffl_domain'); ?></p></td>
    						</tr>
    
    						<tr>
    							<th scope="row"><label for="ffl_settings[show_in_feed]"><?php _e('Show In Posts Feed','ffl_domain'); ?></label></th>
    							<td><input name="ffl_settings[show_in_feed]" type="checkbox" id="ffl_settings[show_in_feed]" value="1" <?php checked('1', $ffl_options['show_in_feed']); ?></td>
    						</tr>
    					</tbody>
    				</table>
    				<p class="submit"><input type="submit" name="submit" id="submit" class="button button-primary" value="<?php _e('Save Changes', 'ffl_domain'); ?>"</p>
    			</form>
    		</div>
    	<?php
    	echo ob_get_clean();
    }

    I am curious about the usage of ob_start() while drawing the option page . I have read up on ob_start() and how it works and understand it improves performance by buffering the whole HTML and outputting it together . My question is , is it recommended to use ob_start() while displaying things in the front end ? Like I end up using shortcodes a lot and most of the time there is much more HTML code in the front end that in the options page . I have never used ob_start() when I code shortocdes , do I start using them for better performance ?

Viewing 2 replies - 1 through 2 (of 2 total)
  • I never read the part about improved performance. I thought it slowed things down, but I can’t find that in the manual. But reading the Notes on https://www.php.net/manual/en/function.ob-start it seems that there are a lot of things to look out for: errors during buffering, headers, compression.
    I would advise you to avoid using it unless you have to capture something that you can’t capture any other way. The code you showed could have been directly output instead of using ob_start. Shortcodes have to be returned in a string, but it depends on the situation as to whether output buffering is needed or not.

    Thread Starter Rajarshi Bose

    (@truthsearcher83)

    Thank you , Joy .

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Question about using ob_start()’ is closed to new replies.