Home › Forums › Front-end Issues › acf/update_value filter issues
Hey there,
I’m hit a wall while using the acf/update_value filter to update user details. Here is the scenario…
Using Genesis and BBPress, I’m loading an ACF form into the Edit Profile section of the site in order to save some extra meta details. In doing so, I figured it would be best to create fields for First Name, Email, etc (redundant of the ones in the basic WordPress user profile) and hook into update_value to update those fields.
So, on page.php I have the following (note, I’m using the Genesis framework):
if (bbp_is_single_user_edit()):
add_filter('acf/update_value', 'nwr_acf_update_profile', 10, 3);
endif;
function nwr_acf_update_profile($value, $post_id, $field) {
fb($field['name'] . ' = ' . $value);
$current_user = wp_get_current_user();
if (current_user_can('edit_user', $current_user->ID)):
$userdata = array('ID' => $current_user->ID);
switch ($field):
case 'user_first_name':
if ($value != $current_user->first_name):
$userdata['first_name'] = $value;
endif;
break;
endswitch;
if (count($userdata) > 1):
wp_update_user($userdata);
endif;
endif;
return $value;
}
On form-user-edit.php, I have the acf_form.
The form works great and updates ACF fields, however, the filter doesn’t seem to be being called. I’m using the update_value filter on another page to change a custom post title without any issue, so I’m clearly missing something here.
Any help or advice would be greatly appreciated.
Thank you!
Does the filter fire when it’s not wrapped in the if (bbp_is_single_user_edit())
? I would also be interested in the filter priority order and whether you’re calling before or after any other filters.
No, the filter doesn’t fire when not wrapped. I tried that as well as placing it directly in the functions.php file. Nothing.
I just tried firing it with priority 5 and 20, still nothing.
I’m really banging my head against the wall on this one.
Thanks, emcniece. Let me know if you have any other ideas.
Out of curiosity…
function print_filters_for( $hook = '' ) {
global $wp_filter;
if( empty( $hook ) || !isset( $wp_filter[$hook] ) )
return;
print '<pre>';
print_r( $wp_filter[$hook] );
print '</pre>';
}
Called with:
print_filters_for( 'acf/update_value' );
Anything interesting in there? Is your function actually being added?
Looks like it is there:
Array
(
[5] => Array
(
[000000004f2d8d300000000032cd93ceupdate_value] => Array
(
[function] => Array
(
[0] => acf_field_functions Object
(
)
[1] => update_value
)
[accepted_args] => 4
)
)
[20] => Array
(
[nwr_acf_update_profile] => Array
(
[function] => nwr_acf_update_profile
[accepted_args] => 3
)
)
)
So I guess it would be nice to know if the function is being executed at all – I usually do this by dropping something fairly identifiable into the first line of the function, like echo "ilovetroubleshooting";
, then I watch for that to appear on the page load when the filter should have executed.
Do you think that the add_filter()
definition is ok in page.php
? I wonder if it should go in functions.php
instead…
Well, at this point in the code, I don’t think an echo will work. I actually have the Simple Fire PHP plugin installed for debugging PHP in the console for just this and no, it doesn’t seem to be firing. That said, I tested the other update_value call I’m using in another section of the site and the FirePhP code didn’t work there either, but that filter works. So that wasn’t helpful.
Further, I did try putting it in functions.php to no avail. The way Genesis works is that it actually allows you do have custom filters/actions on a per page/template basis, outside of functions since the page isn’t called/rendered until the genesis() call is made at the end of the file.
The only difference between how I’m calling this filter that isn’t working at the the one that is working is that the one that is working is all on one page. This one that isn’t, the filter is on page.php, however, the acf_form call is actually on form-user-edit.php (which is called from page.php in bbpress). However, since I’m using Genesis, I’m not sure how to get everything together (if that is the issue). That said, if that is the issue, putting the filter in functions.php should resolve that, however, it doesn’t.
And therefore, I’m still stumped…
Yikes.
Add the filter code to both form-user-edit.php
and page.php
? Lol… yeah, running out of ideas too.
Ha! I found the issue with the code and then found the issue with the rest…
First: my switch statement was
switch ($field)
when it should be
switch ($field['name'])
After fixing that, I figured out the call wasn’t actually happening on page.php OR within the if statement! Not sure why though.
In any case, my final code, within the functions.php file is:
add_filter('acf/update_value', 'nwr_acf_update_profile', 10, 3);
function nwr_acf_update_profile($value, $post_id, $field) {
if (bbp_is_single_user_edit()):
$current_user = wp_get_current_user();
if (current_user_can('edit_user', $current_user->ID)):
$userdata = array('ID' => $current_user->ID);
switch ($field['name']):
case 'user_first_name':
if ($value != $current_user->first_name):
$userdata['first_name'] = $value;
endif;
break;
endswitch;
if (count($userdata) > 1):
wp_update_user($userdata);
endif;
endif;
endif;
return $value;
}
Nice work! I use Genesis once in a while too, it’s great to know how to pull filters like this at least. I’ll have to check out that Simple Fire plugin some time…
and you got this just in time for the weekend, that must feel great 😀
You can find the plugin here: http://wordpress.org/plugins/simple-wp-firephp/ (hasn’t been updated in a long time, but I don’t think there is much to update)
And the FirePHP details: http://www.firephp.org/
Thanks for the help, @emcniece!
The topic ‘acf/update_value filter issues’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.