Support

Account

Home Forums General Issues is it safe to add properties to acf object ?

Solving

is it safe to add properties to acf object ?

  • when manipulating acf field, I sometimes have the need to add information to it, I wonder if it is safe to simply add a property to the acf object, for example like that :

    
    function add_property_to_field($null, $value, $post_id, $field) {
      $field['my_new_property'] = $property_value;
    }
    add_filter('acf/pre_update_value', 'add_property_to_field', 10, 4);
    

    or is there a risk that it is removed in some kind of cleaning process ? Or another reason to avoid doing that ?

    And if it is not a good practice, how can i add information to a field ?

  • You could do that as long as it doesn’t conflict with an existing property but it will only last for the lifecycle of the object i.e it won’t be saved to the db and be available to use when the page loads.

    What additional information do you want to add to the field?

  • ok ! indeed you are right, it will not be saved in the database, so that does not help me, I need to find another way 🙂

    What additional information i need to store ? it depends, but sometimes I would like to store additional information about a field, for example I used the value:label of the checkbox to store information in the ‘value’ part, and then i did some string manipulation to extract the values (ie. separating the portions with slash : for_action_email_notification/7days : send email -> extract the ‘7days’ part with explode('/', $value)[1])

    but it’s just a trick, I could also use one of the class or id of the field or it’s wrapper the same way, but I’m only cheating by using a property that is not made for that

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.