Home Forums ACF PRO ACF Theme Options > Store/Fetch Efficiently Reply To: ACF Theme Options > Store/Fetch Efficiently

  • Some optimizations that you can make.

    1) set the ACF options page to auto load values, this will not reduce the amount of code you need to run, but it will reduce the number of queries that need to be done.

    2) Set your options page to save values to a specific post ID (when doing this, auto load is not used, I think). Doing this you can use somehting like

    $options = get_fields($post_id); // post id where options are stored

    your options will already be in key => value pairs, but this might lead to more checking needed in your theme where the values are used since you can’t perform the conditional parts doing this.

    3) A different direction, instead of building your options on the front end of the site you can build them in the admin. Use an acf/save_post action. You can use the function get_current_screen() to see what options page is being saved. You can then build your theme options array and save this as a new options using update_option() Then on the front end of the site you only need to get the array that’s already been built.

    4) A minor improvement to reduce code, instead of first getting the values and then setting them, do it all at once.

    add_action( 'template_redirect', 'theme_vars' );
    function theme_vars() {
    	static $my_theme_vars = array();
    	if ( empty( $my_theme_vars ) ) {
    		// General
    		$my_theme_vars['my_theme_option_1'] = get_field('my_theme_option_1','option');
    		$my_theme_vars['my_theme_option_2'] = get_field('my_theme_option_2','option');
    			$my_theme_vars['my_theme_option_2'] = 'custom_value'; // I set some values conditionally
    		// This goes on, let's say I have 50 more of these
    	return $my_theme_vars;