airesvsg / acf-to-rest-api-example Goto Github PK
View Code? Open in Web Editor NEWExample for the plugin ACF to REST API
Home Page: http://airesgoncalves.com.br/acf-to-rest-api
Example for the plugin ACF to REST API
Home Page: http://airesgoncalves.com.br/acf-to-rest-api
Hi,
First of all, I'd like to say thanks for this really great plugin, have been using it a lot lately! :)
I have an issue though, trying to add a new field to a repeater vith angulajs: http://prntscr.com/h3uoy6
Here I have a few fields added already to the repeater: http://dev.imok.biz/mikulas/wp-json/acf/v3/pages/5/mikulas
Would like to add more, like so:
$http({ method: 'POST', url: CONFIG.site_url + 'wp-json/acf/v3/pages/5/', data: dataa, headers: { Accept: "application/json", Authorization: 'Bearer ' + user.token } }).then(function (response) { console.log('created ', response); }, function (error) { console.log('error udpated ', error); });
For the dataa I have tried:
var dataa = form.mikulas: { user: 'na', email: '[email protected]' }
or just simply
var dataa = { user: 'na', email: '[email protected]' }
Also tried to change the URL to ... pages/5/mikulas
Whatever I do, I get this response (the login process is OK, I can create new POSTs, etc):
code: "cant_update_item"
status: 500
statusText: "Internal Server Error"
Thanks a bunch!
This is kind of a strange occurrence. I have a custom post type in Wordpress that has a group of custom fields associated to it.
I have a custom importer that I made that utilized wp_insert_post() function to insert values from a web service (json) into the custom post type.
When I run the importer, ACF is storing the data in a certain but it does seem that all connections are there. When I go into the wp-admin, edit my custom post type, i see all of the data listed there when i edit a post.
When I look at the REST API endpoint the value "{acf: false}" is displayed so I can't get the values from the ACF fields. This contunues to happen after I export the database and import it into another system, like my localdev environment. So it seems moving the database can re-create the issue.
One way to fix the issue is to go run through and resave every post. There are thousands of entries so we can't keep doing that.
Do you have any ideas what might be causing this?
Wouldn't be easier to just use ACF frontend form and change the action of the submit button?
https://www.advancedcustomfields.com/resources/acf-pre_save_post/
This will enable you not to have code every field into index.php
Hello ,I'm having problems using PUT methods to update the option fields , my function is like below :
I'm getting this error:
[code] => cant_update_item
[message] => Cannot update item
[data] => stdClass Object
(
[status] => 500
)
I used GET Method and everything is OK , All other API call are all right .
Regards.
I'm trying to update a media ACF field. I know that I'm properly authenticated because I'm able to update post fields in my other ajax calls. For some reason, I cannot get my media field to update. I continue to get the 500 Internal Server error with a response of cant_update_item
.
Here is my call:
var data = {
"fields": {
name: value,
},
};
var url = document.location.origin + '/wp-json/acf/v2/media/' + mediaId + '/' + name;
return $.ajax({
method: 'PUT',
data: data,
dataType: 'json',
url: url,
beforeSend: function(xhr) { xhr.setRequestHeader('X-WP-Nonce', WP_API_Settings.nonce);},
});
I'm using ACF to REST API version 2.2.1 and ACF PRO version 5.5.9.
Any thoughts?
Just add the following to style.css to have RTL support:
<style>
.rtl .nav{padding-right:0;}
.rtl .nav-tabs>li {
float: right;
}
.rtl .radio-inline input[type="radio"],
.rtl .checkbox-inline input[type="checkbox"] {
margin: 4px -20px 0 0;
}
.rtl .checkbox-inline+.checkbox-inline {
margin-right: 10px;
}
.rtl .input-group { direction:ltr; }
</style>
and on line #39 use pull-left
<ul class="nav nav-pills pull-left">
Hello tried using the same user (id = 4) and also an Admin
using end point
POST or PUT
/wp-json/acf/v2/user/4
payload sended (ragione is the custom field associated to the user):
fields: {
ragione: "pippo"
}
I'm sending also the nonce
I always got this error
(it works good updating post acf fields)
{
code: "rest_forbidden",
data : {
status: 403
},
message: "You don't have permission to do this."
}
thank you very much!
Hello.
I explain the problem better . I created a custom field with a repeater inside a date- picker field ..
<?php
$f_r1a = get_field_object( 'field_xxxxx', $u_id );
$f_r1 = get_field( 'field_xxxxxx', $u_id );
if ( ! $f_r1 ):
$f_r1 = array(
'nome' => "",
'cognome' => "",
'sesso' => "",
'relazione' => "",
'carico' => "",
'd_nascita' => "",
);
endif; ?>
<div class="repeater">
<?php foreach ( $f_r1 as $k => $v ) : ?>
<div class="item">
<div class="form-group">
<div class="col-md-2">
<input type="text" name="fields[<?php echo $f_r1a['name']; ?>][<?php echo absint( $k ); ?>][nome]" class="form-control" value="<?php echo esc_attr( $v['nome'] ); ?>">
</div>
<div class="col-md-2">
<input type="text" name="fields[<?php echo $f_r1a['name']; ?>][<?php echo absint( $k ); ?>][cognome]" class="form-control" value="<?php echo esc_attr( $v['cognome'] ); ?>">
</div>
<div class="col-md-2">
<select class="form-control input-sm" name="fields[<?php echo $f_r1a['name']; ?>][<?php echo absint( $k ); ?>][sesso]">
<option value="m" <?php selected( $v['sesso'], 'M' ); ?>>Maschio</option>
<option value="f" <?php selected( $v['sesso'], 'F' ); ?>>Femmina</option>
</select>
</div>
<div class="col-md-2">
<select class="form-control input-sm" name="fields[<?php echo $f_r1a['name']; ?>][<?php echo absint( $k ); ?>][relazione]">
<option value="Capofamiglia" <?php selected( $v['relazione'], 'Capofamiglia' ); ?>>Capofamiglia</option>
<option value="Coniuge" <?php selected( $v['relazione'], 'Coniuge' ); ?>>Coniuge</option>
<option value="Figlio/a" <?php selected( $v['relazione'], 'Figlio/a' ); ?>>Figlio/a</option>
<option value="Altro Convinente" <?php selected( $v['relazione'], 'Altro Convinente' ); ?>>Altro Convinente</option>
</select>
</div>
<div class="col-md-2">
<select class="form-control input-sm" name="fields[<?php echo $f_r1a['name']; ?>][<?php echo absint( $k ); ?>][carico]">
<option value="si" <?php selected( $v['carico'], 'Si' ); ?>>Si</option>
<option value="no" <?php selected( $v['carico'], 'No' ); ?>>No</option>
</select>
</div>
<div class="col-md-2">
<input type="text" name="fields[<?php echo $f_r1a['name']; ?>][<?php echo absint( $k ); ?>][d_nascita]" value="<?php echo esc_attr( $v['d_nascita'] ); ?>" class="form-control datepicker" id="acf-date-picker">
</div>
<button type="button" class="btn btn-danger remove-row" style="float: right; z-index: 1000; position: absolute;"><i class="fa fa-times" aria-hidden="true"></i></button>
<div class="clearfix"></div>
<hr>
</div>
</div>
<?php endforeach; ?>
<button type="button" class="btn btn-primary add-row">Aggiungi Componente</button>
</div>
</div>
if ( $.isFunction( $( '.datepicker' ).datepicker ) ) {
$( '.datepicker' ).datepicker( {
format: 'dd/mm/yyyy',
autoclose: true
} );
}
The problem is that when I go to update data from frontend date is sent in dd / mm / yyyy format and not as it should be yyyymmdd for aggioranre the acf date fields .
Hey! This is not an issue, but a question out of interest:
You defined the function _checked() in functions.php and use it multiple times in index.php.
But sometimes you use it with leading underscore and sometimes without.
I didn't even know you could do that and wanted to ask why you did it! :)
First thing I found was this stackoverflow-discussion where they said it's for private methods and shouldn't be used...
Don't want to discuss here - just interested in why you used it and why once with and once without the underscore! :)
Hello, love you plugin, great job! I have one question - how can I add more rows to repeater field without having to delete old ones. In your demo file now what happends is that you first take the data from db, push to this data array new values and send to API updated array. Is there i way just to add data without sendig huge array each time? Thank you!
WP version: 4.9.8
ACF version: 5.7.2
Could someone, please, help me?
I've tried for days update custom fields but I'm getting the following error:
Console Log:
jquery.js:9600 POST http://vendedor-confiavel.local/wp-json/acf/v3/vendedor/128 500 (Internal Server Error)
send @ jquery.js:9600
ajax @ jquery.js:9206
updateProfile @ LoginScreenForm.js:51
dispatch @ jquery.js:5183
elemData.handle @ jquery.js:4991
I've also installed the Basic Auth plugin.
If I replace the url with http://vendedor-confiavel.local/wp-json/wp/v2/vendedor/128 only the title is updated, not the field "whatsapp".
Console Log:
http://vendedor-confiavel.local/wp-json/wp/v2/vendedor/128
LoginScreenForm.js:61 {id: 128, date: "2018-08-24T01:28:09", date_gmt: "2018-08-24T01:28:09", guid: {…}, modified: "2018-08-28T15:32:15", …}acf: facebook: nullwhatsapp: null__proto__: Objectcontent: {raw: "", rendered: "", protected: false}date: "2018-08-24T01:28:09"date_gmt: "2018-08-24T01:28:09"guid: {rendered: "http://vendedor-confiavel.local/vendedor/128/", raw: "http://vendedor-confiavel.local/vendedor/128/"}id: 128link: "http://vendedor-confiavel.local/vendedor/128/"modified: "2018-08-28T15:32:15"modified_gmt: "2018-08-28T15:32:15"password: ""slug: "128"status: "publish"template: ""title: {raw: "Alex Silva", rendered: "Alex Silva"}type: "vendedor"_links: {self: Array(1), collection: Array(1), about: Array(1), wp:attachment: Array(1), wp:action-publish: Array(1), …}proto: Object
LoginScreenForm.js:62 Updated
Code:
my.js
class LoginScreenForm{
constructor(){
this.events();
}
events(){
$(".save-button").on("click", this.updateProfile);
}
updateProfile(e){
var thisProfile = $(e.target);
var ourUpdatedPost = {
"title" : "Alex Silva",
"acf" : {
"whatsapp" : "12988998591"
}
};
var myUrl = vcData.root_url + '/wp-json/acf/v3/vendedor/' + thisProfile.data('id');
console.log(myUrl);
$.ajax({
beforeSend: (xhr) => {
xhr.setRequestHeader('X-WP-Nonce', vcData.nonce);
},
url: myUrl ,
type: 'POST',
data: ourUpdatedPost,
success: (response) => {
console.log(response);
console.log("Updated");
},
error: (response) => {
console.log(response);
console.log("Error");
}
});
}
}
functions.php
// Enable the option show in rest
add_filter( 'acf/rest_api/field_settings/show_in_rest', '__return_true' );
// Enable the option edit in rest
add_filter( 'acf/rest_api/field_settings/edit_in_rest', '__return_true' );
add_filter( 'acf/rest_api/item_permissions/get', function( $permission ) {
return current_user_can( 'edit_posts' );
} );
add_filter( 'acf/location/rule_match/options_page', function(){
return true;
} );
add_filter( 'acf/rest_api/option/prepare_item', function( $item ){
if ( isset( $item['id'] ) ) {
$item['id'] = 'options';
}
return $item;
});
This spinet
add_filter( 'acf/location/rule_match/options_page', function(){
return true;
} );
add_filter( 'acf/rest_api/option/prepare_item', function( $item ){
if ( isset( $item['id'] ) ) {
$item['id'] = 'options';
}
return $item;
});
is adding the theme options in all pages . How to fix it please .
I have add a coment in this issues
PUT request not working using Oauth2 and acf Pro
The example only shows PUT how to modify an exiting element.
The line
<form action="<?php echo esc_url( $url ); ?>" method="<?php echo $edit ? 'PUT' : 'POST'; ?>">
I modified the $edit
variable to false (POST).
It does not seem to work.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.