PHPackages                             inpsyde/more-menu-fields - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. inpsyde/more-menu-fields

AbandonedArchivedLibrary[Utility &amp; Helpers](/categories/utility)

inpsyde/more-menu-fields
========================

Package to add more fields to WordPress menu edit screen.

0.2.0(8y ago)34100.3k↓28.6%1MITPHPPHP &gt;=7

Since Sep 29Pushed 3y ago4 watchersCompare

[ Source](https://github.com/inpsyde/more-menu-fields)[ Packagist](https://packagist.org/packages/inpsyde/more-menu-fields)[ RSS](/packages/inpsyde-more-menu-fields/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (3)Versions (4)Used By (0)

More Menu Fields [![Latest Stable Version](https://camo.githubusercontent.com/28ef25bd0d2c8454cf390b2d7cbb325cfb3a9e4d70586de1ab29602d178a0d93/68747470733a2f2f706f7365722e707567782e6f72672f696e70737964652f6d6f72652d6d656e752d6669656c64732f762f737461626c65)](https://packagist.org/packages/inpsyde/more-menu-fields) [![Project Status](https://camo.githubusercontent.com/5b5a2250da48f45495a817a4bcdabb5d101fff298acebe00a55a52815b7119ed/687474703a2f2f6f70656e736f757263652e626f782e636f6d2f6261646765732f6163746976652e737667)](http://opensource.box.com/badges) [![Build Status](https://camo.githubusercontent.com/b96d96dc442d185e45a90976abfeb0fa40587ef0ff1763c50f1e971fdfa44156/68747470733a2f2f7472617669732d63692e6f72672f696e70737964652f6d6f72652d6d656e752d6669656c64732e7376673f6272616e63683d6d6173746572)](http://travis-ci.org/inpsyde/more-menu-fields) [![License](https://camo.githubusercontent.com/82db4e79bda54c17c94676ff7dae884ff95f6707e939d7ab6605a4b82de410c6/68747470733a2f2f706f7365722e707567782e6f72672f696e70737964652f6d6f72652d6d656e752d6669656c64732f6c6963656e7365)](https://packagist.org/packages/inpsyde/more-menu-fields)
=============================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

[](#more-menu-fields----)

> Package to add more fields to WordPress menu edit screen.

---

What / Why
----------

[](#what--why)

WordPress provides a nice UI for editing navigation menus.

However, it is quite opinionated about the available settings for each menu item. They are:

- "Navigation Label"
- "Title Attribute"
- "Open link in a new tab"
- "CSS Classes"
- "Link Relationship (XFN)"
- "Description"

For our clients we needed additional fields, e.g. "data" attributes or "rel" attributes ("noopener", "nofollow"...).

Issue is WordPress &lt;5.4.0 provides **no** filter to edit the default fields and there's also no action hook to allow echoing custom form fields HTML, like happens in many other parts of WP backend.

**⚠️ Since WordPress 5.4.0 there is a new Hook `wp_nav_menu_item_custom_fields` implemented which allows you to filter the current item and add custom fields. This package exists, because we needed in WordPress &lt;5.4.0 a way to add more fields that could work if used from more plugins. You can still use this library with newer WordPress version to work in an object oriented way on custom navigation items attributes.**

---

Usage
-----

[](#usage)

### First Step

[](#first-step)

Because the use target of "More Menu Fields" is to be used from plugins, it is not a plugin itself, but a *package*that can be required by plugins via Composer.

When the package is required via Composer and Composer autoload has been loaded, it is needed to *bootstrap* the package.

It can be done inside a plugin by just calling a function:

```
Inpsyde\MoreMenuFields\bootstrap();
```

There's no need to wrap the call in any hook and if called more than once (by different plugins) nothing bad will happen.

### The Field Interfaces

[](#the-field-interfaces)

To add more fields it is necessary to create a PHP class for each of them. The class has to implement the interface `Inpsyde\MoreMenuFields\EditField` which looks like this:

```
interface EditField {

	public function name(): string;

	public function field_markup(): string;
}
```

The first method, `name()`, has to return the field name, it can be any string, but must be unique. This will also be used later on to retrieve the value that is entered in the input field.

The second and last method, `field_markup()`, has to return the HTML markup for the field, as it will appear on the UI.

In the HTML markup it will very likely be necessary to use the input name, its id and its current stored value, if any. Those information can be obtained via an object of type `Inpsyde\MoreMenuFields\EditFieldValue`. More on this soon.

Very often (if not always) the value users enter in the generated input field needs to be sanitized before being saved. This is why the package ships another interface `Inpsyde\MoreMenuFields\SanitizedEditField` which looks like this:

```
interface SanitizedEditField extends EditField {

	public function sanitize_callback(): callable;
}
```

The interface extends `EditField` and its only method, `sanitize_callback()`, can be used to return a callback used to sanitize the users input. It is recommended to implement this interface to create fields and only use `EditField` for form input fields that don't actually take input, like buttons.

### Field Class Example

[](#field-class-example)

Nothing is better than an example to see how things work.

Below there's a real-world example of a class that will render a checkbox to add a "nofollow" attribute on a menu item link.

```
namespace My\Plugin;

class NofollowField implements Inpsyde\MoreMenuFields\SanitizedEditField
{
	private $value;

	public function __construct( Inpsyde\MoreMenuFields\EditFieldValue $value )
	{
		$this->value = $value;
	}

	public function name(): string
	{
		return 'nofollow';
	}

	public function field_markup(): string
	{
		if ( ! $this->value->is_valid() ) {
			return '';
		}
		ob_start();
		?>
