Home Forums General Issues Default Field Value on an Options Page


Default Field Value on an Options Page

  • We are using an options page and applying a field with a default value to the options page.
    If the page has not been saved, there is no data for the field and get_field() is giving an empty response.

    I understand that saving the options page will add the default value to the database. However, we would like to have these default values set without any action by the user.

    Currently, in our class’s constructor, we are checking to see if the get_field() returns nothing, and updating the field with the default value if it is not already set. However, it seems like this is defeating the purpose of using eh default value setting for the field.

    Can ACF be updated to return the default value if there is nothing stored in the database for that field?

    This is only an issue for options pages, since regular posts are initially saved when they are created and options pages are not.

  • If you get the field using the field key it should return the default value.

    The problem isn’t so much the missing value as it is the missing field key reference for the value.

    I personally have always coded default values in. The reason, I may add fields in the future, these fields will not have values for existing posts. I can never count the data being there, any number of things could cause the value to not be set. But that’s just me.

  • That solved the issue.

    Thanks for the tips.

  • I also ran a benchmark because I heard that getting the field by its key will save a database query or two.
    The benchmark was a single php function that called get_field() once. Across 1000 calls, getting the field by its key saved 3 seconds on my machine.

    We will start making the field keys human-readable and making all of our get_field() calls with the key.


  • make sure that your field keys all start with "field_"

  • Ok, I’ll have to update them. We were prefixing with "ft_field_".
    What problems will come about by not prefixing with "field_"?

  • “field_” is how ACF determines that it’s a field key and that prefix is required. Anything else and ACF considers it a field name. Some functions will fail or through errors you use something else. You can put practically anything after the _.

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

The topic ‘Default Field Value on an Options Page’ is closed to new replies.