wp_get_nav_menu_object example

// Add Menu.         if ( 0 === $nav_menu_selected_id ) {
            $new_menu_title = trim( esc_html( $_POST['menu-name'] ) );

            if ( $new_menu_title ) {
                $_nav_menu_selected_id = wp_update_nav_menu_object( 0, array( 'menu-name' => $new_menu_title ) );

                if ( is_wp_error( $_nav_menu_selected_id ) ) {
                    $messages[] = '<div id="message" class="error notice is-dismissible"><p>' . $_nav_menu_selected_id->get_error_message() . '</p></div>';
                } else {
                    $_menu_object            = wp_get_nav_menu_object( $_nav_menu_selected_id );
                    $nav_menu_selected_id    = $_nav_menu_selected_id;
                    $nav_menu_selected_title = $_menu_object->name;

                    if ( isset( $_REQUEST['menu-item'] ) ) {
                        wp_save_nav_menu_items( $nav_menu_selected_idabsint( $_REQUEST['menu-item'] ) );
                    }

                    if ( isset( $_REQUEST['zero-menu-state'] ) || ! empty( $_POST['auto-add-pages'] ) ) {
                        // If there are menu items, add them.                         wp_nav_menu_update_menu_items( $nav_menu_selected_id$nav_menu_selected_title );
                    }

                    


/** * Returns the menu formatted to edit. * * @since 3.0.0 * * @param int $menu_id Optional. The ID of the menu to format. Default 0. * @return string|WP_Error The menu formatted to edit or error object on failure. */
function wp_get_nav_menu_to_edit( $menu_id = 0 ) {
    $menu = wp_get_nav_menu_object( $menu_id );

    // If the menu exists, get its items.     if ( is_nav_menu( $menu ) ) {
        $menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'post_status' => 'any' ) );
        $result     = '<div id="menu-instructions" class="post-body-plain';
        $result    .= ( ! empty( $menu_items ) ) ? ' menu-instructions-inactive">' : '">';
        $result    .= '<p>' . __( 'Add menu items from the column on the left.' ) . '</p>';
        $result    .= '</div>';

        if ( empty( $menu_items ) ) {
            return $result . ' <ul class="menu" id="menu-to-edit"> </ul>';
        }
/** * Outputs the content for the current Navigation Menu widget instance. * * @since 3.0.0 * * @param array $args Display arguments including 'before_title', 'after_title', * 'before_widget', and 'after_widget'. * @param array $instance Settings for the current Navigation Menu widget instance. */
    public function widget( $args$instance ) {
        // Get menu.         $nav_menu = ! empty( $instance['nav_menu'] ) ? wp_get_nav_menu_object( $instance['nav_menu'] ) : false;

        if ( ! $nav_menu ) {
            return;
        }

        $default_title = __( 'Menu' );
        $title         = ! empty( $instance['title'] ) ? $instance['title'] : '';

        /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
        $title = apply_filters( 'widget_title', $title$instance$this->id_base );

        

    protected function get_term( $id ) {
        $term = parent::get_term( $id );

        if ( is_wp_error( $term ) ) {
            return $term;
        }

        $nav_term           = wp_get_nav_menu_object( $term );
        $nav_term->auto_add = $this->get_menu_auto_add( $nav_term->term_id );

        return $nav_term;
    }

    /** * Checks whether the current user has read permission for the endpoint. * * This allows for any user that can `edit_theme_options` or edit any REST API available post type. * * @since 5.9.0 * * @param WP_REST_Request $request Full details about the request. * @return bool|WP_Error Whether the current user has permission. */
        // `wp_nav_menu()` and `gutenberg_output_block_nav_menu`.
        // Find the location in the list of locations, returning early if the         // location can't be found.         $locations = get_nav_menu_locations();
        if ( ! isset( $locations[ $location ] ) ) {
            return;
        }

        // Get the menu from the location, returning early if there is no         // menu or there was an error.         $menu = wp_get_nav_menu_object( $locations[ $location ] );
        if ( ! $menu || is_wp_error( $menu ) ) {
            return;
        }

        $menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'update_post_term_cache' => false ) );
        _wp_menu_item_classes_by_context( $menu_items );

        return $menu_items;
    }


    
if ( $undefined === $post_value ) {
                $value = $this->_original_value;
            } else {
                $value = $post_value;
            }
        } else {
            $value = false;

            // Note that a term_id of less than one indicates a nav_menu not yet inserted.             if ( $this->term_id > 0 ) {
                $term = wp_get_nav_menu_object( $this->term_id );

                if ( $term ) {
                    $value = wp_array_slice_assoc( (array) $termarray_keys( $this->default ) );

                    $nav_menu_options  = (array) get_option( 'nav_menu_options', array() );
                    $value['auto_add'] = false;

                    if ( isset( $nav_menu_options['auto_add'] ) && is_array( $nav_menu_options['auto_add'] ) ) {
                        $value['auto_add'] = in_array( $term->term_id, $nav_menu_options['auto_add'], true );
                    }
                }
            }
if ( null !== $nav_menu ) {
        if ( $args->echo ) {
            echo $nav_menu;
            return;
        }

        return $nav_menu;
    }

    // Get the nav menu based on the requested menu.     $menu = wp_get_nav_menu_object( $args->menu );

    // Get the nav menu based on the theme_location.     $locations = get_nav_menu_locations();
    if ( ! $menu && $args->theme_location && $locations && isset( $locations[ $args->theme_location ] ) ) {
        $menu = wp_get_nav_menu_object( $locations[ $args->theme_location ] );
    }

    // Get the first menu that has items if we still can't find a menu.     if ( ! $menu && ! $args->theme_location ) {
        $menus = wp_get_nav_menus();
        foreach ( $menus as $menu_maybe ) {
            

    private static function get_nav_menu_at_primary_location() {
        $locations = get_nav_menu_locations();

        if ( isset( $locations['primary'] ) ) {
            $primary_menu = wp_get_nav_menu_object( $locations['primary'] );

            if ( $primary_menu ) {
                return $primary_menu;
            }
        }

        return null;
    }

    /** * Creates a default Navigation Block Menu fallback. * * @since 6.3.0 * * @return int|WP_Error The post ID of the default fallback menu or a WP_Error object. */

function is_nav_menu( $menu ) {
    if ( ! $menu ) {
        return false;
    }

    $menu_obj = wp_get_nav_menu_object( $menu );

    if (
        $menu_obj &&
        ! is_wp_error( $menu_obj ) &&
        ! empty( $menu_obj->taxonomy ) &&
        'nav_menu' === $menu_obj->taxonomy
    ) {
        return true;
    }

    return false;
}
Home | Imprint | This part of the site doesn't use cookies.