Giter Site home page Giter Site logo

times / acf-to-wp-api Goto Github PK

View Code? Open in Web Editor NEW
203.0 24.0 24.0 666 KB

Puts all ACF fields from posts, pages, custom post types, attachments and taxonomy terms, into the WP-API output under the 'acf' key

Home Page: http://wordpress.org/plugins/acf-to-wp-api/

License: MIT License

PHP 100.00%

acf-to-wp-api's Introduction

ACF to WP-API

Join the chat at https://gitter.im/times/acf-to-wp-api

Contributors: chrishutchinson, kokarn, ramvi
Tags: acf, api, wp-api
Requires at least: 3.9.0 Tested up to: 4.7.3 Stable tag: 1.4.0 License: MIT
License URI: http://opensource.org/licenses/MIT

Plugs Advanced Custom Fields (ACF) data into the WordPress JSON API (WP-API).

Description

Puts all ACF fields from posts, pages, custom post types, comments, attachments and taxonomy terms, into the WP-API output under the 'acf' key. Creates a new /option endpoint returning options (requires ACF Options Page plugin).

Installation

  1. Unzip and upload the acf-to-wp-api directory to /wp-content/plugins/.
  2. Activate the plugin through the 'Plugins' menu in WordPress

Frequently Asked Questions

How can I log and issue or contribute code?

See the CONTRIBUTING.md file.

How can I filter posts on a custom field?

See issue #13 for example code to do this.

Options Endpoints

/wp-json/wp/v2/acf/options

Request a list of all options configured in ACF

/wp-json/wp/v2/acf/options/{option-name}

Request a specific option, by passing in the option name

Changelog

1.4.0

  • Compatibility improvements for WordPress 4.7

1.3.3

  • Compatibility fix for V2.0Beta9

1.3.2

  • Adds support for custom post types when using v2 of the REST API

1.3.1

  • Fix to support PHP < 5.4

1.3.0

  • Add support for v2 of WP-API
  • Restructure of much of the code, adding documentation throughout
  • Add an additional endpoint for requesting single option values
  • Tested with WordPress 4.3.0

1.2.1

  • Tested with WordPress 4.2.1

1.2.0

  • Added ACF data to comments (Thanks @ramvi).

1.1.0

  • Add /option endpoint for ACF options add-on (Thanks @kokarn).

1.0.1

  • Fix for addACFDataTerm.

1.0.0

  • Initial release.

acf-to-wp-api's People

Contributors

chriscornford avatar chrishutchinson avatar gitter-badger avatar kokarn avatar ramvi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

acf-to-wp-api's Issues

ACF inserted with update_field not shown in REST API

On a new post programmatically inserted, if i user update_field() the REST API won't show ACF fields, an "empty field": false is returned, i have to use update_post_meta, no matter if i publish the post and empty the post cache.

$post_id=wp_insert_post( $new_post );

// UPDATE CUSTOM FIELD
//update_field( "weight", $weight, $post_id );
update_post_meta( $post_id, "weight", $weight );

//update_field( "birth_date", $birthday, $post_id );
update_post_meta( $post_id, "birth_date", $birthday );

//update_field( "chip", $chip, $post_id );
update_post_meta( $post_id, "chip", $chip );

$file = $_FILES['photo'];
$attachment_id = media_handle_upload( 'photo', $post_id );
set_post_thumbnail( $post_id, $attachment_id );

wp_publish_post( $post_id );
clean_post_cache( $post_id );

Image sizes wrong keys and wrong dimensions

Hi, I'm interested in this plugin but I'm finding that it's not assigning the proper keys to image sizes.

Notice in the attached image that the "full"/original file is listed under "medium-large" and that the "large" dimensions are incorrect (actual dims in url).

That "sizes" object is inside a 'acf gallery' inside a 'acf flexible content' field.

image

ACF Image size

Can we somehow output Custom Image Size, so its not original (image) url in Output of ACF?

Writing ACF fields to WP API without clicking publish button

I’m having a problem displaying the ACF fields in the api from dynamically created posts using wp_insert_post()

Here is the function I'm using to import the data as a custom post type:

