Advanced Custom Fields: Nav Menu Field - Version 1.1.0

Version Description

  • Added a field which allows users to choose the containing element for the Menu's ul. See wp_nav_menu's container parameter
Download this release

Release Info

Developer Faison
Plugin Icon wp plugin Advanced Custom Fields: Nav Menu Field
Version 1.1.0
Comparing to
See all releases

Version 1.1.0

Files changed (3) hide show
  1. fz-acf-nav-menu.php +55 -0
  2. nav-menu-v4.php +246 -0
  3. readme.txt +93 -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,246 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ );
29
+
30
+
31
+ // do not delete!
32
+ parent::__construct();
33
+
34
+
35
+ // settings
36
+ $this->settings = array(
37
+ 'path' => apply_filters('acf/helpers/get_path', __FILE__),
38
+ 'dir' => apply_filters('acf/helpers/get_dir', __FILE__),
39
+ 'version' => '1.1.0'
40
+ );
41
+
42
+ }
43
+
44
+
45
+ /*
46
+ * create_options()
47
+ *
48
+ * Create extra options for your field. This is rendered when editing a field.
49
+ * The value of $field['name'] can be used (like bellow) to save extra data to the $field
50
+ *
51
+ * @type action
52
+ * @since 3.6
53
+ * @date 23/01/13
54
+ *
55
+ * @param $field - an array holding all the field's data
56
+ */
57
+
58
+ function create_options( $field )
59
+ {
60
+ // defaults?
61
+ $field = array_merge($this->defaults, $field);
62
+
63
+ // key is needed in the field names to correctly save the data
64
+ $key = $field['name'];
65
+
66
+
67
+ // Create Field Options HTML
68
+ ?>
69
+ <tr class="field_option field_option_<?php echo $this->name; ?>">
70
+ <td class="label">
71
+ <label><?php _e("Return Value",'acf'); ?></label>
72
+ </td>
73
+ <td>
74
+ <?php
75
+
76
+ do_action('acf/create_field', array(
77
+ 'type' => 'radio',
78
+ 'name' => 'fields['.$key.'][save_format]',
79
+ 'value' => $field['save_format'],
80
+ 'layout' => 'horizontal',
81
+ 'choices' => array(
82
+ 'object' => __("Nav Menu Object",'acf'),
83
+ 'menu' => __("Nav Menu HTML",'acf'),
84
+ 'id' => __("Nav Menu ID",'acf')
85
+ )
86
+ ));
87
+
88
+ ?>
89
+ </td>
90
+ </tr>
91
+ <tr class="field_option field_option_<?php echo $this->name; ?>">
92
+ <td class="label">
93
+ <label><?php _e("Menu Container",'acf'); ?></label>
94
+ <p class="description">What to wrap the Menu's ul with.<br />Only used when returning HTML.</p>
95
+ </td>
96
+ <td>
97
+ <?php
98
+
99
+ $choices = $this->get_allowed_nav_container_tags();
100
+
101
+ do_action('acf/create_field', array(
102
+ 'type' => 'select',
103
+ 'name' => 'fields['.$key.'][container]',
104
+ 'value' => $field['container'],
105
+ 'layout' => 'horizontal',
106
+ 'choices' => $choices
107
+ ));
108
+
109
+ ?>
110
+ </td>
111
+ </tr>
112
+ <tr class="field_option field_option_<?php echo $this->name; ?>">
113
+ <td class="label">
114
+ <label><?php _e("Allow Null?",'acf'); ?></label>
115
+ </td>
116
+ <td>
117
+ <?php
118
+ do_action('acf/create_field', array(
119
+ 'type' => 'radio',
120
+ 'name' => 'fields['.$key.'][allow_null]',
121
+ 'value' => $field['allow_null'],
122
+ 'choices' => array(
123
+ 1 => __("Yes",'acf'),
124
+ 0 => __("No",'acf'),
125
+ ),
126
+ 'layout' => 'horizontal',
127
+ ));
128
+ ?>
129
+ </td>
130
+ </tr>
131
+ <?php
132
+
133
+ }
134
+
135
+
136
+ /*
137
+ * create_field()
138
+ *
139
+ * Create the HTML interface for your field
140
+ *
141
+ * @param $field - an array holding all the field's data
142
+ *
143
+ * @type action
144
+ * @since 3.6
145
+ * @date 23/01/13
146
+ */
147
+
148
+ function create_field( $field )
149
+ {
150
+ // defaults?
151
+ /*
152
+ $field = array_merge($this->defaults, $field);
153
+ */
154
+
155
+ // create Field HTML
156
+ echo sprintf( '<select id="%d" class="%s" name="%s">', $field['id'], $field['class'], $field['name'] );
157
+
158
+ // null
159
+ if( $field['allow_null'] )
160
+ {
161
+ echo '<option value="null"> - Select - </option>';
162
+ }
163
+
164
+ // Nav Menus
165
+ $nav_menus = $this->get_nav_menus();
166
+
167
+ foreach( $nav_menus as $nav_menu_id => $nav_menu_name ) {
168
+ $selected = selected( $field['value'], $nav_menu_id );
169
+ echo sprintf( '<option value="%1$d" %3$s>%2$s</option>', $nav_menu_id, $nav_menu_name, $selected );
170
+ }
171
+
172
+ echo '</select>';
173
+ }
174
+
175
+ function get_nav_menus() {
176
+ $navs = get_terms('nav_menu', array( 'hide_empty' => false ) );
177
+
178
+ $nav_menus = array();
179
+ foreach( $navs as $nav ) {
180
+ $nav_menus[ $nav->term_id ] = $nav->name;
181
+ }
182
+
183
+ return $nav_menus;
184
+ }
185
+
186
+ function get_allowed_nav_container_tags() {
187
+ $tags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) );
188
+ $formatted_tags = array(
189
+ array( '0' => 'None' )
190
+ );
191
+ foreach( $tags as $tag ) {
192
+ $formatted_tags[0][$tag] = ucfirst( $tag );
193
+ }
194
+ return $formatted_tags;
195
+ }
196
+
197
+ function format_value_for_api( $value, $post_id, $field )
198
+ {
199
+ // defaults
200
+ $field = array_merge($this->defaults, $field);
201
+
202
+ if( !$value ) {
203
+ return false;
204
+ }
205
+
206
+ // check format
207
+ if( $field['save_format'] == 'object' ) {
208
+ $wp_menu_object = wp_get_nav_menu_object( $value );
209
+
210
+ if( !$wp_menu_object ) {
211
+ return false;
212
+ }
213
+
214
+ $menu_object = new stdClass;
215
+
216
+ $menu_object->ID = $wp_menu_object->term_id;
217
+ $menu_object->name = $wp_menu_object->name;
218
+ $menu_object->slug = $wp_menu_object->slug;
219
+ $menu_object->count = $wp_menu_object->count;
220
+
221
+ return $menu_object;
222
+
223
+ } elseif( $field['save_format'] == 'menu' ) {
224
+
225
+ ob_start();
226
+
227
+ wp_nav_menu( array(
228
+ 'menu' => $value,
229
+ 'container' => $field['container']
230
+ ) );
231
+
232
+ return ob_get_clean();
233
+
234
+ }
235
+
236
+ return $value;
237
+ }
238
+
239
+
240
+ }
241
+
242
+
243
+ // create field
244
+ new acf_field_nav_menu();
245
+
246
+ ?>
readme.txt ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.0
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.0 =
82
+ * 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)
83
+
84
+ = 1.0.0 =
85
+ * Initial Release.
86
+
87
+ == Upgrade Notice ==
88
+
89
+ = 1.1.0 =
90
+ Added a new minor feature for selecting the Menu's containing element.
91
+
92
+ = 1.0.0 =
93
+ 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!