Custom Menu Fields

Custom Menu Fields

from 0$

Custom Menu Fields is a Package Installer from the Open Source WP Plugins range Provides an API to add custom fields to the menu editor.

For more information about a tailored Package Installer like Custom Menu Fields please contact us directly.

This plugin provides an API which allows the developer of a site to add custom fields on the default menu editor.

Example Initialisation

<?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',
            )));
}
?>

Accessing the fields

The easiest way to access the field(s) you’ve added is to use something along the lines of:

$menu = 'menuName';
$posts = wp_get_nav_menu_items($menu);
foreach ($posts as $p) {
    $myitem = get_post_meta($p->ID, '_menu_item_youritem', true);
    // do with $myitem what you like - it should be a string,
    // so the simplest thing is to "echo" it
}

you can use menu locations to get the menu name if you prefer – replace the first line above with:

$locations = get_nav_menu_locations();
$menu = $locations['locationName'];

menu locations are useful if you like to swap your menus about.

Unfortunately these examples don’t allow the use of wordpress’ inbuilt menu walkers. To use those you will need to create a custom walker_nav_menu class and access the custom fields with something along the lines of (very stripped down example will need fleshing out for full walker functionality – there are tutorials on the net for custom nav walkers):

class mywalker extends Walker_Nav_Menu {
    function start_el(&$output, $item, $depth, $args) {
        echo $item->custom_field
    }
}

The important bit here is that the field is placed on the second variable which behaves like an object. the custom_field part is the name you gave your field with dashes replaced with underscores (to allow the name to be used in an accessor).

NOTE

This plugin does nothing by itself. It provides an API only.

Download & install the zip archive

The plugin package installer can be downloaded from the WP2E project tab called “code”.

1 – Select the version to download if this option is available otherwise the “latest” version of the main plugin will be used.

2 – After downloading the zip archive install the plugin package installer in you local environment and activate the script from the plugin list.

3 – Under the section “Plugins” of the admin dashboard you should see a new “Dependencies & Licenses” link. Follow the instructions from this panel to finalize the installation of the missing dependencies.

Tips: Use the WP2E panel to add/suggest new dependencies to the local installation. Press F5 in the list of dependencies if the changes are not displayed right away.

Custom Menu Fields

typePlugin
version0.2
descriptionProvides an API to add custom fields to the menu editor.