Support

Account

Home Forums Backend Issues (wp-admin) Automatically setting default settings on new multisite install

Solved

Automatically setting default settings on new multisite install

  • I am working on a WordPress multisite network, where users can register sites on subdomains.

    I am developing a customized theme for the registered sites using ACF Pro, including use of the ‘Options Page’ feature, in order to implement customization of various features such as menus, layout settings, etc…

    I populate many fields with default settings so that a registered site can function properly on a fresh install. The problem is, is that “Options Pages” do not seem to publish automatically when a site is installed. To activate the settings, the user would need to go to the page and click “publish” before they go into effect and are integrated into the theme’s processes.

    I am trying to either get the options pages to update automatically on an install or get the custom fields themselves to work.

    I am currently experimenting on the ‘wpmu_new_blog’ hook. The fields I am trying to set automatic defaults to are nested within two tiers of groups (parent_group -> sub_group -> sub_field) and I am trying to use the update_sub_field() function specified by ACF PRO

    update_sub_field( array(array('parent_group', 'options'), 1, 'sub_group', 1, 'sub_field'), 'Value for Subfield' );

    This is not working however. The function itself returns a series of errors:

    • Notice: Array to string conversion in [several lines on /advanced-custom-fields-pro/includes/acf-meta-functions.php ]
    • Warning: Illegal offset type in isset or empty in [several lines on /advanced-custom-fields-pro/includes/acf-meta-functions.php ]

    I am at a loss here. Any suggestions would be greatly appreciated!

    Thank you so much.

  • The first thing is that when updating fields that do not always have values then you need to use the field key. This is covered in the documentation.

    I would set the entire group using update_field()

    
    $value = array(
      // a group field's value is an array
      // sub field key => value pairs
      // this is a simple sub field
      'field_xxxxx' => 'value',
      // a nested group would be a nested array
      'field_yyyyy' => array(
         // nested group field
         'field_zzzzz' => 'value of field',
      )
    );
    update_field('field_ggggg', $value, 'options');
    
  • However, after giving the above, I would not attempt to set these values on the creation of a new site.

    First I would try using the field key to get the values. Doing this might return the default values that you have set for the fields.

    If using the field keys does not return the default values then I would code the default values where they are used, which is much easier than trying to set all of your default values during site creation.

    
    $value = get_field('your-field-name', 'options');
    if (!$value) {
      $value = 'the default value';
    }
    

    I might event do both of these things at the same time, use the field key to try to get the value and then set a default in code if there is no value.

  • Thanks for the replies, John! Your explanation of how the Group Field values works was very helpful, as I could not find that information anywhere else (the update_field() page on ACF seems focused mainly on repeaters, with rows and etc..).

    Unfortunately your suggestion of hardcoding default values if there is no value set would not work in my use case; I want users to be able to create blank fields if they wish, and with this suggestion, removing the field entirely would simply return the default value (not an empty space).

    Do you know the simplest way to find the field keys (as opposed to field names)? It does not seem intuitive to me.

    I will try to re-explore this when I get some time to work on it and keep you updated. Thank you so much for the help!

  • At the top of the page when editing the field group under screen options you can show the field keys.

    A group field is actually a special type of repeater field that always has exactly 1 row. So the difference in the array structure is that you do not need sub arrays for each row.

    As far as default values vs blank field entries there is a way to detect this. If a field has been edited but contains no value the the return will usually (but not always) be different than if the field was never set. In most cases the value will be false or null if it was never set and will be an empty string if it was set.

    
    if ($value !== '') {
      // value does not exactly equal an empty string
      // it was never set
      // use a default value
      $value = 'my default value';
    }
    

    of course this will not always work and you’d have to test different types of fields to see what they are when never set vs having been saved without a value.

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

You must be logged in to reply to this topic.