Skip to content

Instantly share code, notes, and snippets.

@paulmiller3000
Last active February 18, 2020 18:08
Show Gist options
  • Save paulmiller3000/390b8a7885a10efd36f3a56085948e25 to your computer and use it in GitHub Desktop.
Save paulmiller3000/390b8a7885a10efd36f3a56085948e25 to your computer and use it in GitHub Desktop.
Register Advanced Custom Fields for WooCommerce with the WordPress API
<?php
/*
* Register ACF fields to WordPress API
* Tutorial: https://battlestardigital.com/wp-admin/post.php?post=2838&action=edit
*/
add_action( 'rest_api_init', 'bsd_register_acf_with_api' );
function bsd_register_acf_with_api() {
if (!function_exists('get_fields')) return;
register_rest_field(
'product',
'acf',
array(
'get_callback' => 'bsd_add_acf_fields',
)
);
}
function bsd_add_acf_fields( $object, $field_name, $request ) {
$acf = get_fields( $object['id'] );
return $acf;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment