Support

Account

Home Forums Backend Issues (wp-admin) Predefined field's value from a function or DB Reply To: Predefined field's value from a function or DB

  • Hi @blexfort

    You can define a text field via PHP and then set the ‘read_only’ field attribute to ‘true’. You can learn about this from the resource page here: https://www.advancedcustomfields.com/resources/register-fields-via-php/

    You can then modify the value of this field by passing the slug via the acf/load_field filter like so:

    <?php
    
    function my_acf_load_field( $field ) {
    	
    //use some logic to  get the new value    
    $field['value'] = $new_value;
    
        return $field;
        
    }
    
    // all
    // add_filter('acf/load_field', 'my_acf_load_field');
    
    // type
    //add_filter('acf/load_field/type=select', 'my_acf_load_field');
    
    // name
    add_filter('acf/load_field/name=field_name', 'my_acf_load_field');
    
    // key
    // add_filter('acf/load_field/key=field_508a263b40457', 'my_acf_load_field');
    
    ?>