acf-to-rest-api icon indicating copy to clipboard operation
acf-to-rest-api copied to clipboard

ACF fields for user on registration + updating user details

Open mattprudente opened this issue 7 years ago • 8 comments

ACF to Rest v3.0.1-beta WP OAuth Server - Pro v3.3.8 WP v4.7.5 AngularJS v1

I have managed to authorise creating of the user and also for POST/PUT to the users ACF field but it isn't actually saving anything. I am using a taxonomy field that saves the Term ID. I POST a term ID to the /wp-json/acf/v3/users/{id} endpoint with data: { "fields": { "field": termID}}. It returns a success but either doesn't create the data for the registering user OR update data for an exisiting user when they edit their profile.

Please advise! Thanks you.

mattprudente avatar May 25 '17 14:05 mattprudente

Hey Matt,

I found the solution creating first the user them in the callback of it updating the ACF fields. The point is: It's not possible do the two actions at the same time.

Did you solve this by another way?

danilopaulinodasilva avatar Jan 22 '18 17:01 danilopaulinodasilva

To fix this with ACF5 i changed the code as follow:

In https://github.com/airesvsg/acf-to-rest-api/blob/99c7731e3cc1443a9dd7af98370db0ec228d7b28/v3/lib/class-acf-to-rest-api-acf-api.php#L64 add:

if(is_string($this->id)){
	$this->id = preg_replace("/[^0-9,.]/", "", $this->id );
}

And replace https://github.com/airesvsg/acf-to-rest-api/blob/99c7731e3cc1443a9dd7af98370db0ec228d7b28/v3/lib/class-acf-to-rest-api-acf-api.php#L133 with:

if ( strpos( $id, 'user_' ) !== false ) {
	$filter = array('user_id' => str_replace( 'user_', '', $id ) );
	$user = get_userdata( $filter["user_id"] );
	if( ! $user || ! $user->exists() || empty($user->roles) || !user_can($user->ID, $user->roles[0])){
		$filter["user_id"] = "new";
	}
} else {
	$filter = array( 'post_id' => $id );
}
$field_groups = acf_get_field_groups( $filter );

ghost avatar Feb 16 '18 16:02 ghost

@ghost You absolutely saved my ass on this one! Fantastic! This was able to work for me for the node-wpapi configuration!

apiPromise.then((site) => {
  site.users().me().update({
    fields:{
      stocks: newMeta
    }
  }).then(function( response ) {
    console.log( response );
  }).catch((err) => {
    console.log( err );
  });
});

99wesley99 avatar Mar 05 '19 17:03 99wesley99

To fix this with ACF5 i changed the code as follow:

In

https://github.com/airesvsg/acf-to-rest-api/blob/99c7731e3cc1443a9dd7af98370db0ec228d7b28/v3/lib/class-acf-to-rest-api-acf-api.php#L64

add:

if(is_string($this->id)){
	$this->id = preg_replace("/[^0-9,.]/", "", $this->id );
}

And replace

https://github.com/airesvsg/acf-to-rest-api/blob/99c7731e3cc1443a9dd7af98370db0ec228d7b28/v3/lib/class-acf-to-rest-api-acf-api.php#L133

with:

if ( strpos( $id, 'user_' ) !== false ) {
	$filter = array('user_id' => str_replace( 'user_', '', $id ) );
	$user = get_userdata( $filter["user_id"] );
	if( ! $user || ! $user->exists() || empty($user->roles) || !user_can($user->ID, $user->roles[0])){
		$filter["user_id"] = "new";
	}
} else {
	$filter = array( 'post_id' => $id );
}
$field_groups = acf_get_field_groups( $filter );

THANKS!

bisk8s avatar Jul 22 '20 15:07 bisk8s

To fix this with ACF5 i changed the code as follow:

In

https://github.com/airesvsg/acf-to-rest-api/blob/99c7731e3cc1443a9dd7af98370db0ec228d7b28/v3/lib/class-acf-to-rest-api-acf-api.php#L64

add:

if(is_string($this->id)){
	$this->id = preg_replace("/[^0-9,.]/", "", $this->id );
}

And replace

https://github.com/airesvsg/acf-to-rest-api/blob/99c7731e3cc1443a9dd7af98370db0ec228d7b28/v3/lib/class-acf-to-rest-api-acf-api.php#L133

with:

if ( strpos( $id, 'user_' ) !== false ) {
	$filter = array('user_id' => str_replace( 'user_', '', $id ) );
	$user = get_userdata( $filter["user_id"] );
	if( ! $user || ! $user->exists() || empty($user->roles) || !user_can($user->ID, $user->roles[0])){
		$filter["user_id"] = "new";
	}
} else {
	$filter = array( 'post_id' => $id );
}
$field_groups = acf_get_field_groups( $filter );

Bang after searching google for 3 hours

shwetdalal avatar Oct 13 '20 21:10 shwetdalal

still doesn't work for me adding this changes

amprodes avatar Jul 03 '21 17:07 amprodes

still doesn't work for me adding this changes

in class-acf-to-rest-api-controller.php

change:  $this->acf->get_field_objects( $id );
to: get_field_objects( $id );

this resolve my problem

acionegatilho avatar Mar 20 '22 17:03 acionegatilho

Thanks, everyone. I had to implement both acionegatilho and shwetdalal changes to get this to fire. :)

robert-j-peterson avatar Apr 16 '23 15:04 robert-j-peterson