function mysite_import_json() {
  $json_feed = 'http://local.mysite.com/wp-content/test.json';
  $json      = file_get_contents( $json_feed );
  $objs      = json_decode( $json, true );
  $wp_error  = true;
  $post_id   = - 1;

foreach ( $objs as $obj ) {
    $title   = $obj['title'];
    $meta1  = $obj['taxonomy'][0];
    $meta2     = $obj['nom'];
    $meta3  = $obj['prenom'];
    $d       = new DateTime();
    $d->setTimestamp( $obj['created'] );
    $date_created = $d->format( 'Y-m-d H:i:s' );
    $post_meta    = array(
        'meta_1'        => $meta1,
        'meta_2'        => $meta2,
        'meta_3'        => $meta3,
    );

    $post_data = array(
        'post_title'  => $title,
        'post_date'   => $date_created,
        'post_status' => 'publish',
        'post_type'   => 'cpt',
        'meta_input'  => $post_meta,
    );

    if ( null === get_page_by_title( $title, 'object', 'message' ) ) {
        $post_id = wp_insert_post( $post_data, $wp_error );
        foreach ( $post_meta as $key => $value ) {
               update_field( $key, $value, $post_id );
        }
    } else {
        $post_id = - 2;
    }
  }
}

add_action( 'after_setup_theme', 'mysite_import_json' );

I’m guessing I need to make the WP API writable to the ACF fields. They do display once I click the Publish button though but there are too many posts to do this.

acf-to-wp-api + jp-rest-cache not working

I'm testing out https://github.com/bkirby989/jp-rest-cache (the version that @bkirby989 provided) together with this plugin – and I found that the custom fields does not get added when using the cache plugin.

jp-rest-cache uses filter rest_pre_dispatch while ​_acf-to-wp-api_​ uses filter rest_api_init. Does someone have a clue why the response that ​_jp-rest-cache_​ gives doesn't include the custom fields?

ACF data for 'Post Object' field

I'd like to display ACF data for the 'Post Object' field in the wp-api. At the moment it just spits out the basic post data.

acf: {
  post-object: [
    {
      ID: 57,
      post_author: "1",
      post_date: "2016-05-16 07:25:34",
      post_date_gmt: "2016-05-16 07:25:34",
    }
  ]
},

This is what I want....

acf: {
  post-object: [
    {
      ID: 57,
      post_author: "1",
      post_date: "2016-05-16 07:25:34",
      post_date_gmt: "2016-05-16 07:25:34",
      acf: {
        some-data: 'Some data'
      }
    }
  ]
},

I guess I'd have to account for a potential infinite loop of referenced post objects. Anyone have any insight into how I can modify acf-to-wp-api to accomplish this?

ACF not showing in Custom Post Type

Hello
Thanks for this great plugin.
I'm having trouble retrieving the acf fields in my cpt response.
Currently im making this call:
GET http://website.com/wp-json/wp/v2/locals/id
the "acf" field is not displaying.

This is what I added in my functions.php

add_action( 'init', 'my_custom_post_type_rest_support', 25 );

function my_custom_post_type_rest_support() {
    global $wp_post_types;
    $post_type_name = 'locals';
    if( isset( $wp_post_types[ $post_type_name ] ) ) {
        $wp_post_types[$post_type_name]->show_in_rest = true;
        $wp_post_types[$post_type_name]->rest_base = $post_type_name;
        $wp_post_types[$post_type_name]->rest_controller_class = 'WP_REST_Posts_Controller';
    }
}

Any suggestion?

Possible to group ACF output by ACF field group

Is it possible to group fields in a post's JSON by the ACF field group? When I request my post's JSON through wp-api i get a flat list of fields underneath acf. I need something grouped so i can determine what module to display in my front-end but also there is a practical concern of fields from different field groups with the same name overriding each other in the output.

/posts, /pages, /options coming up 500

/wp-json pulls up all of the routes, but /posts, /pages, /options are coming up 500 when I have this plugin installed.

Some discussion over here, might pertain: WP-API/WP-API#475

Seems like a few days ago this started happening.

Any ideas?

Nested ACF fields

Hello! Was wondering if it was possible to get nested ACF fields within a repeater or otherwise. And if possible, how many levels of nesting could one go before it became ludicrous?

Parse error in version 1.3.0

Parse error: syntax error, unexpected '[' in acf-to-wp-api/acf-to-wp-api.php on line 403

