Advanced Custom Fields: Nav Menu Field - Version 1.0.0

Version Description

  • Initial Release.

=

Download this release

Release Info

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

Version 1.0.0

Files changed (3) hide show
  1. fz-acf-nav-menu.php +55 -0
  2. nav-menu-v4.php +213 -0
  3. readme.txt +87 -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.0.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,213 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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' => '0.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("Allow Null?",'acf'); ?></label>
94
+ </td>
95
+ <td>
96
+ <?php
97
+ do_action('acf/create_field', array(
98
+ 'type' => 'radio',
99
+ 'name' => 'fields['.$key.'][allow_null]',
100
+ 'value' => $field['allow_null'],
101
+ 'choices' => array(
102
+ 1 => __("Yes",'acf'),
103
+ 0 => __("No",'acf'),
104
+ ),
105
+ 'layout' => 'horizontal',
106
+ ));
107
+ ?>
108
+ </td>
109
+ </tr>
110
+ <?php
111
+
112
+ }
113
+
114
+
115
+ /*
116
+ * create_field()
117
+ *
118
+ * Create the HTML interface for your field
119
+ *
120
+ * @param $field - an array holding all the field's data
121
+ *
122
+ * @type action
123
+ * @since 3.6
124
+ * @date 23/01/13
125
+ */
126
+
127
+ function create_field( $field )
128
+ {
129
+ // defaults?
130
+ /*
131
+ $field = array_merge($this->defaults, $field);
132
+ */
133
+
134
+ // create Field HTML
135
+ echo sprintf( '<select id="%d" class="%s" name="%s">', $field['id'], $field['class'], $field['name'] );
136
+
137
+ // null
138
+ if( $field['allow_null'] )
139
+ {
140
+ echo '<option value="null"> - Select - </option>';
141
+ }
142
+
143
+ // Nav Menus
144
+ $nav_menus = $this->get_nav_menus();
145
+
146
+ foreach( $nav_menus as $nav_menu_id => $nav_menu_name ) {
147
+ $selected = selected( $field['value'], $nav_menu_id );
148
+ echo sprintf( '<option value="%1$d" %3$s>%2$s</option>', $nav_menu_id, $nav_menu_name, $selected );
149
+ }
150
+
151
+ echo '</select>';
152
+ }
153
+
154
+ function get_nav_menus() {
155
+ $navs = get_terms('nav_menu', array( 'hide_empty' => false ) );
156
+
157
+ $nav_menus = array();
158
+ foreach( $navs as $nav ) {
159
+ $nav_menus[ $nav->term_id ] = $nav->name;
160
+ }
161
+
162
+ return $nav_menus;
163
+ }
164
+
165
+ function format_value_for_api( $value, $post_id, $field )
166
+ {
167
+ // defaults
168
+ $field = array_merge($this->defaults, $field);
169
+
170
+ if( !$value ) {
171
+ return false;
172
+ }
173
+
174
+ // check format
175
+ if( $field['save_format'] == 'object' ) {
176
+ $wp_menu_object = wp_get_nav_menu_object( $value );
177
+
178
+ if( !$wp_menu_object ) {
179
+ return false;
180
+ }
181
+
182
+ $menu_object = new stdClass;
183
+
184
+ $menu_object->ID = $wp_menu_object->term_id;
185
+ $menu_object->name = $wp_menu_object->name;
186
+ $menu_object->slug = $wp_menu_object->slug;
187
+ $menu_object->count = $wp_menu_object->count;
188
+
189
+ return $menu_object;
190
+
191
+ } elseif( $field['save_format'] == 'menu' ) {
192
+
193
+ ob_start();
194
+
195
+ wp_nav_menu( array(
196
+ 'menu' => $value
197
+ ) );
198
+
199
+ return ob_get_clean();
200
+
201
+ }
202
+
203
+ return $value;
204
+ }
205
+
206
+
207
+ }
208
+
209
+
210
+ // create field
211
+ new acf_field_nav_menu();
212
+
213
+ ?>
readme.txt ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.0.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.0.0 =
82
+ * Initial Release.
83
+
84
+ == Upgrade Notice ==
85
+
86
+ = 1.0.0 =
87
+ 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!