Skip to content

Commit e56046c

Browse files
Add nav menu field to scf
1 parent cdca3f7 commit e56046c

File tree

4 files changed

+243
-0
lines changed

4 files changed

+243
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Nav Menu Field
2+
3+
The Nav Menu Field field provides a way to select nav menus and output them.
4+
5+
## Key Features
6+
7+
- Different Return Value (Object, HTML, ID)
8+
- Different Menu Container (nav, div)
9+
- Ability to select no value
10+
11+
## Settings
12+
13+
- Return Value
14+
- Menu Container
15+
- Allow Null?
16+
17+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Using the Nav Menu Field
2+
3+
## Basic Setup
4+
5+
1. Create a new Field Group
6+
2. Add a Nav Menu field
7+
3. Configure options:
8+
- Set Return Value
9+
- Set Menu Container
10+
- Set of Allow Null is (true/false)
11+
12+
## Common Use Cases
13+
14+
1. Use to output wordpress menu anywhere
15+
16+
## Tips
17+
18+
- to add more Menu Container use ``wp_nav_menu_container_allowed_tags`` hook
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
<?php
2+
/**
3+
* This is a PHP file containing the code for the acf_field_nav_menu class.
4+
*
5+
* @package wordpress/secure-custom-fields
6+
* @since 6.4.1
7+
*/
8+
9+
if ( ! class_exists( 'Acf_Field_Nav_Menu' ) ) :
10+
/**
11+
* Acf Nav menu field class
12+
*/
13+
class Acf_Field_Nav_Menu extends acf_field {
14+
15+
/**
16+
* This function will setup the field type data
17+
*
18+
* @type function
19+
* @date 5/03/2014
20+
* @since ACF 6.4.1
21+
*/
22+
public function initialize() {
23+
24+
// vars
25+
$this->name = 'nav_menu';
26+
$this->label = _x( 'Nav Menu', 'noun', 'secure-custom-fields' );
27+
$this->category = 'choice';
28+
$this->description = __( 'A dropdown list with a selection of menu choices that you can chose.', 'secure-custom-fields' );
29+
$this->preview_image = acf_get_url() . '/assets/images/field-type-previews/field-preview-select.png';
30+
$this->doc_url = 'https://developer.wordpress.org/secure-custom-fields/features/fields/nav_menu/';
31+
$this->tutorial_url = 'https://developer.wordpress.org/secure-custom-fields/features/fields/select/nav_menu-tutorial/';
32+
$this->defaults = array(
33+
'save_format' => 'id',
34+
'allow_null' => 0,
35+
'container' => 'div',
36+
);
37+
}
38+
39+
/**
40+
* Renders the Nav Menu Field options seen when editing a Nav Menu Field.
41+
*
42+
* @param array $field The array representation of the current Nav Menu Field.
43+
*/
44+
public function render_field_settings( $field ) {
45+
// Register the Return Value format setting
46+
acf_render_field_setting(
47+
$field,
48+
array(
49+
'label' => __( 'Return Value', 'secure-custom-fields' ),
50+
'instructions' => __( 'Specify the returned value on front end', 'secure-custom-fields' ),
51+
'type' => 'radio',
52+
'name' => 'save_format',
53+
'layout' => 'horizontal',
54+
'choices' => array(
55+
'object' => __( 'Nav Menu Object', 'secure-custom-fields' ),
56+
'menu' => __( 'Nav Menu HTML', 'secure-custom-fields' ),
57+
'id' => __( 'Nav Menu ID', 'secure-custom-fields' ),
58+
),
59+
)
60+
);
61+
62+
// Register the Menu Container setting
63+
acf_render_field_setting(
64+
$field,
65+
array(
66+
'label' => __( 'Menu Container', 'secure-custom-fields' ),
67+
'instructions' => __( "What to wrap the Menu's ul with (when returning HTML only)", 'secure-custom-fields' ),
68+
'type' => 'select',
69+
'name' => 'container',
70+
'choices' => $this->get_allowed_nav_container_tags(),
71+
)
72+
);
73+
74+
// Register the Allow Null setting
75+
acf_render_field_setting(
76+
$field,
77+
array(
78+
'label' => __( 'Allow Null?', 'secure-custom-fields' ),
79+
'type' => 'radio',
80+
'name' => 'allow_null',
81+
'layout' => 'horizontal',
82+
'choices' => array(
83+
1 => __( 'Yes', 'secure-custom-fields' ),
84+
0 => __( 'No', 'secure-custom-fields' ),
85+
),
86+
)
87+
);
88+
}
89+
/**
90+
* Get the allowed wrapper tags for use with wp_nav_menu().
91+
*
92+
* @return array An array of allowed wrapper tags.
93+
*/
94+
private function get_allowed_nav_container_tags() {
95+
$tags = apply_filters( 'wp_nav_menu_container_allowed_tags', array( 'div', 'nav' ) );
96+
$formatted_tags = array(
97+
'0' => 'None',
98+
);
99+
100+
foreach ( $tags as $tag ) {
101+
$formatted_tags[ $tag ] = $tag;
102+
}
103+
104+
return $formatted_tags;
105+
}
106+
/**
107+
* Renders the Nav Menu Field.
108+
*
109+
* @param array $field The array representation of the current Nav Menu Field.
110+
*/
111+
public function render_field( $field ) {
112+
$allow_null = $field['allow_null'];
113+
$nav_menus = wp_get_nav_menus( $allow_null );
114+
if ( current_theme_supports( 'menus' ) ) {
115+
if ( empty( $nav_menus ) ) {
116+
?>
117+
<div class="acf-notice" >
118+
<p>
119+
<?php esc_html_e( '⚠️ Warning: You don\'t have any menus created please visit', 'secure-custom-fields' ); ?> <a href="<?php echo esc_url( admin_url( 'nav-menus.php' ) ); ?>"><?php esc_html_e( 'this', 'secure-custom-fields' ); ?></a> <?php esc_html_e( 'link to create menus.', 'secure-custom-fields' ); ?>
120+
</p>
121+
</div>
122+
123+
<?php
124+
} else {
125+
?>
126+
<select id="<?php esc_attr( $field['id'] ); ?>" class="<?php echo esc_attr( $field['class'] ); ?>" name="<?php echo esc_attr( $field['name'] ); ?>">
127+
<?php
128+
if ( $allow_null ) {
129+
?>
130+
<option value="">
131+
<?php esc_html_e( '- Select -', 'secure-custom-fields' ); ?>
132+
</option>
133+
<?php
134+
}
135+
foreach ( $nav_menus as $nav_menu_name ) {
136+
?>
137+
<option value="<?php echo esc_attr( $nav_menu_name->term_id ); ?>" <?php selected( $field['value'], $nav_menu_name->term_id ); ?>>
138+
<?php echo esc_html( $nav_menu_name->name ); ?>
139+
</option>
140+
<?php } ?>
141+
</select>
142+
<?php
143+
}
144+
} else {
145+
?>
146+
<div class="acf-notice">
147+
<p>
148+
<?php esc_html_e( '⚠️ Warning: The theme does not support navigation menus.', 'secure-custom-fields' ); ?>
149+
</p>
150+
</div>
151+
<?php
152+
}
153+
}
154+
155+
/**
156+
* Renders the Nav Menu Field.
157+
*
158+
* @param int $value The Nav Menu ID selected for this Nav Menu Field.
159+
* @param int $post_id The Post ID this $value is associated with.
160+
* @param array $field The array representation of the current Nav Menu Field.
161+
*
162+
* @return mixed The Nav Menu ID, or the Nav Menu HTML, or the Nav Menu Object, or false.
163+
*/
164+
public function format_value( $value, $post_id, $field ) {
165+
// bail early if no value
166+
if ( empty( $value ) ) {
167+
return false;
168+
}
169+
170+
// check format
171+
if ( 'object' === $field['save_format'] ) {
172+
$wp_menu_object = wp_get_nav_menu_object( $value );
173+
174+
if ( empty( $wp_menu_object ) ) {
175+
return false;
176+
}
177+
178+
$menu_object = new stdClass();
179+
180+
$menu_object->ID = $wp_menu_object->term_id;
181+
$menu_object->name = $wp_menu_object->name;
182+
$menu_object->slug = $wp_menu_object->slug;
183+
$menu_object->count = $wp_menu_object->count;
184+
185+
return $menu_object;
186+
} elseif ( 'menu' === $field['save_format'] ) {
187+
ob_start();
188+
189+
wp_nav_menu(
190+
array(
191+
'menu' => $value,
192+
'container' => $field['container'],
193+
)
194+
);
195+
196+
return ob_get_clean();
197+
}
198+
199+
// Just return the Nav Menu ID
200+
return $value;
201+
}
202+
}
203+
204+
205+
// initialize
206+
acf_register_field_type( 'Acf_Field_Nav_Menu' );
207+
endif; // class_exists check

secure-custom-fields.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ public function init() {
327327
acf_include( 'includes/fields/class-acf-field-flexible-content.php' );
328328
acf_include( 'includes/fields/class-acf-field-gallery.php' );
329329
acf_include( 'includes/fields/class-acf-field-clone.php' );
330+
acf_include( 'includes/fields/class-acf-field-nav-menu.php' );
330331

331332
/**
332333
* Fires after field types have been included.

0 commit comments

Comments
 (0)