Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Can't find right hook to save object data on first save WordPress

I am using this function to take some custom field values I've assigned to a user and set them as object terms. Long story short, I'm bridging the gap between two plugins that don't talk to each other. get_field is an ACF function and is returning the array of profession IDs, FYI.

function save_usermeta_as_tax ($user_id) {
$profs = get_field('profession', 'user_'.$user_id);
wp_set_object_terms( $user_id, $profs, 'profession', false);
clean_object_term_cache( $user_id, 'profession' );
}
add_action('personal_options_update', 'save_usermeta_as_tax', 10);


This does the job fine, except that I have to save the user twice to get the object terms to update. I'm assuming I'm not using the correct hook, but I can't seem to find the right one.

Anyone know where I could hook between when the metadata saves and when the object terms are updated?

Answers (3)

2015-04-15

Andrea P answers:

looking at the wordpress codex, it seems that you have to use that hook in congiunction with the edit_user_profile one.

<blockquote>This hook only triggers when a user is viewing their own profile page. If you want to apply your hook to ALL profile pages (including users other than the current one), then you also need to use the edit_user_profile_update hook. </blockquote>

it seems you have to set up both of them, as in the other hook page, it tells the same thing but the other way round:

https://codex.wordpress.org/Plugin_API/Action_Reference/edit_user_profile_update

I hope this will help!
cheers


Zac Eckstein comments:

Thanks for the tip. I only need this to update when the user is viewing their own profile, but I added the other action just in case. Same problem though, I have to save the user twice for the changes to take effect.


Andrea P comments:

so probably the issue is that acf is storing the field only after the wp profile update hook has done.

you could try by hooking your action to the acf saving, rather than the wp user update:

http://www.advancedcustomfields.com/resources/acfsave_post/

in this way, when acf will save the field, it'll trigger your action to update the terms in the user profile


Andrea P comments:

actually, now that I am looking at that code, I have to say that I recently worked with acf frontend forms, and the $_post variable where the data is grouped it was not 'acf' , but instead it was 'field'.

so if that code doesn't work, try to change things like this:

$field = $_POST['acf']['field_abc123'];

into this

$field = $_POST['field']['field_abc123'];

also to avoid that the action will trigger for any acf update, remember to use a conditional like:


$field = $_POST['field']['field_abc123'];
if ( !empty($field) ) {


note that field_abc123 is the field key, not the field slug. http://www.advancedcustomfields.com/resources/update_field/

2015-04-15

Luis Abarca answers:

Looks like you are using the correct hook.

personal_options_update: User updating their own profile.
edit_user_profile_update: Another user updating a user profile.


Zac Eckstein comments:

Any idea how how I can get it to save the first time? Right now if I set the post meta and save the post, I want to the post object to be updated at the same time, but it only updates on the 2nd save, after the meta is already saved from the 1st save.

2015-04-16

Hariprasad Vijayan answers:

Hello,

Have you tried changing priority of add action like
add_action('personal_options_update', 'save_usermeta_as_tax', 100);


Regards,
Hariprasad