Support

Account

Home Forums ACF PRO Options Page: First Use

Solving

Options Page: First Use

  • hi,

    i use acf in our theme (and option page plugin for creating theme option) and create json files in acf-json folder.

    everything it’s ok but one big problem: when user install theme, no field active or can be used until user go to Option Page-> Click on Save.

    any solution exist for this problem?

    sorry about my bad English

    tnx

  • You need to check for and set default values because there are not values saved until the user saves the page. get_field() returns false when a value has not been saved yet.

    
    $variable = get_field('my_field', 'option');
    if (!$variable) {
      $variable = 'my_default_value';
    }
    
  • tnx @hu@Hube2 for reply,

    but any better solution for this? like create a function to run all options page and save theme. for now we are many field and create a default value for each one? not a good idea

  • You can look at this: http://www.advancedcustomfields.com/resources/functions/update_field/, as well as http://codex.wordpress.org/Plugin_API/Action_Reference/after_switch_theme and http://codex.wordpress.org/Plugin_API/Action_Reference/switch_theme

    But this feels like fixing a problem that could have been prevented in the first place by applying some basic coding standards.

    For me, basic value validation is an automatic thing and I think much easier to deal with then building a script to populate the database when my theme is activated.

    Do you attempt to echo $_POST[‘some_key’] before you test to see if it has a value? Well, if you don’t care about warnings and errors, or security I guess. Personally I don’t use any value unless I’m sure I know what it contains before I use it; that’s just basic security and PHP best practices.

  • tnx a lot @Hube2

    for example some field type is true/false, this field type very important and no need to check value. i think @elliot must fix this problem specially for Options Page Premium plugin.

    I still can not find a solution to this problem, but I think use update_field function with a php loop in export file of fields can help me for now… any suggestion?

  • As far as true/false fields go. The value “1” is stored for true and “0” for false. So you’d need to do exact checking:

    
    if ($variable === false) {
      // get_field() returned exactly false, so the value is not set.
      // set the default value
    }
    

    The links above also include the links to the action hooks for when a theme is activated or switched. You’d need to create a function that runs on one or both of these hooks and use the update_field() function to insert all of your default values. You could probably loop through the “fields” sub-array of the export to get the field keys and values to insert. The difficulty of actually inserting the values will depend on the field types involved. I think that repeater fields (and maybe some others) will be problematic.

    Really, there are only 2 choices, value checking in the theme when the value is loaded or building a function to insert the data when your theme is activated, and of course seeing if it was already inserted by a previous activation of your theme or removing the data when your theme is deactivated.

    This isn’t really a problem is the options page addon. The insertion of data does not happen until someone edits the options page. You’re suggesting that instead, when a field group is created that all the default values are auto inserted. There is no way to tell where that data needs to be inserted because you can attach the same field group to many places (posts, pages, options pages, users, custom post types, etc) many of which will not even exist when when the field group is created. Doing this would by pretty much impossible to detect when to do it and where to insert the data.

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

The topic ‘Options Page: First Use’ is closed to new replies.