-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't update user custom field #1
Comments
If I try with another ADMIN user (or changing the same user role as EDITOR ) I got a different error: {
code: "cant_update_item",
data : {
status: 500
},
message: "Cannot update item"
} |
Hi @gino8080, In my example I used the method POST, but I should have used the method PUT to update the ACF fields. See below each method and theirs actions.
Please, can you send me a dump of your database? Thanks |
Hello Aire, thank you for fast answer! just tried with PUT and same results :( Sent my dump (just removed some personal infos) waiting for you! |
Hi @gino8080 , Thanks |
sure, I'm using http angular , the nonce is in the interceptor request calling GET /users/me path works REQUEST: RESPONSE OK Calling the PUT or POST to /wp-json/acf/v2/user/ID (my logged ID user) Does NOT work :( REQUEST //the nonce is added to headers in the interceptors
$http({
method: "PUT", //tried also POST
url: '/traipler-web/wp-json/acf/v2/user/1', //1 is my logged id user
data: fields: {
work: "Developer" //work is the acf field name
}
}) PUT /traipler-web/wp-json/acf/v2/user/1 HTTP/1.1 RESPONSE KO don't know why |
Hi @gino8080, Follow the example below: $http( {
method: "PUT",
url: '/traipler-web/wp-json/acf/v2/user/1',
// headers: { 'X-WP-Nonce' : WP_API_Settings.nonce },
data: {
"fields": {
"work": "Developer"
}
}
} ) |
hello good aire, Tried formatting the request data as you suggested..but nothing :( So I tried using your example php (so just jquery ajax) update a User acf field NOT! this is the simple html and javascript used <form id="myform" action="http://localhost:8888/traipler-web/wp-json/acf/v2/user/1" method="POST">
<input type="text" name="fields[work]" id="acf-title">
<button type="submit">Save</button>
</form> jQuery(document).ready(function($) {
$('#myform').on("submit", function(e) {
e.preventDefault();
var _this = $(this);
var url = _this.attr('action');
var data = _this.serializeArray();
var btn = _this.find('button[type="submit"]');
var modal = $('#modalResponse');
$.ajax({
url: url,
method: 'POST',
beforeSend: function(xhr) {
xhr.setRequestHeader('X-WP-Nonce', myLocalized.nonce);
},
data: data,
dataType: 'json'
}).always(function(data) {
btn.removeProp('disabled');
modal.find('.modal-body').html('<pre>' + JSON.stringify(data, null, "\t") + '</pre>');
modal.modal('show');
});
return false;
});
}); error: 500 please can you give me a working user acf editing example? tank you very much! |
Hi @gino8080, please, add this filter in your functions.php add_filter( 'acf/rest_api/item_permissions/update', function( $permission, $request, $type ) {
if ( 'user' == $type && method_exists( $request, 'get_param' ) && get_current_user_id() == $request->get_param( 'id' ) ) {
return true;
}
return $permission;
}, 10, 3 ); Example: gino.zip Instructions:
define( 'WP_HOME', 'http://localhost:8888/traipler-web' );
define( 'WP_SITEURL', 'http://localhost:8888/traipler-web' ); Thanks |
First thank you for your support airesvsg !! these are the feedbacks: using your wp setup example it works BUT:when I call these endpoints (this happens on both your and mine wp setup):
(Either with the ajax or going directly using the browser) with my wp setup
So the questions are:
thank you very much!! |
ok i can confirm that the problem is acf pro, tested online on staging server, exactly same setup, just swapped ACF PRO (v5.3.3.2 ) with ACF FREE (v4.4.5 ) and it works Maybe the v5 has some breaking changes ? |
Ok @airesvsg I got it! To EDIT a custom field on a USER Current User -- is equal to -- Logged In using the others User location rules : User Form / User Role was not enought! thank you very much for your great support! you can close this now! |
Hi @gino8080, Sorry, I can't answer before. Ok, I'll test this rule and put on readme. Thanks |
I have the same issue with Advanced Custom Fields PRO 5.4.8: for example - acf rest returns correct data for products https://subdomain.domain.at/wp-json/acf/v2/post/3656 it returns: {"acf":{"product_manual":"abc.pdf","product_youtube_url":"dsadasdasdas"}} now I'm trying to post costom fields It did not help to change "Current User -- is equal to -- Logged In" (see above) Thank you in advance |
I think we found some interesting thing: (1) Access Control Headers: to get : Access-Control-Allow-Methods:POST, GET, OPTIONS, PUT, DELETE (2) since October 07, 2016: WordPress REST API (Version 2) https://de.wordpress.org/plugins/rest-api/changelog/ 2.0 Beta 15.0 (October 07, 2016) Introduce support for Post Meta, Term Meta, User Meta, and Comment Meta in their parent endpoints. For your meta fields to be exposed in the REST API, you need to register them. WordPress includes a register_meta() function which is not usually required to get/set fields, but is required for API support. To register your field, simply call register_meta and set the show_in_rest flag to true. Note: register_meta must be called separately for each meta key. Introduce Settings endpoint. Expose options to the REST API with the register_setting() function, by passing $args = array( 'show_in_rest' => true ). Note: WordPress 4.7 is required. See changeset [38635][https://core.trac.wordpress.org/changeset/38635]. |
Hello @airesvsg first thanks for awesome plugin. i'm having issue to update user fields Here is details what we have in our admin panel : ACF to REST API --- Version 3.1.0 This is htaccess link : https://url.upwork.com/_01Hp0Ddp5lbjtnkVudB_RqKfMK5hx8O-5K End point : https://example.com/wp-json/acf/v3/users/1 Body: Response : What could be the issue as i have tried your every thread regarding this issue but no luck yet. Please help regarding this. |
I have this problem :( |
Hi, Please read about authentication, it's necessary to create or update. Thanks |
Be sure "Location" section in the "Edit Field Group" page of the Custom Field admin allows access to the user or role your API calls are made as. It took me a while to find this solution. |
For anyone having similar problem to @gino8080 Here is my workaround; // Allow current user to update own profile
add_filter( 'acf/rest_api/item_permissions/update', function( $permission, $request, $type ) {
if ( 'user' == $type && method_exists( $request, 'get_param' ) && get_current_user_id() == $request->get_param( 'id' ) ) {
return true;
}
return $permission;
}, 10, 3 );
// Allow user acf rest user update to be detected as location "user_form".
add_filter( 'acf/location/screen', function( $screen, $deprecated ) {
if(!empty($screen['post_id']) && preg_match("/^user_\d+$/i",$screen['post_id'])){
$screen = wp_parse_args($screen, array(
'user_form' => 'edit'
));
}
return $screen;
}, 10, 2 ); Set field location to: User Form = all or edit Example request |
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):
I'm sending also the nonce
I always got this error
(it works good updating post acf fields)
thank you very much!
The text was updated successfully, but these errors were encountered: