Version Description
- I forgot to add a default value for the Menu Container field, so I added 'div' as the default value. If you upgraded from 1.0.0 to 1.1.0 and had WP_DEBUG enabled, you would receive a warning about an unknown index. Since I like debug mode to run without warnings, I fixed this.
Download this release
Release Info
Developer | Faison |
Plugin | Advanced Custom Fields: Nav Menu Field |
Version | 1.1.1 |
Comparing to | |
See all releases |
Version 1.1.1
- fz-acf-nav-menu.php +55 -0
- nav-menu-v4.php +247 -0
- readme.txt +99 -0
fz-acf-nav-menu.php
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
/*
|
3 |
+
Plugin Name: Advanced Custom Fields: Nav Menu Field
|
4 |
+
Plugin URI: http://faisonz.com/wordpress-plugins/advanced-custom-fields-nav-menu-field/
|
5 |
+
Description: Add-On plugin for Advanced Custom Fields (ACF) that adds a 'Nav Menu' Field type.
|
6 |
+
Version: 1.1.0
|
7 |
+
Author: Faison Zutavern
|
8 |
+
Author URI: http://faisonz.com
|
9 |
+
License: GPL2 or later
|
10 |
+
*/
|
11 |
+
|
12 |
+
|
13 |
+
class acf_field_nav_menu_plugin
|
14 |
+
{
|
15 |
+
/*
|
16 |
+
* Construct
|
17 |
+
*
|
18 |
+
* @description:
|
19 |
+
* @since: 3.6
|
20 |
+
* @created: 1/04/13
|
21 |
+
*/
|
22 |
+
|
23 |
+
function __construct()
|
24 |
+
{
|
25 |
+
// set text domain
|
26 |
+
/*
|
27 |
+
$domain = 'acf-nav_menu';
|
28 |
+
$mofile = trailingslashit(dirname(__File__)) . 'lang/' . $domain . '-' . get_locale() . '.mo';
|
29 |
+
load_textdomain( $domain, $mofile );
|
30 |
+
*/
|
31 |
+
|
32 |
+
|
33 |
+
// version 4+
|
34 |
+
add_action('acf/register_fields', array($this, 'register_fields'));
|
35 |
+
|
36 |
+
}
|
37 |
+
|
38 |
+
/*
|
39 |
+
* register_fields
|
40 |
+
*
|
41 |
+
* @description:
|
42 |
+
* @since: 3.6
|
43 |
+
* @created: 1/04/13
|
44 |
+
*/
|
45 |
+
|
46 |
+
function register_fields()
|
47 |
+
{
|
48 |
+
include_once('nav-menu-v4.php');
|
49 |
+
}
|
50 |
+
|
51 |
+
}
|
52 |
+
|
53 |
+
new acf_field_nav_menu_plugin();
|
54 |
+
|
55 |
+
?>
|
nav-menu-v4.php
ADDED
@@ -0,0 +1,247 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
|
3 |
+
class acf_field_nav_menu extends acf_field
|
4 |
+
{
|
5 |
+
// vars
|
6 |
+
var $settings, // will hold info such as dir / path
|
7 |
+
$defaults; // will hold default field options
|
8 |
+
|
9 |
+
|
10 |
+
/*
|
11 |
+
* __construct
|
12 |
+
*
|
13 |
+
* Set name / label needed for actions / filters
|
14 |
+
*
|
15 |
+
* @since 3.6
|
16 |
+
* @date 23/01/13
|
17 |
+
*/
|
18 |
+
|
19 |
+
function __construct()
|
20 |
+
{
|
21 |
+
// vars
|
22 |
+
$this->name = 'nav_menu';
|
23 |
+
$this->label = __('Nav Menu');
|
24 |
+
$this->category = __("Relational",'acf'); // Basic, Content, Choice, etc
|
25 |
+
$this->defaults = array(
|
26 |
+
'save_format' => 'id',
|
27 |
+
'allow_null' => 0,
|
28 |
+
'container' => 'div'
|
29 |
+
);
|
30 |
+
|
31 |
+
|
32 |
+
// do not delete!
|
33 |
+
parent::__construct();
|
34 |
+
|
35 |
+
|
36 |
+
// settings
|
37 |
+
$this->settings = array(
|
38 |
+
'path' => apply_filters('acf/helpers/get_path', __FILE__),
|
39 |
+
'dir' => apply_filters('acf/helpers/get_dir', __FILE__),
|
40 |
+
'version' => '1.1.1'
|
41 |
+
);
|
42 |
+
|
43 |
+
}
|
44 |
+
|
45 |
+
|
46 |
+
/*
|
47 |
+
* create_options()
|
48 |
+
*
|
49 |
+
* Create extra options for your field. This is rendered when editing a field.
|
50 |
+
* The value of $field['name'] can be used (like bellow) to save extra data to the $field
|
51 |
+
*
|
52 |
+
* @type action
|
53 |
+
* @since 3.6
|
54 |
+
* @date 23/01/13
|
55 |
+
*
|
56 |
+
* @param $field - an array holding all the field's data
|
57 |
+
*/
|
58 |
+
|
59 |
+
function create_options( $field )
|
60 |
+
{
|
61 |
+
// defaults?
|
62 |
+
$field = array_merge($this->defaults, $field);
|
63 |
+
|
64 |
+
// key is needed in the field names to correctly save the data
|
65 |
+
$key = $field['name'];
|
66 |
+
|
67 |
+
|
68 |
+
// Create Field Options HTML
|
69 |
+
?>
|
70 |
+
<tr class="field_option field_option_<?php echo $this->name; ?>">
|
71 |
+
<td class="label">
|
72 |
+
<label><?php _e("Return Value",'acf'); ?></label>
|
73 |
+
</td>
|
74 |
+
<td>
|
75 |
+
<?php
|
76 |
+
|
77 |
+
do_action('acf/create_field', array(
|
78 |
+
'type' => 'radio',
|
79 |
+
'name' => 'fields['.$key.'][save_format]',
|
80 |
+
'value' => $field['save_format'],
|
81 |
+
'layout' => 'horizontal',
|
82 |
+
'choices' => array(
|
83 |
+
'object' => __("Nav Menu Object",'acf'),
|
84 |
+
'menu' => __("Nav Menu HTML",'acf'),
|
85 |
+
'id' => __("Nav Menu ID",'acf')
|
86 |
+
)
|
87 |
+
));
|
88 |
+
|
89 |
+
?>
|
90 |
+
</td>
|
91 |
+
</tr>
|
92 |
+
<tr class="field_option field_option_<?php echo $this->name; ?>">
|
93 |
+
<td class="label">
|
94 |
+
<label><?php _e("Menu Container",'acf'); ?></label>
|
95 |
+
<p class="description">What to wrap the Menu's ul with.<br />Only used when returning HTML.</p>
|
96 |
+
</td>
|
97 |
+
<td>
|
98 |
+
<?php
|
99 |
+
|
100 |
+
$choices = $this->get_allowed_nav_container_tags();
|
101 |
+
|
102 |
+
do_action('acf/create_field', array(
|
103 |
+
'type' => 'select',
|
104 |
+
'name' => 'fields['.$key.'][container]',
|
105 |
+
'value' => $field['container'],
|
106 |
+
'layout' => 'horizontal',
|
107 |
+
'choices' => $choices
|
108 |
+
));
|
109 |
+
|
110 |
+
?>
|
111 |
+
</td>
|
112 |
+
</tr>
|
113 |
+
<tr class="field_option field_option_<?php echo $this->name; ?>">
|
114 |
+
<td class="label">
|
115 |
+
<label><?php _e("Allow Null?",'acf'); ?></label>
|
116 |
+
</td>
|
117 |
+
<td>
|
118 |
+
<?php
|
119 |
+
do_action('acf/create_field', array(
|
120 |
+
'type' => 'radio',
|
121 |
+
'name' => 'fields['.$key.'][allow_null]',
|
122 |
+
'value' => $field['allow_null'],
|
123 |
+
'choices' => array(
|
124 |
+
1 => __("Yes",'acf'),
|
125 |
+
0 => __("No",'acf'),
|
126 |
+
),
|
127 |
+
'layout' => 'horizontal',
|
128 |
+
));
|
129 |
+
?>
|
130 |
+
</td>
|
131 |
+
</tr>
|
132 |
+
<?php
|
133 |
+
|
134 |
+
}
|
135 |
+
|
136 |
+
|
137 |
+
/*
|
138 |
+
* create_field()
|
139 |
+
*
|
140 |
+
* Create the HTML interface for your field
|
141 |
+
*
|
142 |
+
* @param $field - an array holding all the field's data
|
143 |
+
*
|
144 |
+
* @type action
|
145 |
+
* @since 3.6
|
146 |
+
* @date 23/01/13
|
147 |
+
*/
|
148 |
+
|
149 |
+
function create_field( $field )
|
150 |
+
{
|
151 |
+
// defaults?
|
152 |
+
/*
|
153 |
+
$field = array_merge($this->defaults, $field);
|
154 |
+
*/
|
155 |
+
|
156 |
+
// create Field HTML
|
157 |
+
echo sprintf( '<select id="%d" class="%s" name="%s">', $field['id'], $field['class'], $field['name'] );
|
158 |
+
|
159 |
+
// null
|
160 |
+
if( $field['allow_null'] )
|
161 |
+
{
|
162 |
+
echo '<option value="null"> - Select - </option>';
|
163 |
+
}
|
164 |
+
|
165 |
+
// Nav Menus
|
166 |
+
$nav_menus = $this->get_nav_menus();
|
167 |
+
|
168 |
+
foreach( $nav_menus as $nav_menu_id => $nav_menu_name ) {
|
169 |
+
$selected = selected( $field['value'], $nav_menu_id );
|
170 |
+
echo sprintf( '<option value="%1$d" %3$s>%2$s</option>', $nav_menu_id, $nav_menu_name, $selected );
|
171 |
+
}
|
172 |
+
|
173 |
+
echo '</select>';
|
174 |
+
}
|
175 |
+
|
176 |
+
function get_nav_menus() {
|
177 |
+
$navs = get_terms('nav_menu', array( 'hide_empty' => false ) );
|
178 |
+
|
179 |
+
$nav_menus = array();
|
180 |
+
foreach( $navs as $nav ) {
|
181 |
+
$nav_menus[ $nav->term_id ] = $nav->name;
|
182 |
+
}
|
183 |
+
|
184 |
+
return $nav_menus;
|
185 |
+
}
|
186 |
+
|
187 |
+
function get_allowed_nav_container_tags() {
|
188 |
+
$tags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) );
|
189 |
+
$formatted_tags = array(
|
190 |
+
array( '0' => 'None' )
|
191 |
+
);
|
192 |
+
foreach( $tags as $tag ) {
|
193 |
+
$formatted_tags[0][$tag] = ucfirst( $tag );
|
194 |
+
}
|
195 |
+
return $formatted_tags;
|
196 |
+
}
|
197 |
+
|
198 |
+
function format_value_for_api( $value, $post_id, $field )
|
199 |
+
{
|
200 |
+
// defaults
|
201 |
+
$field = array_merge($this->defaults, $field);
|
202 |
+
|
203 |
+
if( !$value ) {
|
204 |
+
return false;
|
205 |
+
}
|
206 |
+
|
207 |
+
// check format
|
208 |
+
if( $field['save_format'] == 'object' ) {
|
209 |
+
$wp_menu_object = wp_get_nav_menu_object( $value );
|
210 |
+
|
211 |
+
if( !$wp_menu_object ) {
|
212 |
+
return false;
|
213 |
+
}
|
214 |
+
|
215 |
+
$menu_object = new stdClass;
|
216 |
+
|
217 |
+
$menu_object->ID = $wp_menu_object->term_id;
|
218 |
+
$menu_object->name = $wp_menu_object->name;
|
219 |
+
$menu_object->slug = $wp_menu_object->slug;
|
220 |
+
$menu_object->count = $wp_menu_object->count;
|
221 |
+
|
222 |
+
return $menu_object;
|
223 |
+
|
224 |
+
} elseif( $field['save_format'] == 'menu' ) {
|
225 |
+
|
226 |
+
ob_start();
|
227 |
+
|
228 |
+
wp_nav_menu( array(
|
229 |
+
'menu' => $value,
|
230 |
+
'container' => $field['container']
|
231 |
+
) );
|
232 |
+
|
233 |
+
return ob_get_clean();
|
234 |
+
|
235 |
+
}
|
236 |
+
|
237 |
+
return $value;
|
238 |
+
}
|
239 |
+
|
240 |
+
|
241 |
+
}
|
242 |
+
|
243 |
+
|
244 |
+
// create field
|
245 |
+
new acf_field_nav_menu();
|
246 |
+
|
247 |
+
?>
|
readme.txt
ADDED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
=== Advanced Custom Fields: Nav Menu Field ===
|
2 |
+
Contributors: Faison
|
3 |
+
Tags: Advanced Custom Fields, acf, acf4, custom fields, admin, menu, nav menu, navigation
|
4 |
+
Author: Faison Zutavern
|
5 |
+
Author URI: http://faisonz.com
|
6 |
+
Requires at least: 3.4
|
7 |
+
Tested up to: 3.5.1
|
8 |
+
Stable tag: 1.1.1
|
9 |
+
License: GPL2 or later
|
10 |
+
License URI: http://www.gnu.org/licenses/gpl-2.0.html
|
11 |
+
|
12 |
+
Add-On plugin for Advanced Custom Fields (ACF) that adds a 'Nav Menu' Field type.
|
13 |
+
|
14 |
+
== Description ==
|
15 |
+
|
16 |
+
Add [Navigation Menus](http://codex.wordpress.org/Navigation_Menus) to [Advanced Custom Fields](http://wordpress.org/extend/plugins/advanced-custom-fields/) (ACF) with the Nav Menu Field plugin! This plugin adds the Nav Menu Field type to ACF (version 4 and up), allowing you to select from the menus you create in the WordPress Admin backend to use on your website's frontend.
|
17 |
+
|
18 |
+
Using ACF, you can set the Nav Menu Field to return the selected menu's:
|
19 |
+
|
20 |
+
* ID for lightweight coding,
|
21 |
+
* Object for more involved programming, or
|
22 |
+
* HTML (generated from [wp_nav_menu](http://codex.wordpress.org/Function_Reference/wp_nav_menu)) for quickly displaying a menu.
|
23 |
+
|
24 |
+
I created this plugin because I needed to display a secondary menu that changed depending on what page you're on. Most of those pages were children of the same page, but then I had to throw a couple of Custom Post Types in there too. Because of the Custom Post Types, I couldn't just grab the top most parent for the current page and use [wp_list_pages](http://codex.wordpress.org/Function_Reference/wp_list_pages). So I did some research and decided to extend the functionality of my favourite plugin, [Advanced Custom Fields](http://wordpress.org/extend/plugins/advanced-custom-fields/). Now when I create a new Page or Custom Post, I just select the menu from a drop down menu!
|
25 |
+
|
26 |
+
Feel free to try this add-on on your dev site, ask questions on the support link above, and please review this add-on. By leaving a rating and review, you help this plugin become even better!
|
27 |
+
|
28 |
+
= Advanced Custom Fields Compatibility =
|
29 |
+
|
30 |
+
This add-on will work with:
|
31 |
+
|
32 |
+
* version 4 and up
|
33 |
+
|
34 |
+
== Installation ==
|
35 |
+
|
36 |
+
This add-on can be treated as both a WP plugin and a theme include.
|
37 |
+
|
38 |
+
= Plugin =
|
39 |
+
1. Copy the 'fz-acf-nav-menu' folder into your plugins folder
|
40 |
+
2. Activate the plugin via the Plugins admin page
|
41 |
+
|
42 |
+
= Include =
|
43 |
+
1. Copy the 'fz-acf-nav-menu' folder into your theme folder (can use sub folders). You can place the folder anywhere inside the 'wp-content' directory
|
44 |
+
2. Edit your functions.php file and add the code below (Make sure the path is correct to include the fz-acf-nav-menu.php file)
|
45 |
+
|
46 |
+
`
|
47 |
+
add_action('acf/register_fields', 'my_register_fields');
|
48 |
+
|
49 |
+
function my_register_fields()
|
50 |
+
{
|
51 |
+
include_once('fz-acf-nav-menu/fz-acf-nav-menu.php');
|
52 |
+
}
|
53 |
+
`
|
54 |
+
|
55 |
+
== Frequently Asked Questions ==
|
56 |
+
|
57 |
+
= Will you make this plugin compatible with Advanced Custom Fields v3? =
|
58 |
+
|
59 |
+
I will do that soon, but you really should think about upgrading Advanced Custom Fields. ACF has seen a lot of great changes from v3 to the most current version.
|
60 |
+
|
61 |
+
= Why does the Nav Menu returned by your plugin look like an unstyled list of links? =
|
62 |
+
|
63 |
+
I decided to return the Nav Menus without any added style, because I don't know what your website looks like. Frankly, I find it annoying when I have to compete with a plugin's styles, especially when a lot of them use !important. Now I can use this plugin on many sites with only a little extra styling work ahead of me.
|
64 |
+
|
65 |
+
= I added the Nav Menu Field to Pages, selected my menu when creating a new page, but the menu doesn't show. What gives? =
|
66 |
+
|
67 |
+
First, check that you added the necessary ACF code to your templates. If you don't know what I'm talking about, read up on how to use [Advanced Custom Fields](http://wordpress.org/extend/plugins/advanced-custom-fields/). If you're already familiar with ACF and you still can't figure out why the menu isn't showing up, start a new support thread, include details and a little code, and I'll do my best to help you.
|
68 |
+
|
69 |
+
== Screenshots ==
|
70 |
+
|
71 |
+
1. Adding the Nav Menu Field to an Advanced Custom Fields Field Group
|
72 |
+
|
73 |
+
2. Creating your menu in the WordPress Admin backend
|
74 |
+
|
75 |
+
3. Selecting the previously created menu in the meta box created by Advanced Custom Fields
|
76 |
+
|
77 |
+
4. Viewing the (not styled) menu displayed by calling 'the_field()' in the sidebar
|
78 |
+
|
79 |
+
== Changelog ==
|
80 |
+
|
81 |
+
= 1.1.1 =
|
82 |
+
* I forgot to add a default value for the Menu Container field, so I added 'div' as the default value. If you upgraded from 1.0.0 to 1.1.0 and had WP_DEBUG enabled, you would receive a warning about an unknown index. Since I like debug mode to run without warnings, I fixed this.
|
83 |
+
|
84 |
+
= 1.1.0 =
|
85 |
+
* Added a field which allows users to choose the containing element for the Menu's ul. See [wp_nav_menu's container parameter](http://codex.wordpress.org/Function_Reference/wp_nav_menu#Parameters)
|
86 |
+
|
87 |
+
= 1.0.0 =
|
88 |
+
* Initial Release.
|
89 |
+
|
90 |
+
== Upgrade Notice ==
|
91 |
+
|
92 |
+
= 1.1.1 =
|
93 |
+
I forgot to add a default value for the Menu Container field. So to eliminate WP_DEBUG warnings, I added 'div' as the default value. Please upgrade to avoid the warnings.
|
94 |
+
|
95 |
+
= 1.1.0 =
|
96 |
+
Added a new minor feature for selecting the Menu's containing element.
|
97 |
+
|
98 |
+
= 1.0.0 =
|
99 |
+
If you have a version less than 0.1.0, something went really, really wrong. Upgrade now, because I have no idea what will happen if you don't!
|