Viewing 13 replies - 1 through 13 (of 13 total)
  • Brandon Holcombe

    (@brandonhcreativemediacom)

    I have ACF 5 loaded and this plugin is not working. Looking forward to seeing an update soon.

    Thanks!

    I can also confirm this is an issue with ACF v5 PRO.

    An update in the next few days would be very appreciated.

    Thanks,

    Andi

    I had this same problem last night so I attempted to create a ACF v5 PRO port. This worked for me but there is probably a better way — *use at your own risk*

    Based on the Creating A New Field Type I needed 2 things to make this work. In (#1) fz-acf-nav-menu.php, register v5 support in via (#2) nav-menu-v5.php.

    #1
    Register v5 port in /plugins/advanced-custom-fields-nav-menu-field/fz-acf-nav-menu.php

    <?php
    /*
    Plugin Name: Advanced Custom Fields: Nav Menu Field
    Plugin URI: https://faisonz.com/wordpress-plugins/advanced-custom-fields-nav-menu-field/
    Description: Add-On plugin for Advanced Custom Fields (ACF) that adds a 'Nav Menu' Field type. <br>ACF v5 PRO port by <a href="https://www.justgooddesign.com">Jesse Graupmann</a>.
    Version: 1.1.2.5
    Author: Faison Zutavern + (ACF v5 PRO port by Jesse Graupmann)
    Author URI: https://faisonz.com | https://www.justgooddesign.com
    License: GPL2 or later
    */
    
    class acf_field_nav_menu_plugin
    {
    	function __construct()
    	{
    		// version 4+
    		add_action('acf/register_fields', array($this, 'register_fields'));	
    
    		// version 5+
    		add_action('acf/include_field_types', array($this, 'include_field_types'));
    	}
    
    	function register_fields()
    	{
    		include_once('nav-menu-v4.php');
    	}
    
    	function include_field_types()
    	{
    		include_once('nav-menu-v5.php');
    	}
    }
    new acf_field_nav_menu_plugin();

    #2
    Create the v5 file at /plugins/advanced-custom-fields-nav-menu-field/nav-menu-v5.php and use the following:

    <?php
    // ACF Pro v5 Port by Jesse Graupmann (https://justgooddesign.com)
    // Original from https://faisonz.com/wordpress-plugins/advanced-custom-fields-nav-menu-field/ by Faison Zutavern (https://faisonz.com)
    
    class acf_field_nav_menu_v5 extends acf_field
    {
    	var $settings, $defaults;
    	function __construct()
    	{
    		$this->name = 'nav_menu';
    		$this->label = __('Nav Menu', 'acf');
    		$this->category = __("Relational",'acf'); // Basic, Content, Choice, etc
    		$this->defaults = array(
    			'save_format' => 'id',
    			'allow_null' => 0,
    			'container' => 'div'
    		);
        	parent::__construct();
    		$this->settings = array(
    			'path' => apply_filters('acf/helpers/get_path', __FILE__),
    			'dir' => apply_filters('acf/helpers/get_dir', __FILE__),
    			'version' => '1.1.2'
    		);
    	} 
    
    	function render_field_settings( $field )
    	{
    		$field = array_merge($this->defaults, $field);
    		$key = $field['name']; 
    
    		// Create Field Options HTML
    		acf_render_field_setting( $field, array(
    			'label'			=> __('Return Value','acf'),
    			'type'		=>	'radio',
    			'name'		=>	'fields['.$key.'][save_format]',
    			'value'		=>	$field['save_format'],
    			'layout'	=>	'horizontal',
    			'choices' 	=>	array(
    				'object'	=>	__("Nav Menu Object",'acf'),
    				'menu'		=>	__("Nav Menu HTML",'acf'),
    				'id'		=>	__("Nav Menu ID",'acf')
    			)
    		));
    
    		$choices = $this->get_allowed_nav_container_tags();
    		acf_render_field_setting( $field, array(
    			'label'			=> __('Menu Container','acf'),
    			'instructions'	=> __('What to wrap the Menu\'s ul with.<br />Only used when returning HTML.','acf'),
    			'type'		=>	'select',
    			'name'		=>	'fields['.$key.'][container]',
    			'value'		=>	$field['container'],
    			'choices' 	=>	$choices
    		)); 
    
    		acf_render_field_setting( $field, array(
    			'label'			=> __('Allow Null?','acf'),
    			'type'	=>	'radio',
    			'name'	=>	'fields['.$key.'][allow_null]',
    			'value'	=>	$field['allow_null'],
    			'choices'	=>	array(
    				1	=>	__("Yes",'acf'),
    				0	=>	__("No",'acf'),
    			),
    			'layout'	=>	'horizontal',
    		));
    	}
    
    	function render_field( $field )
    	{
    		// create Field HTML
    		echo sprintf( '<select id="%d" class="%s" name="%s">', $field['id'], $field['class'], $field['name']  );
    
    		if( $field['allow_null'] )
    		{
    			echo '<option value=""> - Select - </option>';
    		}
    
    		// Nav Menus
    		$nav_menus = $this->get_nav_menus();
    
    		foreach( $nav_menus as $nav_menu_id => $nav_menu_name ) {
    			$selected = selected( $field['value'], $nav_menu_id );
    			echo sprintf( '<option value="%1$d" %3$s>%2$s</option>', $nav_menu_id, $nav_menu_name, $selected );
    		}
    
    		echo '</select>';
    	}
    
    	function get_nav_menus() {
    		$navs = get_terms('nav_menu', array( 'hide_empty' => false ) );
    
    		$nav_menus = array();
    		foreach( $navs as $nav ) {
    			$nav_menus[ $nav->term_id ] = $nav->name;
    		}
    
    		return $nav_menus;
    	}
    
    	function get_allowed_nav_container_tags() {
    		$tags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) );
    		$formatted_tags = array(
    			array( '0' => 'None' )
    		);
    		foreach( $tags as $tag ) {
        		$formatted_tags[0][$tag] = ucfirst( $tag );
    		}
    		return $formatted_tags;
    	}
    
    	function format_value_for_api( $value, $post_id, $field )
    	{
    		$field = array_merge($this->defaults, $field);
    
    		if( !$value ) {
    			return false;
    		}
    
    		// check format
    		if( $field['save_format'] == 'object' ) {
    			$wp_menu_object = wp_get_nav_menu_object( $value );
    
    			if( !$wp_menu_object ) {
    				return false;
    			}
    
    			$menu_object = new stdClass;
    
    			$menu_object->ID = $wp_menu_object->term_id;
    			$menu_object->name = $wp_menu_object->name;
    			$menu_object->slug = $wp_menu_object->slug;
    			$menu_object->count = $wp_menu_object->count;
    
    			return $menu_object;
    
    		} elseif( $field['save_format'] == 'menu' ) {
    			ob_start();
    			wp_nav_menu( array(
    				'menu' => $value,
    				'container' => $field['container']
    			) );
    			return ob_get_clean();
    		}
    		return $value;
    	}
    }
    
    // create field
    new acf_field_nav_menu_v5();

    And that should be it.

    (Optional) I was able to get the values back out by using:

    <?php
    
    $post_id = 259; // your post id here
    $acf_field_id = 'sidebar_menu'; // your acf field id here
    
    $field = get_field($acf_field_id, $post_id);
    if($field){
    	$items = wp_get_nav_menu_items($field );
    	if($items){
    		// view menu as json
    		// echo json_encode($items);
    
    		// OR grab all the titles
    		$titles = array();
    		foreach($items as $key => $value){
    			$titles[] = $value ->title;
    		}
    		print_r($titles);
    	}
    }

    Hope this helps someone,
    Enjoy!

    I can’t seem to edit the post above so I’ll supply a fix for #2 here. ACF had changed the way variables are stored and recalled. In addition, the function ‘format_value_for_api’ was changed to ‘format_value’.

    #2 [FIXED]
    /plugins/advanced-custom-fields-nav-menu-field/nav-menu-v5.php

    <?php
    // ACF Pro v5 Port by Jesse Graupmann (https://justgooddesign.com)
    // Original from https://faisonz.com/wordpress-plugins/advanced-custom-fields-nav-menu-field/ by Faison Zutavern (https://faisonz.com)
    
    class acf_field_nav_menu_v5 extends acf_field
    {
    	var $settings,$defaults;
    	function __construct()
    	{
    		// vars
    		$this->name = 'nav_menu';
    		$this->label = __('Nav Menu', 'acf');
    		$this->category = __("Relational",'acf'); // Basic, Content, Choice, etc
    		$this->defaults = array(
    			'save_format' => 'id',
    			'allow_null' => 0,
    			'container' => 'div'
    		);
    
        	parent::__construct();
    
    		$this->settings = array(
    			'path' => apply_filters('acf/helpers/get_path', __FILE__),
    			'dir' => apply_filters('acf/helpers/get_dir', __FILE__),
    			'version' => '1.1.2'
    		);
    	} 
    
    	function render_field_settings( $field )
    	{
    		// Create Field Options HTML
    		acf_render_field_setting( $field, array(
    			'label'			=> __('Return Value','acf'),
    			'type'		=>	'radio',
    			'name'		=> 'save_format',
    			'layout'	=>	'horizontal',
    			'choices' 	=>	array(
    				'object'	=>	__("Nav Menu Object",'acf'),
    				'menu'		=>	__("Nav Menu HTML",'acf'),
    				'id'		=>	__("Nav Menu ID",'acf')
    			)
    		));
    
    		$choices = $this->get_allowed_nav_container_tags();
    		$usingMenu = $field['save_format'] === 'menu' ? '*' : '.';
    		acf_render_field_setting( $field, array(
    			'label'			=> __('Menu Container','acf'),
    			'instructions'	=> __('What to wrap the Menu\'s ul with.<br />Only used when returning HTML' . $usingMenu,'acf'),
    			'type'		=>	'select',
    			'name'		=> 'container',
    			'choices' 	=>	$choices
    		)); 
    
    		acf_render_field_setting( $field, array(
    			'label'			=> __('Allow Null?','acf'),
    			'type'	=>	'radio',
    			'name'		=> 'allow_null',
    			'choices'	=>	array(
    				1	=>	__("Yes",'acf'),
    				0	=>	__("No",'acf'),
    			),
    			'layout'	=>	'horizontal',
    		));
    	}
    
    	function render_field( $field )
    	{
    		// create Field HTML
    		echo sprintf( '<select id="%d" class="%s" name="%s">', $field['id'], $field['class'], $field['name']  );
    
    		if( $field['allow_null'] )
    		{
    			echo '<option value=""> - Select - </option>';
    		}
    
    		// Nav Menus
    		$nav_menus = $this->get_nav_menus();
    
    		foreach( $nav_menus as $nav_menu_id => $nav_menu_name ) {
    			$selected = selected( $field['value'], $nav_menu_id );
    			echo sprintf( '<option value="%1$d" %3$s>%2$s</option>', $nav_menu_id, $nav_menu_name, $selected );
    		}
    
    		echo '</select>';
    	}
    
    	function get_nav_menus() {
    		$navs = get_terms('nav_menu', array( 'hide_empty' => false ) );
    
    		$nav_menus = array();
    		foreach( $navs as $nav ) {
    			$nav_menus[ $nav->term_id ] = $nav->name;
    		}
    
    		return $nav_menus;
    	}
    
    	function get_allowed_nav_container_tags() {
    		$tags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) );
    		$formatted_tags = array(
    			array( '0' => 'None' )
    		);
    		foreach( $tags as $tag ) {
        		$formatted_tags[0][$tag] = ucfirst( $tag );
    		}
    		return $formatted_tags;
    	}
    
    	function format_value( $value, $post_id, $field )
    	{
    		// defaults
    		$field = array_merge($this->defaults, $field);
    
    		if( !$value ) {
    			return false;
    		}
    
    		// check format
    		if( $field['save_format'] == 'object' ) {
    			$wp_menu_object = wp_get_nav_menu_object( $value );
    
    			if( !$wp_menu_object ) {
    				return false;
    			}
    
    			$menu_object = new stdClass;
    
    			$menu_object->ID = $wp_menu_object->term_id;
    			$menu_object->name = $wp_menu_object->name;
    			$menu_object->slug = $wp_menu_object->slug;
    			$menu_object->count = $wp_menu_object->count;
    
    			return $menu_object;
    
    		} elseif( $field['save_format'] == 'menu' ) {
    
    			ob_start();
    
    			wp_nav_menu( array(
    				'menu' => $value,
    				'container' => $field['container']
    			) );
    
    			return ob_get_clean();
    
    		}
    		return $value;
    	}
    }
    
    // create field
    new acf_field_nav_menu_v5();

    Hi,
    I also am having issues and the plugin is not working with the acf 5 version.
    Will there be a plugin release to have it work with version 5?

    Much appreciated,

    Dan

    I just went ahead and created a GitHub repo that includes the code above + the original plugin.

    https://github.com/jgraup/advanced-custom-fields-nav-menu-field

    Plugin Author Faison

    (@faison)

    Hi Everyone,

    Sorry for my absence. I actually started work on adding ACF v5 support earlier this year, but then I got a job at 10up and had to put my plugins on hold while I settled in.

    Anyways, I’m settled in now and had the time tonight to put ACF Nav Menu Fields on Github. I started a 2.0.0 branch where I added support for ACF v5 (as well as cleaned up all my original code that made babies cry). If any of you are all still around, I was wondering if you could manually install the 2.0.0 branch and tell me if everything appears to be running smoothly? I just want to have a little bit of testing before pushing the changes live to www.remarpro.com.

    You can find the 2.0.0 branch here: https://github.com/Faison/ACF-Nav-Menu-Field/tree/2.0.0

    Andi North

    (@mangopeargroup)

    Hi Faison,

    Thanks for the update, I’ll try and get that installed later today on the site I had issues with.

    I’ll let you know how it goes.

    Panda

    Plugin Author Faison

    (@faison)

    Thanks, I look forward to hearing back from you ??

    Faison: I installed the 2.0 branch and it seems to run flawlessly. Thanks! Do you have an ETA for when 2.0 i released here in the WordPress Plugin directory? I’d rather require it with Composer than just including the files in my repo.

    Andi North

    (@mangopeargroup)

    We installed it on the website and it worked perfectly.

    Time to roll it out across all sites, along with ACF v5 ??

    Thanks,

    Andi

    Plugin Author Faison

    (@faison)

    Hi frebro and Andi (mangopeargroup),

    Thank you for testing 2.0.0, It is now up-to-date on wp.org ??

    Thanks,
    Faison

    Awesomepants. Stellar work with this plugin. Thanks man!

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘ACF 5 compatibility’ is closed to new replies.