A new Advanced Custom Field type lets you select any WordPress menu.

Entrepreneur, teacher, developer.
Search for a command to run...

Entrepreneur, teacher, developer.
No comments yet. Be the first to comment.
If you can sum up Web3 in one word — I think most people would say "decentralized." While I do enjoy the idea of a decentralized web — I doubt that it will truly be "decentralized" as massive whales, corporations, and governments move in to invest th...

Whether it's a Zoom meeting or recorded content — I've been asked on several occasions what gear I use — and each time, I piecemeal a list and send it. So — I've decided to break down each piece of audio gear that I use and its purpose so anyone inte...

In previous articles, we stepped through making a plugin from the folder to the file and adding some practical functionality. This article will dive in much deeper and learn how to create a plugin and prepare it to release in the world for production...

In the previous article — we walked through creating a plugin by only adding a folder, a file, and the plugin header information at the top. This article will create functionality for our plugin to help demonstrate how you can build your own WordPres...

WordPress plugins can be as simple or complex as you want them to be — and they are ridiculously easy to create, like, super easy. In this article — I will walk you through creating a WordPress plugin in a way that I've come to learn, and hopefully, ...

I faced a recent challenge where a site needed to support different main menus on different parent pages. I'm a big fan of Advanced Custom Fields and immediately knew that it would be the perfect tool for the job.
There were 40 parent pages that each required their own navigation. Under each of the 40 parent pages would be several children pages and probably children of children and so on.
I grappled with the ACF Relationship field first, but it wouldn’t allow me to nest pages natively. There was a thought that “Maybe the user could select the parent pages and the menu will build itself,” but the project requirements dictated otherwise.
Ultimately the user would need to control both nesting and order.
Briefly — I performed research but quickly pivoted to, “Hey, I bet it wouldn’t be too difficult to make my own ACF field.”
Then I found this; ACF | Creating a new field type.
Of course — Elliot Condon and his team would make it dead simple for developers to create their fields; after all, they’ve done such an amazing job doing just that for years now.
I dived right into their boilerplate repo and had to figure out where I would be writing my code.
Here’s what I found.
function render_field( $field ) {
}
Turns out — this is where I needed to write the code. The idea was to expose a new field type where an array of menu ids would power the data.
Here what I did to get the menu ids:
$menus = wp_get_nav_menus();
foreach ($menus as $key => $value) {
$field['choices'][$value->term_id] = $value->name;
}
That was pretty easy! However — this next part, not so much.
I wanted to use the select2 dropdown field that ACF uses — you know — that slick-looking dropdown with a search field inside it?
The reason was not purely based on bias; remember, there will be 40 pages that need 40 different menus, and that’s a lot to scroll through!
After doing some Google searching, I was able to find this little gem.
$field['type'] = 'select';
$field['ui'] = 1;
$field['label'] = '';
acf_render_field_wrap($field);
Turns out — there are a few things that are required if you want to use the select2 or “fancy select dropdown.”
Setting both the type to “select” and the UI to “1” is required. Then acf_render_field_wrap($field) brings the magic sauce.
Here’s what it all looks like put together.
function render_field( $field ) {
$field['type'] = 'select';
$field['ui'] = 1;
$field['label'] = '';
$menus = wp_get_nav_menus();
foreach ($menus as $key => $value) {
$field['choices'][$value->term_id] = $value->name;
}
acf_render_field_wrap($field);
}
With this in place — we now have the ability to create a new menu field for our pages. When the user makes a selection — the ACF function get_field() will return the menu ID. You can use this ID to display the menu using the WP functionwp_nav_menu().
Here's what it looks like in action.

Pretty sweet — yea? Let's see what it looks like when editing a page.

If you find yourself needing this type of functionality — hopefully, this saves you some time.
You can download the plugin here.
Cheers! 🎉