Read-Only REST Endpoint for ACF Options
Publishes selected fields from your ACF options pages at /wp-json/wpide/v1/site-options, so a headless front end or app can load site-wide settings.
Opens WPIDE on
Code
// Options fields that may be shared publicly.
$wpide_acf_public_options = array( 'phone', 'email', 'address', 'social_links' );
add_action( 'rest_api_init', function () use ( $wpide_acf_public_options ) {
register_rest_route( 'wpide/v1', '/site-options', array(
'methods' => 'GET',
'permission_callback' => '__return_true',
'callback' => function () use ( $wpide_acf_public_options ) {
$data = array();
foreach ( $wpide_acf_public_options as $name ) {
$data[ $name ] = function_exists( 'get_field' ) ? get_field( $name, 'option' ) : null;
}
$response = rest_ensure_response( $data );
$response->header( 'Cache-Control', 'public, max-age=300' );
return $response;
},
) );
} );
Notes
Only list fields that are safe to make public. The response is cached by browsers for five minutes.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.