Asked  7 Months ago    Answers:  5   Viewed   57 times

I want to expose all the ACF fields that belong to a page or custom post type to the WordPress REST API in order to do some API calls through javascript.

The final expected result would be all the ACF fields inside an ACF object that you can easily access.

 Answers

53

Through the following code, you will be able to expose page and your custom postypes ACF fields in the wordpress REST API and access them inside the ACF object.

You can obviously customise the postypes to exclude or to include in the arrays: $postypes_to_exclude and $extra_postypes_to_include.

function create_ACF_meta_in_REST() {
    $postypes_to_exclude = ['acf-field-group','acf-field'];
    $extra_postypes_to_include = ["page"];
    $post_types = array_diff(get_post_types(["_builtin" => false], 'names'),$postypes_to_exclude);

    array_push($post_types, $extra_postypes_to_include);

    foreach ($post_types as $post_type) {
        register_rest_field( $post_type, 'ACF', [
            'get_callback'    => 'expose_ACF_fields',
            'schema'          => null,
       ]
     );
    }

}

function expose_ACF_fields( $object ) {
    $ID = $object['id'];
    return get_fields($ID);
}

add_action( 'rest_api_init', 'create_ACF_meta_in_REST' );

Here's the gist for reference: https://gist.github.com/MelMacaluso/6c4cb3db5ac87894f66a456ab8615f10

Wednesday, March 31, 2021
 
turson
answered 7 Months ago
76

A quick and dirty way to do it would be to find this file:

/wp-admin/edit.php

And comment out the following line:

echo '<div id="message" class="updated notice is-dismissible"><p>' . join( ' ', $messages ) . '</p></div>';

Of course that will get rid of all your 'moved to Trash' messages. So you'll probably want to keep it in there, but check for your custom post type first.

Or better yet, figure out how to override this from your functions.php file so the next WP update doesn't kill it.

Saturday, May 29, 2021
 
Gilko
answered 5 Months ago
86

My suspicion is that you are not using a new-enough version of Confluence. The REST API for creating a new page was introduced in Confluence 5.5 (which came out 8 days ago). The API documentation is versioned, and you should always use the version corresponding to your Confluence release. The 5.5 API docs include the page creation API you need, but older versions do not. You can change the version in the above URL to get the API version matching your Confluence release.

Confluence 5.4 and prior also used a different root prefix for the REST API (/rest/prototype/1/content) which is one possible reason for getting a page not found error.

The example on the Atlassian site is also confusing because it includes an extra "/confluence" in the URL, which you would only need if Confluence were set up with a context path. This could also result in a page not found error if you were using Confluence 5.5+ (although your post suggests that you already corrected for this).

Additionally, you need to tell Confluence that you are using the basic authentication method by adding a special os_authType query parameter.

The following example works for me on Confluence 5.5 (don't forget to change the port and space key as appropriate).

For safety, I also added the appropriate content type to the Accept header, although this seems to be not needed in practice.

curl -v -u admin:admin -X POST -H 'Content-Type: application/json' -H 'Accept: application/json' -d'{"type":"page","title":"new page","space":{"key":"ATTACH"},"body":{"storage":{"value":"<p>This is a new page</p>","representation":"storage"}}}' "http://localhost:8090/rest/api/content/?os_authType=basic"

To answer your last question, the specific API that creates the page is determined by the URL itself and the request method. For example, performing a GET on "/rest/api/content" will fetch an existing page (given the appropriate query parameters), while performing a POST will create a new page.

EDITED TO ADD:

See also my comment below for how to create a page as a child of another existing page, as opposed to just at the top level of a space.

Wednesday, June 23, 2021
 
Savageman
answered 4 Months ago
73

Like the GET request, You can use the POST request as well to store data into the CMS. What you need to do is to pass the authorization headers with the POST API call.
You can get more detail about the authorization mechanism here: https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/

Headers:

Authorization:Bearer <token>
Content-Type:application/json

Secondly you can pass the Body data as a RAW json as below:

{
"title":"Sample ACF field demo",
"status": "publish",
"fields": 
    {      
        "text_custom_field_name" : "Text value",
        "checkbox_custom_field_name" : [
                "Option1,",
                "Option2,",
                "Option3"
            ],
        "textarea_custom_field_name" : "This is message field",
        "boolean_custom_field_name" : [
                true
            ]
    }
}

Please let me know if any help needed.

Thanks

Saturday, July 31, 2021
 
Bálint Molnár
answered 3 Months ago
98

To add rows into sub-fields of a repeater field, you can do:

$field_key = "repeater_field";
$user_id = "user_123"; // save to user (user id = 123)

$value = get_field($field_key, $user_id);
$value[] = array("event_id " => 25);
$value[] = array("event_id " => 30);
update_field( $field_key, $value, $user_id );

To remove items, I'd use a PHP function such as the one in the accepted answer at Delete element from multidimensional-array based on value:

$value = removeElementWithValue($value, "event_id", 25);
update_field( $field_key, $value, $user_id );

As to where to add these codes (ie. the action hook) depends on when you'd like to add/remove the items.

Tuesday, August 10, 2021
 
Vaishak Suresh
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :