Support

Account

Home Forums General Issues Default values by option page on first load

Solved

Default values by option page on first load

  • Hey,

    I created a theme with ACF pro and now I test the complete functionality on a new WordPress installation. I using default field values by option settings (e.g. True/False). Some of the fields are true by default value.

    After activating the theme and importing all data, the true field values are not loading. Only, if I go to ACF option page and click the update button, the default values are loading.

    How I can handle this, that all default values ​​loading from the beginning without the extra click on option page update button?

  • @acf-support is there a way to trigger the option page update via theme setup?

  • You either need to use update_field() to set defaults for every field when the theme is set up https://www.advancedcustomfields.com/resources/update_field/ or check the values and if nothing is returned then assume the default.

    
    $value = get_field('my_options', 'option');
    if ($value === NULL) {
      $value = 'my-default-value';
    }
    

    I prefer the second method.

  • @hube2 thanks for your reply. With both methods, then I would have to do this for each field? This is very complex with over 50 fields. πŸ™

    Is there a way to make a virtual click by the option page update button? πŸ™‚ Can I use a acf function for this?

    I can not understand this exactly.

    $value = get_field('my_options', 'option');

    Is one of my option fields with a default value.

    $value = 'my-default-value';

    But what is ‘my-default-value’ or the content of this? The value comes from the field. And I look for this directly after the theme setup.

  • I tried it but I do not know if that is correct.

    function mytest() {
    	$value = get_field('my_options', 'option');
    	if ($value === NULL) {
    	  $value = '1';
    	}
    }
    
    add_filter( 'acf/load_field/key=field_580b7d8b9ceea', 'mytest' );

    $value = ‘1’ = true (from True/False field)?!

    Unfortunately it does not work.

  • With both methods, then I would have to do this for each field?

    Yes

    Is there a way to make a virtual click by the option page update button?

    No

    I can not understand this exactly.

    
    $value = get_field('my_options', 'option');
    

    ‘my_option’ is whatever field name you are trying to get

    $value = ‘my-default-value’;

    This is just an example, ‘my-default-value’ is whatever value it is you want to be the default. It could be true, false, a number, it depends on what you want the value to be.

    ACF does not automatically create default values when a field group is created and it will not return the default value if a value is not set.

    Another way that you can work around this would be to create a filter for each field type, for example, a true/false field might be (see https://www.advancedcustomfields.com/resources/acfload_value/)

    
    /*
      please note that this is just an example and you will need to test this
      I am not 100% sure as I am writing this what $field['default_value']
      for a true/false field will contain. The best way to figure this out
      is to go to ACF => Tools and export a field groups an you'll see by
      looking at each field array what is in the default value
    */
    add_filter('acf/load_value/type=true_false', 'get_default_true_false_value', 20, 3);
    function get_default_true_false_value($value, $post_id, $field) {
      if ($value === NULL) {
        $value = $field['default_value'];
      }
      return $value;
    }
    
  • See the comment in my code above. I don’t know if you should be setting the value to ‘1’ or 1 or true, it could make a difference. any non empty value will result in true in a statement like

    
    if ($value) {
      // do something
    }
    

    even a value of ‘0’ will return true because it is a non empty string.

  • Ok I tested your first solution:

    $white_menu = get_field('top_nav_color', 'option');
    	if ($white_menu === NULL) {
    	  $white_menu = '1';
    	}

    This is my example and it works, by including this in the specific template, where the field is called.

    But, now I can’t unterstand your next solution. Is this a workaround for all true/false fields or do I have to define it for each field also?

    The easier way is to write a documentation, with explaining to go to the backend and click the update button πŸ™‚

  • @hube2 thanks for helping:

    I have find a way to solve it:

    First I set the fields as globals with there default values in function.php.

    
    function acf_global_vars() {
    
        global $white_menu;
        $white_menu = get_field('top_nav_color', 'option'); 
    	if ($white_menu === NULL) { $white_menu = '1'; } // default value
    	
    	// ------ //
    	
    	global $image_white;
        $image_white = get_field('logo_white', 'option');
    	if ($image_white === NULL) { 
    		$image_white['url'] = get_template_directory_uri() . '/img/small-business-logo-white.svg'; 
    		$image_white['alt'] = 'hallo'; 
    	} // default value
    	
    	// ------ //
    	
    	global $image_normal;
        $image_normal = get_field('logo_normal', 'option'); 
    	if ($image_normal === NULL) { 
    		$image_normal['url'] = get_template_directory_uri() . '/img/small-business-logo.svg';
    		$image_normal['alt'] = 'hallo'; 
    	} // default value
    	
    	// ------ //
    	
    	global $logo_white_width;
        $logo_white_width = get_field('logo_white_width', 'option'); 
    	if ($logo_white_width === NULL) { $logo_white_width = '200'; } // default value
    	
    	// ------ //
    	
    	global $logo_normal_width;
        $logo_normal_width = get_field('logo_normal_width', 'option'); 
    	if ($logo_normal_width === NULL) { $logo_normal_width = '200'; } // default value
    	
    	// ------ //
    	
    	global $top_search;
        $top_search = get_field('top_search_show', 'option');
    	if ($top_search === NULL) { $top_search = '1'; } // default value
    
    }
    
    add_action( 'template_redirect', 'acf_global_vars' );
    

    Than I call the globals in the template header.php

    global $white_menu, $image_white, $image_normal, $logo_white_width, $logo_normal_width, $top_search;

    And start the IF

    <?php if( $white_menu ): ?>
    <img class="logo-white" src="<?php echo $image_white['url']; ?>" alt="<?php echo $image_white['alt']; ?>" width="<?php echo $logo_white_width; ?>" />
    <?php endif; ?> 

    Is this the best method I can have?

  • Glad you worked out a solution. There are probably a lot of different ways it could be done. Setting default values in code is one of those extra steps that you need to take, one way or another, when you’re building something with ACF that will be distributed. ACF probably does not do this because most people will not need it.

  • @hube2 Thank you very much for your patience. πŸ™‚

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

The topic ‘Default values by option page on first load’ is closed to new replies.