Support

Account

Home Forums ACF PRO ACF Theme Options > Store/Fetch Efficiently

Solved

ACF Theme Options > Store/Fetch Efficiently

  • Hi there!

    I would like to:

    1) use ACF theme options fields to set values both on a theme and post level [Status: Done]
    2) use a way to store/fetch these with as little overhead (and fastest TTFB) as possible [Status: I have the below function in my functions.php file]
    3) I would like to know a) is there a better way to do this? If so, what would that look like?

    My code:

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

    So then for instance, to fetch my variable I would do something like:

    
    if( class_exists('acf') ) {
    	global $my_theme_vars;
    	$my_theme_vars = theme_vars();
    };
    
    echo $my_theme_vars['my_theme_option_1'];
    

    Thanks in advance!

  • 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() https://codex.wordpress.org/Function_Reference/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');
    		if(empty($my_theme_vars['my_theme_option_2'])){
    			$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;
    };
    
  • AWESOME! Thank you John.

    Just to step back a bit and see if I’m understanding what you mean on a concept level…is the main gist of what your getting at: store multiple options as an array in a singular field as opposed to multiple calls for each individual one?

    I will work on what you outlined, but just have a few questions:

        RE: Points 1-3) Would these be exclusive of each other or in conjunction? How would this be implemented for the options page, via hook?
        RE: 1 – ACF Page > Autoload values) Could you clarify what you mean here? Where would I load vales from/to? Is there are documentation page for this?
        RE: 3 – Admin/Front-end Build / Array) So, basically, using acf/save_post, I just dump all of the options into one option_field?
        RE: 4 – Much better, thanks! 🙂

    Thanks! I massively appreciate the insights, they are superb!

    ~Ryan

  • 1 and 3 could both be done, 1 will reduce the number of the mysql queries performed to get values stored in the options table. WP does a query and caches all options values that are set to autoload in a single query.

    1) autoload was added as a argument for options page in 5.2.8 and is listed here https://www.advancedcustomfields.com/resources/acf_add_options_page/

    3) Yes, that’s what I’m saying. Use the ACF options page to make it easier to build the backend form, but then do what most plugins and themes do and store the “Real” options in an array that is per-assembled. It will slow down the admin a hair. Although, the main thing that will slow down the front end is additional queries, a few hundred lines of code will not have a significant impact. Well, depending on what those lines of code do I guess.

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘ACF Theme Options > Store/Fetch Efficiently’ is closed to new replies.