megclaypool of Rootid
8/23/2019 - 1:27 PM

WordPress Advanced Custom Fields Button Group Code

Import the JSON file to ACF. Then make sure the PHP code is loaded into functions.php (we've got it in its own file and then require_onced into functions.php). The php code will automatically add a field to each button called 'href' which will be auto set to either the local page or external url depending on the switch position.

[
    {
        "key": "group_5d51aa5ca0e01",
        "title": "Base Fields to Duplicate and Move",
        "fields": [
            {
                "key": "field_5d51aaef15c2a",
                "label": "Button",
                "name": "button",
                "type": "group",
                "instructions": "",
                "required": 0,
                "conditional_logic": 0,
                "wrapper": {
                    "width": "",
                    "class": "link-group",
                    "id": ""
                },
                "layout": "block",
                "sub_fields": [
                    {
                        "key": "field_5d51ab1915c2b",
                        "label": "Text",
                        "name": "content",
                        "type": "text",
                        "instructions": "",
                        "required": 0,
                        "conditional_logic": 0,
                        "wrapper": {
                            "width": "",
                            "class": "link-text",
                            "id": ""
                        },
                        "default_value": "Learn More",
                        "placeholder": "",
                        "prepend": "",
                        "append": "",
                        "maxlength": ""
                    },
                    {
                        "key": "field_5d51ab4d15c2c",
                        "label": "Link Type",
                        "name": "type",
                        "type": "true_false",
                        "instructions": "",
                        "required": 0,
                        "conditional_logic": 0,
                        "wrapper": {
                            "width": "",
                            "class": "link-type",
                            "id": ""
                        },
                        "message": "",
                        "default_value": 1,
                        "ui": 1,
                        "ui_on_text": "Local",
                        "ui_off_text": "External"
                    },
                    {
                        "key": "field_5d51ab7a15c2d",
                        "label": "Local Page",
                        "name": "local_page",
                        "type": "page_link",
                        "instructions": "",
                        "required": 0,
                        "conditional_logic": [
                            [
                                {
                                    "field": "field_5d51ab4d15c2c",
                                    "operator": "==",
                                    "value": "1"
                                }
                            ]
                        ],
                        "wrapper": {
                            "width": "",
                            "class": "link-picker",
                            "id": ""
                        },
                        "post_type": "",
                        "taxonomy": "",
                        "allow_null": 0,
                        "allow_archives": 1,
                        "multiple": 0
                    },
                    {
                        "key": "field_5d51ab9715c2e",
                        "label": "External URL",
                        "name": "external_url",
                        "type": "url",
                        "instructions": "",
                        "required": 0,
                        "conditional_logic": [
                            [
                                {
                                    "field": "field_5d51ab4d15c2c",
                                    "operator": "!=",
                                    "value": "1"
                                }
                            ]
                        ],
                        "wrapper": {
                            "width": "",
                            "class": "link-picker",
                            "id": ""
                        },
                        "default_value": "",
                        "placeholder": ""
                    }
                ]
            }
        ],
        "location": [
            [
                {
                    "param": "post_type",
                    "operator": "==",
                    "value": "post"
                },
                {
                    "param": "post_type",
                    "operator": "==",
                    "value": "page"
                }
            ]
        ],
        "menu_order": 0,
        "position": "normal",
        "style": "default",
        "label_placement": "top",
        "instruction_placement": "label",
        "hide_on_screen": "",
        "active": true,
        "description": ""
    }
]
<?php

function update_button_href($value, $post_id, $field) {
  if($field['sub_fields'][0]['type'] == 'text' && $field['sub_fields'][1]['type'] == 'true_false' && $field['sub_fields'][2]['type'] == 'page_link') {
    $value['href'] = $value['type'] ? $value['local_page'] : ($value['external_url'] ? $value['external_url'] : '');
  }
  return $value;
}

add_filter('acf/format_value/type=group', 'update_button_href', 1000, 3);