Fatal error after updating acf-to-wp-api from version 1.2.1 to 1.3.0; breaks WordPress build until plugin files are removed, or if installing fresh, prevents plugin from being activated. No other plugins updated or changed.

ACF to WP-API 1.3.0
WP-API 1.2.3
WordPress 4.3

Other plugins installed: ACF Pro 5.3.0, Custom Post Type UI 1.1.2

Filter by ACF fields value

Hi and thanks for this very helpfull plugin!!!
I’m facing an issue with the filtering of posts by custom fields value, using angular. Maybe you can help me?
I thought I could easily do it using:

../wp-json/posts/?type=some_custom_type&filter[X]=Y

But I cant get it to work. Do you know a way to do it?
Many thanks

post data back into acf?

I am building an ios client for the WP API, I am able to read the values from the 'acf' key that comes with the post without any issues. What I am trying to figure out is how to have wordpress save the updates I send back on the custom fields.

Do you have any idea or clue of what filters would allow me to iterate over the items on the acf array and save their updated values?

Order by ACF Date field on custom post

Hi,

i tried with the following but it's not working at all:
/wp-json/wp/v2/movie?filter[movie_type]=library&filter[orderby]=meta_value_num&filter[meta_key]=data_uscita&filter[order]=DESC

where data_uscita is my ACF Date field and library the custom taxonomy term of the custom post movie.

I read the readme page and tested the code in Issue #13 but unfortunately with no results.

Can you help me?

Thanks,
Andrea

WP-API version number - Does it work in the Wp-APi v1?

Hello all,

I am working in project where I am retrieving custom post type data from a wordpress site and publishing it into another.

I have a WP-API v1 working nicely, although I did create a ACF and I need to retrieve it too.

Does this plugging is able to work with V1 of WP-API ?

I tried to use V2 although the implementation to retrieve custom post type is kind of confusing.

Thanks ahead.

Pages and custom post types issue with WP-API v2

I need to do more digging into this, but v2.0-beta4 doesn't seem to handle custom post types very well, so no ACF data comes through there yet.

This doesn't affect users with version 1.x of WP-APi.

Fatal error: uncaught error – can some help me to solve this issue?

Fatal error: Uncaught Error: Call to undefined function get_field() in /home/.sites/869/site4366094/web/wavelife/wp-content/themes/proformat/functions.php:340 Stack trace: #0 /home/.sites/869/site4366094/web/wavelife/wp-settings.php(479): include() #1 /home/.sites/869/site4366094/web/wavelife/wp-config.php(90): require_once('/home/.sites/86...') #2 /home/.sites/869/site4366094/web/wavelife/wp-load.php(37): require_once('/home/.sites/86...') #3 /home/.sites/869/site4366094/web/wavelife/wp-blog-header.php(13): require_once('/home/.sites/86...') #4 /home/.sites/869/site4366094/web/wavelife/index.php(17): require('/home/.sites/86...') #5 {main} thrown in /home/.sites/869/site4366094/web/wavelife/wp-content/themes/proformat/functions.php on line 340

PHP notice about undefined constant (v1.3.3)

I have v1.3.3 of this plugin installed.
I also have WP REST API plugin v2.0beta7 and Wordpress core v4.3

On every page there is a PHP error about an undefined constant.

Notice: Use of undefined constant REST_API_VERSION - assumed 'REST_API_VERSION' in /var/www/courtdocs/public/wp-content/plugins/acf-to-wp-api/acf-to-wp-api.php on line 41

How can I search in WP-API for a custom field

like we are searching for any post title as

wp-json/wp/v2/posts/?filter[name]=

Then how to search for acf

I tried
wp-json/wp/v2/posts/?filter[meta_key]=field_name&filter[meta_value]=filter_value

But no success.... Can you please help???

"acf" key set to false with Advanced Custom Fields PRO

The plugin works great with the standard Advanced Custom Fields plugin, however as soon as I enable Advanced Custom Fields PRO the "acf" key on each object is set to false. Deactivating the Advanced Custom Fields PRO plugin restores all of the data within the "acf" key.

I have input my serial number for Advanced Custom Fields PRO and the plugin is working as expected throughout the rest of my site.

I have looked through the code for ACF to WP API and Advanced Custom Fields PRO but as yet I have not spotted any obvious cause for this behaviour.

