Skip to content

Instantly share code, notes, and snippets.

@heyjones
Created June 30, 2021 16:59
Show Gist options
  • Save heyjones/58ae68b357e8e3b5db97a5314e1ff40e to your computer and use it in GitHub Desktop.
Save heyjones/58ae68b357e8e3b5db97a5314e1ff40e to your computer and use it in GitHub Desktop.
<?php
namespace WPGraphQL\Menu\Locations;
/**
* Enable all menus in WPGraphQL
*
* This code adds menu support, loops through all available menus
* and programatically syncs them to menu locations to make them
* all accessible through WPGraphQL.
*
* https://www.wpgraphql.com/docs/menus/
*/
add_action( 'after_setup_theme', __NAMESPACE__ . '\\after_setup_theme' );
function after_setup_theme () {
// Enable support for menus
add_theme_support( 'menus' );
// Get all of the menus
$nav_menu = get_terms( 'nav_menu' );
// Create an array of locations
$nav_menus = array_combine( wp_list_pluck( $nav_menu, 'slug' ), wp_list_pluck( $nav_menu, 'name' ) );
// Register the locations
register_nav_menus( $nav_menus );
// Assign the menus to the locations
$nav_menu_locations = array_combine( wp_list_pluck( $nav_menu, 'slug' ), wp_list_pluck( $nav_menu, 'term_id' ) );
// Update the locations
set_theme_mod( 'nav_menu_locations', $nav_menu_locations );
}
@heyjones
Copy link
Author

Turns out there's a much better way of doing this:

https://www.wpgraphql.com/recipes/making-menus-and-menu-items-public/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment