Skip to content

Commit a7ead72

Browse files
committed
Navigation block: Add support for passing through a class name
Let sites pass through a class like `current-menu-item` in cases where the "current" page is not a page that can use the logic of `kind: post_type`. See WordPress/wporg-developer#498
1 parent 7d3d925 commit a7ead72

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

mu-plugins/blocks/navigation/index.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ function render_menu_item( $item ) {
122122

123123
$output .= '<!-- /wp:navigation-submenu -->';
124124
} else {
125-
$block_code = '<!-- wp:navigation-link {"label":"%1$s","url":"%2$s","kind":"custom"} /-->';
125+
$block_code = '<!-- wp:navigation-link {"label":"%1$s","url":"%2$s","kind":"custom","className":"%4$s"} /-->';
126126

127127
// If this is a relative link, convert it to absolute and try to find
128128
// the corresponding ID, so that the `current` attributes are used.
@@ -131,7 +131,7 @@ function render_menu_item( $item ) {
131131
$item['url'] = home_url( $item['url'] );
132132
if ( $page_obj ) {
133133
// A page was found, so use the post-type link.
134-
$block_code = '<!-- wp:navigation-link {"label":"%1$s","url":"%2$s","kind":"post-type","id":"%3$s"} /-->';
134+
$block_code = '<!-- wp:navigation-link {"label":"%1$s","url":"%2$s","kind":"post-type","id":"%3$s","className":"%4$s"} /-->';
135135
$item['id'] = $page_obj->ID;
136136
}
137137
}
@@ -140,7 +140,8 @@ function render_menu_item( $item ) {
140140
$block_code,
141141
esc_html( $item['label'] ),
142142
esc_url( $item['url'], ),
143-
isset( $item['id'] ) ? intval( $item['id'] ) : ''
143+
isset( $item['id'] ) ? intval( $item['id'] ) : '',
144+
isset( $item['className'] ) ? esc_attr( $item['className'] ) : '',
144145
);
145146
}
146147

0 commit comments

Comments
 (0)