Filters the wp_get_nav_menu_items() result to supply the previewed menu items.
Description
See also
Parameters
Source
public function filter_wp_get_nav_menu_items( $items, $menu, $args ) {
$this_item = $this->value();
$current_nav_menu_term_id = null;
if ( isset( $this_item['nav_menu_term_id'] ) ) {
$current_nav_menu_term_id = $this_item['nav_menu_term_id'];
unset( $this_item['nav_menu_term_id'] );
}
$should_filter = (
$menu->term_id === $this->original_nav_menu_term_id
||
$menu->term_id === $current_nav_menu_term_id
);
if ( ! $should_filter ) {
return $items;
}
// Handle deleted menu item, or menu item moved to another menu.
$should_remove = (
false === $this_item
||
( isset( $this_item['_invalid'] ) && true === $this_item['_invalid'] )
||
(
$this->original_nav_menu_term_id === $menu->term_id
&&
$current_nav_menu_term_id !== $this->original_nav_menu_term_id
)
);
if ( $should_remove ) {
$filtered_items = array();
foreach ( $items as $item ) {
if ( $item->db_id !== $this->post_id ) {
$filtered_items[] = $item;
}
}
return $filtered_items;
}
$mutated = false;
$should_update = (
is_array( $this_item )
&&
$current_nav_menu_term_id === $menu->term_id
);
if ( $should_update ) {
foreach ( $items as $item ) {
if ( $item->db_id === $this->post_id ) {
foreach ( get_object_vars( $this->value_as_wp_post_nav_menu_item() ) as $key => $value ) {
$item->$key = $value;
}
$mutated = true;
}
}
// Not found so we have to append it..
if ( ! $mutated ) {
$items[] = $this->value_as_wp_post_nav_menu_item();
}
}
return $items;
}
Changelog
Version | Description |
---|---|
4.3.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.