Force ACF Default Values On Existing Posts In Oxygen Builder

You may want to add a field to your WordPress posts using the Advanced Custom Fields plugin. But you may also want that field's value to fall back to a default value, if for whatever reason, the ACF field is not provided by the user.

Initially this seems easy to do in Advanced Customer Fields, as they do provide an opportunity for you to specify a default value.

ACF Allows for default values
Where to place a default value in ACF

But What About Existing Posts?

The problem is that any default value supplied in the way shown above, will not apply to any existing posts. If this is a problem for you, you can address the default value in another way by adding a quick code block and changing the data source of the field in your Oxygen Template.

Example Workaround Using A Code Block

Here is an example where we're designing an Oxygen template and want to specify a different hero image for each post.

The hero image URL for each individual post will be supplied via an advanced custom field. But unfortunately, in our case our website already has a large number of posts. To avoid having to re-visit each post and re-save it, we'll have to come up with a faster and easier way to specify a default for any existing posts that don't have a specific hero image supplied.

Please note, if you are using this method you do not need to specify a default value within ACF. You can - but as already pointed out it will not apply to existing posts. But it will also not be over-written on new posts by the coded workaround either. So if you were to make the ACF default different to the code block default, you'd end up with different defaults for missing posts. Just thought I'd point that unlikely scenario out, LOL.

  1. First, add a code block as close to the top of the template as you can. If you add it too far down it may not be defined in time to be used.
  2. Inside the code block place the following code then change it to suit your needs. You must put the correct field name in the call to get_field, then where I have specified a URL, you must return your chosen default value. Please note the code must appear between
    <?php and ?> tags.
  1. The next step is to refer to the function wherever you need to use the result it returns. In my case this is in the background image of a section. To set this up, I click on the data button on the Oxygen background image field, and arrive here at the Insert Dynamic Data screen.
  2. Because we have written our own function, click on the PHP Function Return value button.

Specify dynamic data
Specify dynamic data in Oxygen Builder
  1. The next screen to show is this one. Type the name of the function to call - in this case, herobg into the function name box.
PHP Function Return screen
Specifying the function name to call

That's it. When you specify a specific URL through ACF on your post, you'll get the image of your choice, and when you don't you'll get the default stated in the code.

Enjoy The Article? Leave A Comment

We welcome your comments, but please do not use ALL CAPS when providing your name, email or comment or we cannot guarantee your comment will make it through our comment system. Your email address will not be published.

linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram