Git Product home page Git Product logo

wp-menu-item-custom-fields's Introduction

Menu Item Custom Fields

Easily add custom fields to nav menu items.

Contributors: kucrut
Tags: menu, nav-menu, custom-fields, metadata
Requires at least: 3.8
Tested up to: 4.7.2
Stable tag: 1.0.0
License: GPLv2
Donate Link: https://www.paypal.me/kucrut

Build Status

Description

Breaking Change

Since version 1.0.0, the first parameter passed to the wp_nav_menu_item_custom_fields is the menu item ID, instead of the nav menu ID. This should not have a big impact, since the nav menu ID passed was always 0 (not used by core).

This is a library plugin. It doesn't do anything visible on its own. It was written to allow other plugins/themes to add custom fields to menu items easily. See Installation.

Development of this plugin is done on GitHub. Pull requests welcome. Please see issues reported there before going to the plugin forum.

Installation

As regular plugin

  1. Upload menu-item-custom-fields to the /wp-content/plugins/ directory
  2. Activate the plugin through the 'Plugins' menu in WordPress

As library in your plugin/theme

Simply copy menu-item-custom-fields to your plugin directory and require the main plugin file, eg: require_once dirname( __FILE__ ) . '/menu-item-custom-fields/menu-item-custom-fields.php';

Usage

Copy (and customize) and include the menu-item-custom-fields-example.php file found in the doc/ directory of this plugin into your plugin/theme.

Changelog

1.0.0

  • Pass correct parameters to the wp_nav_menu_item_custom_fields hook, props @helgatheviking.

0.4.0

0.3.0

  • Use wp_nav_menu_item_custom_fields as walker hook. See this blog post.
  • Update example plugin

0.2.1

  • Update compatibility info

0.2.0

  • Improve walker class loader

0.1.1

  • Move custom fields up (before <p.field-move />)

0.1.0

  • Initial public release

wp-menu-item-custom-fields's People

Contributors

kucrut avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

wp-menu-item-custom-fields's Issues

Warning in settings screen

self::$fields is an array of arrays, but $columns must be an array of strings
so we have to take only "label" of fields by using wp_list_pluck

In the example file change the last function to:

public static function _columns( $columns ) {

	$columns = array_merge( $columns, wp_list_pluck( self::$fields, 'label' ) );

	return $columns;
}

How early is the new menu item field added?

If I am using wp_setup_nav_menu_item() to create menu items, will this allow me to insert custom fields dynamically with that function? Or is it called after the menu items are created?

Doesn't work in WordPress 4.7

This works fine with WordPress 4.6.1 but doesn't work with WordPress 4.7. I can't seem to figure out the issue. Could you please point me in the right direction.

Thank you!

Can't add new menu items

I have this included in my theme as a library, with only slight modifications to the example implementation.

Without a conditional on all the code, the ajax call for adding a new menu item fails and users can't add new menu items.

Fix looks like:

if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) :

function myplugin_load_menu_item_custom_fields() {
    require_once get_template_directory() . '/vendor/wp-menu-item-custom-fields/menu-item-custom-fields.php';
}
add_action( 'load-nav-menus.php', 'myplugin_load_menu_item_custom_fields' );

/**
 * Menu item metadata
 */
class Pipeline_Menu_Item_Custom_Fields {

}
Pipeline_Menu_Item_Custom_Fields::init();

endif; // AJAX check

Issue with duplicating Menu_Item_Custom_Fields_Walker via wp-menu-icons plugin

Hi @kucrut again.

I'm using your brilliant plugin wp-menu-icons together with your wp-menu-item-custom-fields library for adding my own menu item custom fields.

I required Menu_Item_Custom_Fields_Walker from wp-menu-item-custom-fields library directly in my theme and extended with some extra functions for my needs.
So, when my theme activated within wp-menu-icons plugin โ€” i can't use my functions from my theme's Menu_Item_Custom_Fields_Walker, because class is already exist in menu icons plugin.

For example, here is my Menu_Item_Custom_Fields_Walker where I adding my own item-controls in .menu-item-bar.

class Menu_Item_Custom_Fields_Walker extends Walker_Nav_Menu_Edit {

    function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
        $item_output = '';
        parent::start_el( $item_output, $item, $depth, $args, $id );
        $output_pre = preg_replace(
            // NOTE: Check this regex from time to time!
            '/(?=<span[^>]+class="[^"]*item-type)/',
            $this->get_labels( $item, $depth, $args ),
            $item_output
        );
        $output .= preg_replace(
            // NOTE: Check this regex from time to time!
            '/(?=<p[^>]+class="[^"]*field-move)/',
            $this->get_fields( $item, $depth, $args ),
            $output_pre
        );
    }

    protected function get_fields( $item, $depth, $args = array(), $id = 0 ) {
        ob_start();
        do_action( 'wp_nav_menu_item_custom_fields', $id, $item, $depth, $args );
        return ob_get_clean();
    }

    protected function get_labels( $item, $depth, $args = array(), $id = 0 ) {
        ob_start();
        do_action( 'wp_nav_menu_item_custom_labels', $id, $item, $depth, $args );
        return ob_get_clean();
    }
}

in OS_Menu_Item_Custom_Fields i've added that action.

add_action( 'wp_nav_menu_item_custom_labels', array( __CLASS__, '_labels' ), 11, 4 );

public static function _labels( $id, $item, $depth, $args ){
    $out = '<span class="os-item-label-mega"><span class="os_label os_bg_brand">Mega Menu</span></span>';
    $out .= '<span class="os-item-label-mega-column"><span class="os_label os_bg_brand_alt">Column</span></span>';
    echo $out;
}

Interesting that, when i've used wp-menu-icons from icon-picker-integration branch โ€” my Menu_Item_Custom_Fields_Walker class was not overridden by plugin's class.

So, I have a question. How to right filter Walker_Nav_Menu_Edit walker via your Menu_Item_Custom_Fields_Walker from this library that in the future do not have problems with other plug-ins, which can also extend Walker_Nav_Menu_Edit walker class?

Cheers!

Duplicated fields

I was planning on using this library. But for some reason the fields are duplicated.

What I have for each menu item is:
Custom Field #1
Custom Field #2
Custom Field #1
Custom Field #2

Displaying data on front end?

While this does a fairly good job at quickly and easily creating additional input fields on menu items...does it actually display on the front end at all?

I'm looking for a way to add a field for 'Custom Class', and hook into the default menu walker without having to load my own.

Custom action hook parameters are not the same as in Nav Menu Roles

Originally reported by @helgatheviking.

First, I love the preg_replace() idea! And two, thanks for adding the wp_nav_menu_item_custom_fields action hook so Nav Menu Roles and Menu Icons can be compatible.

However, I've found that your action hook's params aren't the same as mine.

You have:

do_action( 'wp_nav_menu_item_custom_fields', $id, $item, $depth, $args );

While I have:

do_action( 'wp_nav_menu_item_custom_fields', $item_id, $item, $depth, $args );

In your case $id will always be zero. I don't know if this has a lot of impact since it's only used by us menu plugin authors, but I thought I'd point it out.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.