Home Forums Add-ons Options Page Options Page Fields Need Unique Names


Options Page Fields Need Unique Names

  • Bug: Options Page Add-on Field Names Must be Unique

    Problem Observed: When I used the same field names for different field groups with the options page add-on, changes to one field group would overwrite content in other field groups.

    Possible Solution: While the wp_options table does store unique field IDs for each field, ACF doesn’t seem to use the unique field ID. Maybe you could append the field ID to option_name in wp_options, and possibly use a third argument for getting the value with the_field, like:
    the_field( 'field_name', 'option', 'field_ID' )

  • In the case of options pages, acf is just a wrapper for the WP functions get_option() and update_option(). There is nothing built into these functions that allows for duplicate field names. In fact, the database is set up so that the “option_name” value must be unique.

    There is a work-a-round for this that is not well known. When saving options ACF prefixes the option name with “options_”. You can alter this by altering the post ID used for the options page. This is one of the settings when adding an options page. For example, if you used the post ID of one page or “theme_options” and the post ID of “some_other_options” for a second page and both options pages have a field name “some_field_name” ACF will store one at “theme_options_some_field_name” and the other at “some_other_options_some_field_name”

    You just need to give the correct post ID when getting these options, instead of get_field('my_field', 'options') you would do get_field('my_field', 'theme_options')

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

The topic ‘Options Page Fields Need Unique Names’ is closed to new replies.