WordPress custom menu fields

I’ve packaged-up a plugin that I wrote a while ago for a project that I worked on which required a WordPress site’s menu items to have a separately addressable excerpt field which could override the linked page’s in-built excerpt.

The plugin that I’ve now released was half of the resolution to this requirement, which allows for simple programmatic field additions to the menu system.

Custom Menu Fields is available at the plugin repo.

To use this plugin all one needs to do is install and activate the plugin as normal and then include a snippet like below in their theme’s functions.php (or custom plugin code. Just be sure it loads earlier than the init action!):

<?php add_action('init', 'menu_excerpt__add_menu_field'); function menu_excerpt__add_menu_field() { if (!is_callable('bh_add_custom_menu_fields')) return; bh_add_custom_menu_fields(array( 'excerpt' => array( 'description' => 'Excerpt', 'type' => 'textarea', ))); } ?>
Code language: PHP (php)

Now you can get access to the ‘excerpt’ field by using the attribute on the menu variable you assign when you get the menu from WordPress.

e.g. to print the excerpt created above:

$menu = wp_nav_menu('theme_location' => 'your-theme-location-name', 'echo' => false); print $menu->excerpt;
Code language: PHP (php)

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: