Version Description
- Added ACF v5 class.
- Updated code to follow coding standards
- Updated the ACF v4 class to use the updated code found in the ACF v5 Class
Download this release
Release Info
Developer | Faison |
Plugin | Advanced Custom Fields: Nav Menu Field |
Version | 2.0.0 |
Comparing to | |
See all releases |
Code changes from version 1.1.2 to 2.0.0
- fz-acf-nav-menu.php +36 -45
- nav-menu-v4.php +160 -188
- nav-menu-v5.php +189 -0
- readme.txt +17 -25
fz-acf-nav-menu.php
CHANGED
@@ -1,55 +1,46 @@
|
|
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:
|
7 |
-
Author: Faison Zutavern
|
8 |
-
Author URI: http://faisonz.com
|
9 |
-
License: GPL2 or later
|
10 |
-
*/
|
11 |
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
class
|
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 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
49 |
}
|
50 |
|
51 |
}
|
52 |
|
53 |
-
new
|
54 |
-
|
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: 2.0.0
|
7 |
+
* Author: Faison Zutavern
|
8 |
+
* Author URI: http://faisonz.com
|
9 |
+
* License: GPL2 or later
|
10 |
+
*/
|
11 |
|
12 |
+
// Exit if accessed directly
|
13 |
+
if ( ! defined( 'ABSPATH' ) ) {
|
14 |
+
exit;
|
15 |
+
}
|
16 |
|
17 |
+
class ACF_Nav_Menu_Field_Plugin {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
/**
|
20 |
+
* Adds register hooks for the Nav Menu Field.
|
21 |
+
*/
|
22 |
+
public function __construct() {
|
23 |
+
// version 4
|
24 |
+
add_action( 'acf/register_fields', array( $this, 'register_field_v4' ) );
|
25 |
+
|
26 |
+
// version 5
|
27 |
+
add_action( 'acf/include_field_types', array( $this, 'register_field_v5' ) );
|
28 |
}
|
29 |
+
|
30 |
+
/**
|
31 |
+
* Loads up the Nav Menu Field for ACF v4
|
32 |
+
*/
|
33 |
+
public function register_field_v4() {
|
34 |
+
include_once 'nav-menu-v4.php';
|
35 |
+
}
|
36 |
+
|
37 |
+
/**
|
38 |
+
* Loads up the Nav Menu Field for ACF v5
|
39 |
+
*/
|
40 |
+
public function register_field_v5() {
|
41 |
+
include_once 'nav-menu-v5.php';
|
42 |
}
|
43 |
|
44 |
}
|
45 |
|
46 |
+
new ACF_Nav_Menu_Field_Plugin();
|
|
|
|
nav-menu-v4.php
CHANGED
@@ -1,246 +1,218 @@
|
|
1 |
<?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
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'
|
28 |
-
'container'
|
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.2'
|
41 |
);
|
42 |
|
|
|
43 |
}
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
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 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
<
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
<?php
|
|
|
99 |
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
)
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
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 |
-
|
130 |
-
|
|
|
|
|
|
|
|
|
|
|
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=""> - Select - </option>';
|
162 |
-
}
|
163 |
|
164 |
-
|
165 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
166 |
|
167 |
-
|
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 |
-
|
173 |
-
|
|
|
174 |
|
175 |
-
|
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 |
-
|
187 |
-
|
|
|
|
|
|
|
|
|
|
|
188 |
$formatted_tags = array(
|
189 |
-
|
190 |
);
|
191 |
-
|
192 |
-
|
|
|
193 |
}
|
|
|
194 |
return $formatted_tags;
|
195 |
}
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
200 |
$field = array_merge($this->defaults, $field);
|
201 |
|
202 |
-
if(
|
203 |
return false;
|
204 |
}
|
205 |
|
206 |
// check format
|
207 |
-
if( $field['save_format']
|
208 |
$wp_menu_object = wp_get_nav_menu_object( $value );
|
209 |
|
210 |
-
if(
|
211 |
return false;
|
212 |
}
|
213 |
|
214 |
$menu_object = new stdClass;
|
215 |
|
216 |
-
$menu_object->ID
|
217 |
-
$menu_object->name
|
218 |
-
$menu_object->slug
|
219 |
$menu_object->count = $wp_menu_object->count;
|
220 |
|
221 |
return $menu_object;
|
222 |
|
223 |
-
} elseif( $field['save_format']
|
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 |
-
?>
|
1 |
<?php
|
2 |
+
/**
|
3 |
+
* Nav Menu Field v4
|
4 |
+
*
|
5 |
+
* @package ACF Nav Menu Field
|
6 |
+
*/
|
7 |
+
|
8 |
+
// Exit if accessed directly
|
9 |
+
if ( ! defined( 'ABSPATH' ) ) {
|
10 |
+
exit;
|
11 |
+
}
|
12 |
|
13 |
+
/**
|
14 |
+
* ACF_Field_Nav_Menu_V4 Class
|
15 |
+
*
|
16 |
+
* This class contains all the custom workings for the Nav Menu Field for ACF v4
|
17 |
+
*/
|
18 |
+
class ACF_Field_Nav_Menu_V4 extends acf_field {
|
19 |
+
|
20 |
+
/**
|
21 |
+
* Sets up some default values and delegats work to the parent constructor.
|
22 |
+
*/
|
23 |
+
public function __construct() {
|
24 |
+
$this->name = 'nav_menu';
|
25 |
+
$this->label = __( 'Nav Menu' );
|
26 |
+
$this->category = __( 'Relational' ); // Basic, Content, Choice, etc
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
$this->defaults = array(
|
28 |
'save_format' => 'id',
|
29 |
+
'allow_null' => 0,
|
30 |
+
'container' => 'div',
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
);
|
32 |
|
33 |
+
parent::__construct();
|
34 |
}
|
35 |
+
|
36 |
+
/**
|
37 |
+
* Renders the Nav Menu Field options seen when editing a Nav Menu Field.
|
38 |
+
*
|
39 |
+
* @param array $field The array representation of the current Nav Menu Field.
|
40 |
+
*/
|
41 |
+
public function create_options( $field ) {
|
42 |
+
$field = array_merge( $this->defaults, $field );
|
43 |
+
$key = $field['name'];
|
44 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
// Create Field Options HTML
|
46 |
?>
|
47 |
+
<tr class="field_option field_option_<?php echo esc_attr( $this->name ); ?>">
|
48 |
+
<td class="label">
|
49 |
+
<label><?php _e( 'Return Value' ); ?></label>
|
50 |
+
</td>
|
51 |
+
<td>
|
52 |
+
<?php
|
53 |
+
do_action('acf/create_field', array(
|
54 |
+
'type' => 'radio',
|
55 |
+
'name' => 'fields['.$key.'][save_format]',
|
56 |
+
'value' => $field['save_format'],
|
57 |
+
'layout' => 'horizontal',
|
58 |
+
'choices' => array(
|
59 |
+
'object' => __( 'Nav Menu Object' ),
|
60 |
+
'menu' => __( 'Nav Menu HTML' ),
|
61 |
+
'id' => __( 'Nav Menu ID' ),
|
62 |
+
),
|
63 |
+
) );
|
64 |
+
?>
|
65 |
+
</td>
|
66 |
+
</tr>
|
67 |
+
<tr class="field_option field_option_<?php echo esc_attr( $this->name ); ?>">
|
68 |
+
<td class="label">
|
69 |
+
<label><?php _e( 'Menu Container' ); ?></label>
|
70 |
+
<p class="description"><?php _e( "What to wrap the Menu's ul with (when returning HTML only)" ) ?></p>
|
71 |
+
</td>
|
72 |
+
<td>
|
73 |
+
<?php
|
74 |
+
do_action('acf/create_field', array(
|
75 |
+
'type' => 'select',
|
76 |
+
'name' => 'fields['.$key.'][container]',
|
77 |
+
'value' => $field['container'],
|
78 |
+
'choices' => $this->get_allowed_nav_container_tags(),
|
79 |
+
) );
|
80 |
+
?>
|
81 |
+
</td>
|
82 |
+
</tr>
|
83 |
+
<tr class="field_option field_option_<?php echo esc_attr( $this->name ); ?>">
|
84 |
+
<td class="label">
|
85 |
+
<label><?php _e( 'Allow Null?' ); ?></label>
|
86 |
+
</td>
|
87 |
+
<td>
|
88 |
+
<?php
|
89 |
+
do_action('acf/create_field', array(
|
90 |
+
'type' => 'radio',
|
91 |
+
'name' => 'fields['.$key.'][allow_null]',
|
92 |
+
'value' => $field['allow_null'],
|
93 |
+
'layout' => 'horizontal',
|
94 |
+
'choices' => array(
|
95 |
+
1 => __( 'Yes' ),
|
96 |
+
0 => __( 'No' ),
|
97 |
+
),
|
98 |
+
) );
|
99 |
+
?>
|
100 |
+
</td>
|
101 |
+
</tr>
|
102 |
<?php
|
103 |
+
}
|
104 |
|
105 |
+
/**
|
106 |
+
* Renders the Nav Menu Field.
|
107 |
+
*
|
108 |
+
* @param array $field The array representation of the current Nav Menu Field.
|
109 |
+
*/
|
110 |
+
public function create_field( $field ) {
|
111 |
+
$allow_null = $field['allow_null'];
|
112 |
+
$nav_menus = $this->get_nav_menus( $allow_null );
|
113 |
+
|
114 |
+
if ( empty( $nav_menus ) ) {
|
115 |
+
return;
|
116 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
?>
|
118 |
+
<select id="<?php esc_attr( $field['id'] ); ?>" class="<?php echo esc_attr( $field['class'] ); ?>" name="<?php echo esc_attr( $field['name'] ); ?>">
|
119 |
+
<?php foreach( $nav_menus as $nav_menu_id => $nav_menu_name ) : ?>
|
120 |
+
<option value="<?php echo esc_attr( $nav_menu_id ); ?>" <?php selected( $field['value'], $nav_menu_id ); ?>>
|
121 |
+
<?php echo esc_html( $nav_menu_name ); ?>
|
122 |
+
</option>
|
123 |
+
<?php endforeach; ?>
|
124 |
+
</select>
|
125 |
<?php
|
|
|
126 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
|
128 |
+
/**
|
129 |
+
* Gets a list of Nav Menus indexed by their Nav Menu IDs.
|
130 |
+
*
|
131 |
+
* @param bool $allow_null If true, prepends the null option.
|
132 |
+
*
|
133 |
+
* @return array An array of Nav Menus indexed by their Nav Menu IDs.
|
134 |
+
*/
|
135 |
+
private function get_nav_menus( $allow_null = false ) {
|
136 |
+
$navs = get_terms( 'nav_menu', array( 'hide_empty' => false ) );
|
137 |
|
138 |
+
$nav_menus = array();
|
|
|
|
|
|
|
139 |
|
140 |
+
if ( $allow_null ) {
|
141 |
+
$nav_menus[''] = ' - Select - ';
|
142 |
+
}
|
143 |
|
144 |
+
foreach ( $navs as $nav ) {
|
|
|
|
|
|
|
|
|
145 |
$nav_menus[ $nav->term_id ] = $nav->name;
|
146 |
}
|
147 |
|
148 |
return $nav_menus;
|
149 |
}
|
150 |
|
151 |
+
/**
|
152 |
+
* Get the allowed wrapper tags for use with wp_nav_menu().
|
153 |
+
*
|
154 |
+
* @return array An array of allowed wrapper tags.
|
155 |
+
*/
|
156 |
+
private function get_allowed_nav_container_tags() {
|
157 |
+
$tags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) );
|
158 |
$formatted_tags = array(
|
159 |
+
'0' => 'None',
|
160 |
);
|
161 |
+
|
162 |
+
foreach ( $tags as $tag ) {
|
163 |
+
$formatted_tags[$tag] = ucfirst( $tag );
|
164 |
}
|
165 |
+
|
166 |
return $formatted_tags;
|
167 |
}
|
168 |
+
|
169 |
+
/**
|
170 |
+
* Renders the Nav Menu Field.
|
171 |
+
*
|
172 |
+
* @param int $value The Nav Menu ID selected for this Nav Menu Field.
|
173 |
+
* @param int $post_id The Post ID this $value is associated with.
|
174 |
+
* @param array $field The array representation of the current Nav Menu Field.
|
175 |
+
*
|
176 |
+
* @return mixed The Nav Menu ID, or the Nav Menu HTML, or the Nav Menu Object, or false.
|
177 |
+
*/
|
178 |
+
public function format_value_for_api( $value, $post_id, $field ) {
|
179 |
$field = array_merge($this->defaults, $field);
|
180 |
|
181 |
+
if( empty( $value ) ) {
|
182 |
return false;
|
183 |
}
|
184 |
|
185 |
// check format
|
186 |
+
if( 'object' == $field['save_format'] ) {
|
187 |
$wp_menu_object = wp_get_nav_menu_object( $value );
|
188 |
|
189 |
+
if( empty( $wp_menu_object ) ) {
|
190 |
return false;
|
191 |
}
|
192 |
|
193 |
$menu_object = new stdClass;
|
194 |
|
195 |
+
$menu_object->ID = $wp_menu_object->term_id;
|
196 |
+
$menu_object->name = $wp_menu_object->name;
|
197 |
+
$menu_object->slug = $wp_menu_object->slug;
|
198 |
$menu_object->count = $wp_menu_object->count;
|
199 |
|
200 |
return $menu_object;
|
201 |
|
202 |
+
} elseif( 'menu' == $field['save_format'] ) {
|
|
|
203 |
ob_start();
|
204 |
|
205 |
wp_nav_menu( array(
|
206 |
'menu' => $value,
|
207 |
'container' => $field['container']
|
208 |
) );
|
|
|
|
|
209 |
|
210 |
+
return ob_get_clean();
|
211 |
}
|
212 |
+
|
213 |
+
// Just return the Nav Menu ID
|
214 |
return $value;
|
215 |
}
|
|
|
|
|
216 |
}
|
217 |
|
218 |
+
new ACF_Field_Nav_Menu_V4();
|
|
|
|
|
|
|
|
nav-menu-v5.php
ADDED
@@ -0,0 +1,189 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
/**
|
3 |
+
* Nav Menu Field v5
|
4 |
+
*
|
5 |
+
* @package ACF Nav Menu Field
|
6 |
+
*/
|
7 |
+
|
8 |
+
// Exit if accessed directly
|
9 |
+
if ( ! defined( 'ABSPATH' ) ) {
|
10 |
+
exit;
|
11 |
+
}
|
12 |
+
|
13 |
+
/**
|
14 |
+
* ACF_Field_Nav_Menu_V5 Class
|
15 |
+
*
|
16 |
+
* This class contains all the custom workings for the Nav Menu Field for ACF v5
|
17 |
+
*/
|
18 |
+
class ACF_Field_Nav_Menu_V5 extends acf_field {
|
19 |
+
|
20 |
+
/**
|
21 |
+
* Sets up some default values and delegats work to the parent constructor.
|
22 |
+
*/
|
23 |
+
public function __construct() {
|
24 |
+
$this->name = 'nav_menu';
|
25 |
+
$this->label = __( 'Nav Menu' );
|
26 |
+
$this->category = 'relational';
|
27 |
+
$this->defaults = array(
|
28 |
+
'save_format' => 'id',
|
29 |
+
'allow_null' => 0,
|
30 |
+
'container' => 'div',
|
31 |
+
);
|
32 |
+
|
33 |
+
parent::__construct();
|
34 |
+
}
|
35 |
+
|
36 |
+
/**
|
37 |
+
* Renders the Nav Menu Field options seen when editing a Nav Menu Field.
|
38 |
+
*
|
39 |
+
* @param array $field The array representation of the current Nav Menu Field.
|
40 |
+
*/
|
41 |
+
public function render_field_settings( $field ) {
|
42 |
+
// Register the Return Value format setting
|
43 |
+
acf_render_field_setting( $field, array(
|
44 |
+
'label' => __( 'Return Value' ),
|
45 |
+
'instructions' => __( 'Specify the returned value on front end' ),
|
46 |
+
'type' => 'radio',
|
47 |
+
'name' => 'save_format',
|
48 |
+
'layout' => 'horizontal',
|
49 |
+
'choices' => array(
|
50 |
+
'object' => __( 'Nav Menu Object' ),
|
51 |
+
'menu' => __( 'Nav Menu HTML' ),
|
52 |
+
'id' => __( 'Nav Menu ID' ),
|
53 |
+
),
|
54 |
+
) );
|
55 |
+
|
56 |
+
// Register the Menu Container setting
|
57 |
+
acf_render_field_setting( $field, array(
|
58 |
+
'label' => __( 'Menu Container' ),
|
59 |
+
'instructions' => __( "What to wrap the Menu's ul with (when returning HTML only)" ),
|
60 |
+
'type' => 'select',
|
61 |
+
'name' => 'container',
|
62 |
+
'choices' => $this->get_allowed_nav_container_tags(),
|
63 |
+
) );
|
64 |
+
|
65 |
+
// Register the Allow Null setting
|
66 |
+
acf_render_field_setting( $field, array(
|
67 |
+
'label' => __( 'Allow Null?' ),
|
68 |
+
'type' => 'radio',
|
69 |
+
'name' => 'allow_null',
|
70 |
+
'layout' => 'horizontal',
|
71 |
+
'choices' => array(
|
72 |
+
1 => __( 'Yes' ),
|
73 |
+
0 => __( 'No' ),
|
74 |
+
),
|
75 |
+
) );
|
76 |
+
}
|
77 |
+
|
78 |
+
/**
|
79 |
+
* Get the allowed wrapper tags for use with wp_nav_menu().
|
80 |
+
*
|
81 |
+
* @return array An array of allowed wrapper tags.
|
82 |
+
*/
|
83 |
+
private function get_allowed_nav_container_tags() {
|
84 |
+
$tags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) );
|
85 |
+
$formatted_tags = array(
|
86 |
+
'0' => 'None',
|
87 |
+
);
|
88 |
+
|
89 |
+
foreach ( $tags as $tag ) {
|
90 |
+
$formatted_tags[$tag] = ucfirst( $tag );
|
91 |
+
}
|
92 |
+
|
93 |
+
return $formatted_tags;
|
94 |
+
}
|
95 |
+
|
96 |
+
/**
|
97 |
+
* Renders the Nav Menu Field.
|
98 |
+
*
|
99 |
+
* @param array $field The array representation of the current Nav Menu Field.
|
100 |
+
*/
|
101 |
+
public function render_field( $field ) {
|
102 |
+
$allow_null = $field['allow_null'];
|
103 |
+
$nav_menus = $this->get_nav_menus( $allow_null );
|
104 |
+
|
105 |
+
if ( empty( $nav_menus ) ) {
|
106 |
+
return;
|
107 |
+
}
|
108 |
+
?>
|
109 |
+
<select id="<?php esc_attr( $field['id'] ); ?>" class="<?php echo esc_attr( $field['class'] ); ?>" name="<?php echo esc_attr( $field['name'] ); ?>">
|
110 |
+
<?php foreach( $nav_menus as $nav_menu_id => $nav_menu_name ) : ?>
|
111 |
+
<option value="<?php echo esc_attr( $nav_menu_id ); ?>" <?php selected( $field['value'], $nav_menu_id ); ?>>
|
112 |
+
<?php echo esc_html( $nav_menu_name ); ?>
|
113 |
+
</option>
|
114 |
+
<?php endforeach; ?>
|
115 |
+
</select>
|
116 |
+
<?php
|
117 |
+
}
|
118 |
+
|
119 |
+
/**
|
120 |
+
* Gets a list of Nav Menus indexed by their Nav Menu IDs.
|
121 |
+
*
|
122 |
+
* @param bool $allow_null If true, prepends the null option.
|
123 |
+
*
|
124 |
+
* @return array An array of Nav Menus indexed by their Nav Menu IDs.
|
125 |
+
*/
|
126 |
+
private function get_nav_menus( $allow_null = false ) {
|
127 |
+
$navs = get_terms( 'nav_menu', array( 'hide_empty' => false ) );
|
128 |
+
|
129 |
+
$nav_menus = array();
|
130 |
+
|
131 |
+
if ( $allow_null ) {
|
132 |
+
$nav_menus[''] = ' - Select - ';
|
133 |
+
}
|
134 |
+
|
135 |
+
foreach ( $navs as $nav ) {
|
136 |
+
$nav_menus[ $nav->term_id ] = $nav->name;
|
137 |
+
}
|
138 |
+
|
139 |
+
return $nav_menus;
|
140 |
+
}
|
141 |
+
|
142 |
+
/**
|
143 |
+
* Renders the Nav Menu Field.
|
144 |
+
*
|
145 |
+
* @param int $value The Nav Menu ID selected for this Nav Menu Field.
|
146 |
+
* @param int $post_id The Post ID this $value is associated with.
|
147 |
+
* @param array $field The array representation of the current Nav Menu Field.
|
148 |
+
*
|
149 |
+
* @return mixed The Nav Menu ID, or the Nav Menu HTML, or the Nav Menu Object, or false.
|
150 |
+
*/
|
151 |
+
public function format_value( $value, $post_id, $field ) {
|
152 |
+
// bail early if no value
|
153 |
+
if ( empty( $value ) ) {
|
154 |
+
return false;
|
155 |
+
}
|
156 |
+
|
157 |
+
// check format
|
158 |
+
if ( 'object' == $field['save_format'] ) {
|
159 |
+
$wp_menu_object = wp_get_nav_menu_object( $value );
|
160 |
+
|
161 |
+
if( empty( $wp_menu_object ) ) {
|
162 |
+
return false;
|
163 |
+
}
|
164 |
+
|
165 |
+
$menu_object = new stdClass;
|
166 |
+
|
167 |
+
$menu_object->ID = $wp_menu_object->term_id;
|
168 |
+
$menu_object->name = $wp_menu_object->name;
|
169 |
+
$menu_object->slug = $wp_menu_object->slug;
|
170 |
+
$menu_object->count = $wp_menu_object->count;
|
171 |
+
|
172 |
+
return $menu_object;
|
173 |
+
} elseif ( 'menu' == $field['save_format'] ) {
|
174 |
+
ob_start();
|
175 |
+
|
176 |
+
wp_nav_menu( array(
|
177 |
+
'menu' => $value,
|
178 |
+
'container' => $field['container']
|
179 |
+
) );
|
180 |
+
|
181 |
+
return ob_get_clean();
|
182 |
+
}
|
183 |
+
|
184 |
+
// Just return the Nav Menu ID
|
185 |
+
return $value;
|
186 |
+
}
|
187 |
+
}
|
188 |
+
|
189 |
+
new ACF_Field_Nav_Menu_V5();
|
readme.txt
CHANGED
@@ -1,11 +1,11 @@
|
|
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:
|
8 |
-
Stable tag:
|
9 |
License: GPL2 or later
|
10 |
License URI: http://www.gnu.org/licenses/gpl-2.0.html
|
11 |
|
@@ -13,7 +13,7 @@ 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
|
17 |
|
18 |
Using ACF, you can set the Nav Menu Field to return the selected menu's:
|
19 |
|
@@ -29,28 +29,12 @@ Feel free to try this add-on on your dev site, ask questions on the support link
|
|
29 |
|
30 |
This add-on will work with:
|
31 |
|
32 |
-
* version
|
|
|
33 |
|
34 |
== Installation ==
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
= Plugin =
|
39 |
-
1. Copy the 'advanced-custom-fields-nav-menu-field' folder into your plugins folder
|
40 |
-
2. Activate the plugin via the Plugins admin page
|
41 |
-
|
42 |
-
= Include =
|
43 |
-
1. Copy the 'advanced-custom-fields-nav-menu-field' 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 nav-menu-v4.php file)
|
45 |
-
|
46 |
-
`
|
47 |
-
add_action('acf/register_fields', 'my_register_fields');
|
48 |
-
|
49 |
-
function my_register_fields()
|
50 |
-
{
|
51 |
-
include_once('advanced-custom-fields-nav-menu-field/nav-menu-v4.php');
|
52 |
-
}
|
53 |
-
`
|
54 |
|
55 |
== Frequently Asked Questions ==
|
56 |
|
@@ -75,11 +59,11 @@ Finally, create or edit a page, select a menu in the Side Menu field, and view t
|
|
75 |
|
76 |
= Will you make this plugin compatible with Advanced Custom Fields v3? =
|
77 |
|
78 |
-
|
79 |
|
80 |
= Why does the Nav Menu returned by your plugin look like an unstyled list of links? =
|
81 |
|
82 |
-
|
83 |
|
84 |
= I added the Nav Menu Field to Pages, selected my menu when creating a new page, but the menu doesn't show. What gives? =
|
85 |
|
@@ -97,6 +81,11 @@ First, check that you added the necessary ACF code to your templates. If you don
|
|
97 |
|
98 |
== Changelog ==
|
99 |
|
|
|
|
|
|
|
|
|
|
|
100 |
= 1.1.2 =
|
101 |
* Fixed a silly mistake related to allowing Null for a Nav Menu Field. Basically, it was storing the string "null" when you don't select a menu, that's taken care of now.
|
102 |
|
@@ -111,6 +100,9 @@ First, check that you added the necessary ACF code to your templates. If you don
|
|
111 |
|
112 |
== Upgrade Notice ==
|
113 |
|
|
|
|
|
|
|
114 |
= 1.1.1 =
|
115 |
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.
|
116 |
|
1 |
=== Advanced Custom Fields: Nav Menu Field ===
|
2 |
Contributors: Faison
|
3 |
+
Tags: Advanced Custom Fields, acf, acf4, acf5, 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: 4.0
|
8 |
+
Stable tag: 2.0.0
|
9 |
License: GPL2 or later
|
10 |
License URI: http://www.gnu.org/licenses/gpl-2.0.html
|
11 |
|
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 5 & 4), 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 |
|
29 |
|
30 |
This add-on will work with:
|
31 |
|
32 |
+
* version 5
|
33 |
+
* version 4
|
34 |
|
35 |
== Installation ==
|
36 |
|
37 |
+
Follow the following instructions: https://codex.wordpress.org/Managing_Plugins#Installing_Plugins
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
== Frequently Asked Questions ==
|
40 |
|
59 |
|
60 |
= Will you make this plugin compatible with Advanced Custom Fields v3? =
|
61 |
|
62 |
+
No.
|
63 |
|
64 |
= Why does the Nav Menu returned by your plugin look like an unstyled list of links? =
|
65 |
|
66 |
+
So that you can style it yourself. I don't want to step on your toes :)
|
67 |
|
68 |
= I added the Nav Menu Field to Pages, selected my menu when creating a new page, but the menu doesn't show. What gives? =
|
69 |
|
81 |
|
82 |
== Changelog ==
|
83 |
|
84 |
+
= 2.0.0 =
|
85 |
+
* Added ACF v5 class.
|
86 |
+
* Updated code to follow coding standards
|
87 |
+
* Updated the ACF v4 class to use the updated code found in the ACF v5 Class
|
88 |
+
|
89 |
= 1.1.2 =
|
90 |
* Fixed a silly mistake related to allowing Null for a Nav Menu Field. Basically, it was storing the string "null" when you don't select a menu, that's taken care of now.
|
91 |
|
100 |
|
101 |
== Upgrade Notice ==
|
102 |
|
103 |
+
= 2.0.0 =
|
104 |
+
You now have support for ACF v5, and when the code is read, it makes fewer people cry!
|
105 |
+
|
106 |
= 1.1.1 =
|
107 |
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.
|
108 |
|