• Resolved maven1129

    (@maven1129)


    I’m trying to find a solution whereby custom fields default values are set based on other field values. For example, create a default value for Phone_link field from field Phone_number. Phone_link would not be editable, but instead would be set from the value of Phone_number. When Phone_number is changed or edited, this Phone_link would need to be updated. I am yet to find a solution for this. Any capabilities in this plugin?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Thanks for the feedback! You can set the phone_link as a Hidden Input Field, and then use the following code in order to update its value based on the phone_number field:

    add_filter('acf/update_value/name=phone_number', 'my_phone_number_update', 10, 3);
    function my_phone_number_update($value, $post_id, $field){
        
        // update phone_link
        update_field('phone_link', $value, $post_id);
        
        return $value;
        
    }
    

    Please note that Phone Link must be above the Phone Number field in the Fields list, so the update_field() is triggered after it. Otherwise you can also use the acf/save_post hook combined with update_field().

    If you don’t want to use a Hidden Input field, you can use the regular ACF Text field, the acf/update_value hook will work the same. You can then set phone_link as disabled using the following code:

    add_filter('acf/prepare_field/name=phone_link', 'my_phone_link_prepare');
    function my_phone_link_prepare($field){
        
        $field['disabled'] = true;
        
        return $field;
        
    }
    

    Please note that the $field['disabled'] setting is not officially supported and only work with few simple fields like “Text” fields.

    Have a nice day!

    Regards.

    Thread Starter maven1129

    (@maven1129)

    Wow! Amazing support and thank YOU for the thorough, comprehensive reply. I will give this a go… I didn’t realize that I could put php into the field value field.

    Just do it like this? I’d like to wrap it in a “tel:” link as well.

    2021-04-23_19-21-06.jpg

    I was always told that I would have to go in an edit each page it was used on, then save it, for the field to update. Are you saying that any time the page is rendered, and this field is present on the page visibility sections, it would update the field that is actually in use on the page, or would I need to also include that hidden field in the content of the page somewhere?

    • This reply was modified 3 years, 7 months ago by maven1129.
    • This reply was modified 3 years, 7 months ago by maven1129.
    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    There was a misunderstanding, PHP in the UI won’t work, you should avoid doing that. The PHP script I posted has to go in your theme functions.php file. In your screenshot, your fields don’t have the same name as in your first post, so you have to update the code like this:

    
    // In the hook name you need to set the "name=provider_phone_number" to the main phone field name
    add_filter('acf/update_value/name=provider_phone_number', 'my_phone_provider_update', 10, 3);
    function my_phone_provider_update($value, $post_id, $field){
        
        // Here you got to put the "Hidden" field name "provider_phone_number_copy"
        update_field('provider_phone_number_copy', $value, $post_id);
        
        return $value;
        
    }
    

    See acf/update_value documentation for more information.

    You’ll then have to go to each post and save the page so the provider_phone_number_copy get correctly updated.

    With the code above in your functions.php file, every time the page is saved, both fields will have the same value.

    Regards.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Update default value’ is closed to new replies.