Moe Loubani
Back to blog
WordPress

Menu select in ACF

Today I'm working on a feature where I'll need to be able to select a menu on an options page in ACF inside a repeater field. I took a look and didn't see anyone else offering up how they did this so ...

Today I’m working on a feature where I’ll need to be able to select a menu on an options page in ACF inside a repeater field. I took a look and didn’t see anyone else offering up how they did this so here it goes: I started with this https://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/ and created a select field called ‘menus’.

The code then to get things to work was

function add_acf_menu_field( $field ) {
    // reset choices
    $field['choices'] = array();

    // get a list of the menus and their associated ids
    $menus = wp_get_nav_menus();

    foreach($menus as $menu) {
        $field['choices'][$menu->term_id] = $menu->name;
    }

    // return the field
    return $field;
}

add_filter('acf/prepare_field/name=menus', 'add_acf_menu_field');

Hope this helps anyone coming across the same thing.