10 Nov

ACF 5.11 Release – REST API

By Iain Poulson

Our next major release of Advanced Custom Fields is now widely available. ACF Version 5.11 includes some exciting developer-focused improvements.

WP REST API Support

The biggest addition in this release is support for ACF custom fields in the WordPress REST API.

Until now, ACF field values were not viewable or editable via the WP REST API without the use of custom code or another plugin. With this release, support for the WP REST API is baked right into ACF field groups with the flip of a switch. This means that ACF field values can now be accessed through existing endpoints for WordPress content objects like posts, pages, custom post types, and users.

This gives ACF-powered sites endless possibilities with the WP REST API. For example, let’s say you have a site for a car dealership that utilizes a custom post type for cars, enriched with ACF fields for make, model, engine size, color, etc. If third-party services need to access this data, you can supply the WP REST API URL which returns JSON data to be consumed by those services:

https://cranfordcars.com/wp-json/wp/v2/cars/
{
  "id": 65,
  "date": "2021-11-10T08:26:55",
  "date_gmt": "2021-11-10T08:26:55",
  "guid": {
    "rendered": "https://cranfordcars.com/?post_type=cars&p=65"
  },
  "modified": "2021-11-10T08:26:55",
  "modified_gmt": "2021-11-10T08:26:55",
  "slug": "bmw-320i",
  "status": "publish",
  "type": "cars",
  "link": "https://cranfordcars.com/cars/bmw-320i/",
  "title": {
    "rendered": "BMW 320i"
  },
  "content": {
    "rendered": "<p>The <b>BMW 3 Series</b> is a line of <a title=\"Compact executive car\" href=\"https://en.wikipedia.org/wiki/Compact_executive_car\">compact executive cars</a> manufactured by the German automaker <a title=\"BMW\" href=\"https://en.wikipedia.org/wiki/BMW\">BMW</a> since May 1975. It is the successor to the <a title=\"BMW 02 Series\" href=\"https://en.wikipedia.org/wiki/BMW_02_Series\">02 Series</a> and has been produced in seven different generations.</p>\n",
    "protected": false
  },
  "template": "",
  "acf": {
    "make": "BMW",
    "model": "320i",
    "engine_size": "2l",
    "color": "White"
  },
  "_links": {
    "self": [
      {
        "href": "https://cranfordcars.com/wp-json/wp/v2/cars/65"
      }
    ],
    "collection": [
      {
        "href": "https://cranfordcars.com/wp-json/wp/v2/cars"
      }
    ],
    "about": [
      {
        "href": "https://cranfordcars.com/wp-json/wp/v2/types/cars"
      }
    ],
    "wp:attachment": [
      {
        "href": "https://cranfordcars.com/wp-json/wp/v2/media?parent=65"
      }
    ],
    "curies": [
      {
        "name": "wp",
        "href": "https://api.w.org/{rel}",
        "templated": true
      }
    ]
  }
}

This also allows developers to build custom themes using React, Vue, or any other JavaScript library.

You can enable or disable specific field groups from being included in the REST API by toggling the “Show in REST API” option in the field group settings. The default for this setting is No, so you must toggle this to Yes if you want your field groups to show in the WP REST API.

Show in REST API field group setting.

Any custom field groups added to WordPress data like posts (including all custom post types), users, and categories (including all custom taxonomies), will be available in their respective WP REST API endpoints.

The ACF data is available when using the following request verbs on a valid WP REST API endpoint:

  1. GET requests – allow you to view ACF data.
  2. OPTIONS requests – allow you to view the schema for the ACF data.
  3. POST requests – allow you to update any ACF fields.

Due to the fact that POST requests allow you to manipulate your ACF data, you will be required to authenticate your POST requests, using one of the authentication methods available to WordPress.

You can find all the details on how the ACF integration for the WP REST API works in our WP REST API doc, including a list of available endpoints, details on how to authenticate your POST requests, and full code examples.

REST API support is available in both the ACF and ACF PRO plugins.

License Key Improvements

One common request we’ve seen from users is the ability to define the ACF PRO license key in code rather than storing it in the WordPress database. This is an option we have enabled for all our WordPress plugins, so it made sense to add it to ACF.

ACF will now look for and use a PHP constant named ACF_PRO_LICENSE, which you can define with your license key.

define('ACF_PRO_LICENSE', 'yourkeyhere' );

This can be done in the wp-config.php file or any other file that WordPress runs before it loads the ACF PRO plugin. For more details, check out our guide on how to activate the ACF PRO plugin.

Security Fixes, Improvements, and Bug Fixes

We’re always looking at ways to improve the security and functionality of our products, and ACF is no exception. In this release we included the following improvements:

  1. Following the lead set by the WooCommerce team, we’ve removed all instances of the extract function and explicitly set the intended variables.
  2. We have improved security for AJAX user queries in ACF.
  3. We’ve fixed a bug where the .acf-block-preview wrapper didn’t appear when previewing a block in auto mode.
  4. We’ve fixed a security issue where ACF functions like get_field() could be used to retrieve values not associated with ACF data. For more details, see the get_field() documentation.

Developer Notes

Thanks to translation contributor Dariusz Zielonka for sending us updated Polish translation strings. 🎉

If you are interested in helping improve the translation of ACF into other languages, please let me know. 🙌

The acf_shortcode function has been updated so that ‘acf’ is passed as the $shortcode parameter to the shortcode_atts function. This enables an ACF specific filter, shortcode_atts_acf which allows developers to filter shortcode attributes.

add_filter( "shortcode_atts_acf", function( $out, $pairs, $atts, $shortcode ) {
        // we're in the context of an acf shortcode
}, 10, 4 );

WordPress Blocks API v2 Updates

In ACF 5.10, we moved to support version 2 of the WordPress Blocks API included in WordPress 5.6. The goal was to support more advanced functionality such as the ability to use the Block Editor’s extraProps filter. Unfortunately, our implementation of this triggered re-rendering of ACF blocks any time a property changed, which caused issues with other block editor features such as block styles and the built-in spacing/padding support.

ACF 5.11 removes support of the extraProps functionality to prevent these re-renders. This will improve performance and solve issues with block styles.

We want ACF blocks to function as closely as possible to core or custom-built WordPress blocks. Therefore we’re working on re-introducing support for this in an upcoming release. Doing so will change the structure of ACF blocks to include an HTML wrapper outside of your template which can handle any extra props, spacing, and custom styles.

More Goodness

These are just the highlights of all the features, improvements, and bug fixes bundled in this release. To see a full list of all the updates, take a look at the changelog.

Internal Improvements

While there is plenty new and improved for you in this release, we’ve also been hard at work behind the scenes. Since the last release, we’ve implemented a series of code quality and testing improvements, which will help us build better and faster going forward.

  1. We refactored the existing test suites to use Codeception for unit testing and acceptance tests.
  2. The ACF integration with the WP REST API includes unit tests.
  3. The asset build scripts have been updated to use webpack and wp-scripts.
  4. All JavaScript code has been formatted to WordPress coding standards.

What’s Next?

As we wrap up our latest release, let’s take a look at what we have planned for the next one:

  1. Compatibility with WordPress 5.9, which is due to release on December 14th.
  2. Better support for v2 of the WordPress Blocks API and reintroducing support for the extraProps filter.
  3. Improvements to the Field Group editing experience.
  4. Database performance improvements.

Thanks to everyone in the ACF community who helped make this release possible. 🙌

Are you excited about ACF 5.11? What are you planning on doing with the WP REST API integration? Let us know on Twitter.

For questions and help about this release, please contact our support team.

About the Author