I have the following additional code in my theme's functions.php (note to self: move this code to a plugin)

function allow_meta( $valid_vars ) {
  $valid_vars = array_merge( $valid_vars, array( 'meta_key', 'meta_value' ) );
  return $valid_vars;
}
add_filter( 'rest_query_vars', 'allow_meta' );

My installation is as follows:

  • PHP 7.0.5-3+donate.sury.org~wily+1
  • MySQL 5.6.17
  • Wordpress 4.5.2
  • Advanced Custom Fields 4.4.7
  • Advanced Custom Fields PRO 5.3.8.1
  • ACF to WP API 1.3.3

Suggestion: Add a CONTRIBUTING.md file or equivalent section to README.md

In order to improve the quality of issues in the queue, it might help to have a "CONTRIBUTING.md" file, or a section in the README outlining how to best request help. It seems it would be particularly helpful if issue reporters supply the ACF config when submitting bugs — several recent issues have closed due to the reporter not following up with their config, and while that points to a fault on their end, it makes it hard to investigate whether it's actually an issue with acf2wpapi.

Different type of response

Hi! Thanks for plugin!

I want get url for category with ACF to WP API, but have problem. When data exist, plugin send through JSON - dictionary, when not - bool value. How it can fix? I need response with dictionary in both situation

{
    ID = 178;
    acf =     {
        imageforcategory = "http://xxx/wp-content/uploads/2015/09/image.jpg";
    };
 {
        ID = 24;
        acf = 0;

404 on Wordpress 4.7.4

I've seen in the README only supported up to 4.7.3
But letting you know i get a 404 when clicking on Activate on WP 4.7.4
No errors in the logs.

How can I update a post in rest with their ACF?

Hi,

First of all, very good work with this plugin Chris!
I have read some topics about that but have you find a solution about how create a post in rest with their advanced custom field?
Is it a WP REST API "problem" or can I fix it into your script?

I'am using WP REST API v2.0-beta8, ACF to WP-API 1.3.2 and CPT UI.

Thx
Réda.

Retrieve ACF for custom post in WP REST API V2

I can't seem to retrieve ACF for a custom post type of mine, using v2 of the REST API plugin. I have included 'show_in_rest' in the registration of my custom posts, and I can retrieve an individual custom post at /wp-json/wp/v2/post-type-slug/{id} but then there is no ACF endpoint it seems. The description of this plugin says you should be able to retrieve ACF for custom post types, can you offer me a pointer on what I need to do? Thank you very much

Filtering of option fields

Great work on the plugin. I was wondering if it would be worth having a way to filter the option fields, like you do with the standard rest_prepare_x. Maybe adding the filter to addACFOptionRouteV2cb

return apply_filters( 'rest_prepare_acf_options', $request, $fields, $option );

Allowing you to do:

function remove_private_options($request, $fields, $option) {

    $private_prefix = '_private_';

    if ( substr($option, 0, strlen($private_prefix) ) === $private_prefix )
    {
        return false;
    }

    foreach($fields as $name => $value)
    {
        if ( substr($name, 0, strlen($private_prefix) ) === $private_prefix )
        {
            unset($fields[$name]);
        }
    }

    return $fields;
}

Above is an example of removing any field in the options with a prefix of _private_

I can't access to the data from a "Flexible Content" ACF json object.

Hi,
I'm trying your plugin but I can't access to the data from a "Flexible Content" ACF json object. I can console.log the filelds but can't show totally them in the view.

{{ product.meta }} or {{ product.meta }} returns the array I need, but I can't deep more. Once there, how can I access to the keys/values from that array?

thanks for this great plugin!,
s.
captura de pantalla 2015-05-14 a las 20 05 56

Media attachment meta output issue when used with ACF Pro Gallery !?

Hi there,
firstly thank you very much for this awesome plugin. It generally works like a charme and makes working with acf custom fields and the REST API so comfortable!

I might have noticed an issue. Correct me if I am wrong ;)

I am using ACF Pro and added a gallery field:

1

In the next step I added media attachment meta fields that appear for every image uploaded to a gallery. Just for you to understand the intention: Those are two dropdown fields that can set the horizontal and vertical focus point position for an image that is used as a background cover on the frontend (responsive design).

2

So far so good... But here comes the issue: on the frontend side this information is only displayed once within the [acf][bg-showcase-images] node ('bg-showcase-images' is the gallery field-name as you can see on the first screenshot).

But as the meta value can be set individually for each image uploaded to an acf gallery field, I expected it to appear for each image entry of the gallery ([acf][bg-showcase-images][0], [bg-showcase-images][1], [bg-showcase-images][2], ...) which is sadly not the case. In the following screenshot you can see that the attachment meta values img_focus_x and img_focus_y are only once within [acf][bg-showcase-images] and that they don't appear for each image within the gallery:

3

Am I doing something wrong, missing something or is the script not working correctly for this special occasion?

Looking forward to read about your thoughts.

Cheers,
Tim

Custom Post Types

I'm using V2 of the WP-API and I'm trying to get my custom post types. I'm using Version: 1.3.2 of acf-to-wp-api.

I tried making a request with Postman to this endpoint...

http://localhost:8888/wp-json/wp/v2/car

With 'car' being my custom post type of course. But I get this response...

[
    {
        "code": "rest_no_route",
        "message": "No route was found matching the URL and request method",
        "data": {
            "status": 404
        }
    }
]

I found this blog post that goes through setting up getting custom post types with WP-API V2 (https://bay-a.co.uk/wordpress-tips/wp-api-v2-tips/)

When I added this to my functions.php in my theme, I could get my custom post type response.

add_action( 'init', 'add_events_to_json_api', 30 );
function add_events_to_json_api(){

    global $wp_post_types;
    $wp_post_types['car']->show_in_rest = true;
    $wp_post_types['car']->rest_base = 'car';
    $wp_post_types['car']->rest_controller_class = 'WP_REST_Posts_Controller';
}

I looked through the plugin code and I can see where custom post types are registered, but it doesn't seem to be working for me. Maybe my url is incorrect?

Any help would be awesome! Thanks!

ACF Image size

Hi.
First of all, thank you for creating this plugin!

At the moment, when i have a image field in acf , only the full size source is given to the json.
Should urls for all image sizes be added to the json?

Maybe with all the fields that wp-api usually gives you with an attachment?

ACF false for Archive of custom post type

I have two custom post types. One is displaying acf property on both single and archive endpoints.
The other is showing acf only on single endpoint.
They're almost identical in register_post_type.

No acf field in custom taxonomies

I had the issue where custom taxonomies weren't showing the 'acf' field to retrieve data from custom fields.

Would you be able to patch your plugin to include this at some point.

Other than that, it's great plugin. 🙌🏻

I've added a suggested temporary fix in place for anyone with a similar problem:

$types = get_taxonomies(array( 'public' => true, '_builtin' => false )); foreach($types as $key => $type) { register_rest_field( $type, 'acf', array( 'get_callback' => array( $this, 'addACFDataTermV2cb' ), 'update_callback' => null, 'schema' => null, ) ); }

Call to undefined function get_fields()

I installed your plugin in wordpress. unfortunately i get this error


Fatal error: Call to undefined function get_fields() in /var/www/web1205/html/MerseburgerApp/wp-content/plugins/acf-to-wp-api/acf-to-wp-api.php on line 57

My url is .../wp-json/posts?type=tribe_events

Thanks,

Martin

Update 1 item in array without erase everything else

Hi, how can i update salary for "Charlotte Doe" without erase "Chris Doe" and without send in all information for both Employees?

[
  {
    "company_id": 3,
    "company_info": {
      "active": true,
      "company_employees": [
        {
          "id": "1",
          "name": "Charlotte Doe",
          "salary": "4500"
        },
        {
          "id": "2",
          "name": "Chris Doe",
          "salary": "5200"
        }
      ]
    }
  }
]

Consider integration tests for your WP REST API endpoints

Hi!

On behalf of Pantheon, I'm writing a tutorial on why integration tests are critical for your WP REST API endpoints, and how you can go about writing them.

Given your use of register_rest_field(), I think you could benefit from reading and applying the tutorial. And, because the tutorial isn't yet published, I'd very much appreciate any constructive criticism you have, so that we can make the tutorial an evergreen resource for the community.

Thanks in advance for your time. Happy to take any questions, comments, etc. you might have in this Github issue, or on the document itself (you'll need to be signed into Dropbox).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.