I am here again with a strange question.
First, I'm using ACF plugin for adding cistom fields. My code looks somethink like that:
global $testMe;
$testMe = 0;
function my_acf_update_value( $value, $post_id, $field ) {
global $testMe;
$testMe = $value;
return $value;
}
add_filter('acf/update_value/key=field_5308e5e79784b', 'my_acf_update_value', 10, 3);
echo $testMe; // -> print 0, not the $value!?
The problem is that I want, after the filter apply, $testMe to contain the value of $value.
Any ideas where I'm wrong?
I am here again with a strange question.
First, I'm using ACF plugin for adding cistom fields. My code looks somethink like that:
global $testMe;
$testMe = 0;
function my_acf_update_value( $value, $post_id, $field ) {
global $testMe;
$testMe = $value;
return $value;
}
add_filter('acf/update_value/key=field_5308e5e79784b', 'my_acf_update_value', 10, 3);
echo $testMe; // -> print 0, not the $value!?
The problem is that I want, after the filter apply, $testMe to contain the value of $value.
Any ideas where I'm wrong?
I ran a test and the following works:
global $testMe;
$testMe = 0;
function my_acf_update_value( $value, $post_id, $field ) {
global $testMe;
$testMe = $value;
return $value;
}
add_filter('acf/update_value/key=field_5308e5e79784b', 'my_acf_update_value', 10, 3);
// Test: Apply the filter
apply_filters('acf/update_value/key=field_5308e5e79784b','a','b','c');
// end Test
echo $testMe; // prints 'a'
So, in principle, your code is functional. There are a couple of things that I can think that may be going wrong.
apply_filters
before the rest of the code and it won't work.Other than that, it should work. However, this global
variable technique is a bit messy. Might I suggest something like this:
function my_acf_grab_value($value,$echo = false) {
static $var = 0;
if (false === $echo) {
$var = $value;
} else {
return $var;
}
}
function my_acf_update_value( $value, $post_id, $field ) {
my_acf_grab_value($value);
return $value;
}
add_filter('acf/update_value/key=field_5308e5e79784b', 'my_acf_update_value', 10, 3);
// Test: Apply the filter
apply_filters('acf/update_value/key=field_5308e5e79784b','Yay','b','c');
// end test
echo my_acf_grab_value('',true);
Or this:
function my_acf_update_value( $value = '', $post_id = 0, $field = '' ) {
static $var = 0;
if (!empty($value)) {
$var = $value;
} else {
return $var;
}
return $value;
}
add_filter('acf/update_value/key=field_5308e5e79784b', 'my_acf_update_value', 10, 3);
// Test: Apply the filter
apply_filters('acf/update_value/key=field_5308e5e79784b','Yay','b','c');
// end test
echo my_acf_update_value();