Support

Account

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

Helping

Predefined field's value from a function or DB

  • Hi!

    I am trying to predefine text fields value.

    What I need is to show at the editor page a DISABLED (non-editable) text field with the predefined data (I need to fetch it from the DB using inner functions). Say: I have to insert into the test field a slug of the last post of the particular type.

    Do you have any idea of how to do that?

    Thank you!

  • 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');
    
    ?>
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Predefined field's value from a function or DB’ is closed to new replies.