Adding a counter to add_filter ( Plugin: ACF )
-
Hello all
I’m using add_filter to attempt to add unique IDs to objects. In this case, to Advanced custom fields. Im using a counter with I want to increase with each time the filter function is called. Although my issue is ACF specific, what I want to understand is how variables are passed and returned from a filter.
So, I started by setting up a counter variable in my functions.php :-
$counter = 0;
And then the filter looks like this:-
function my_acf_load_field( $field ) { global $counter; $field['wrapper']['id'] = 'unique-id-'.$counter; $counter++; return $field; } add_filter('acf/load_field', 'my_acf_load_field');
The trouble is, the counter never increments. It stays at zero. I’m guessing incrementing the counter inside the filter function doesn’t then pass the result back, so the counter never goes up.
So, how would I go about putting a counter variable inside a filter, then modifying that counter so it goes up with each call of the function.
I really hope the above makes some sense! Im pretty new to this so I might be getting my terminology wrong. Any help would be appreciated.
- The topic ‘Adding a counter to add_filter ( Plugin: ACF )’ is closed to new replies.