unfulvio / wp-api-menus

:abcd: Menu routes for WordPress JSON REST API.
https://wordpress.org/plugins/wp-api-menus
GNU General Public License v2.0
140 stars 59 forks source link

Add route to return rendered html of a single menu #27

Open Underdawgq opened 8 years ago

Underdawgq commented 8 years ago

I did not test the code for v1 of the REST API.

fahrradflucht commented 7 years ago

@Underdawgq Could you explain what this does and how do you indent someone to use it?

Underdawgq commented 7 years ago

Instead of returning the menu as a tree these routes return the menu in html. If someone wanted to simply mirror a menu from one WordPress site to another this is much easier than setting up a loop to transform the tree into html.

So if site B wanted the same menu as site A then all they would have to do is edit the theme for site B from something like this: wp_nav_menu( array ( 'menu_class' => 'nav-menu', 'menu' => 'menu-slug' ) );

to use something like: // Get json file content $siteAmenu_jsoncontent=file_get_contents('http://www.siteA.com/wp-json/wp-api-menus/v2/menu-html/menu-slug'); //Decode json content into an array $siteAmenu_json=json_decode($siteAmenu_jsoncontent, true); // Optional: Replace generic id and/or class attributes with id and class attributes for this specific menu $siteAmenu_json['html']=str_replace('ul class="menu"', 'ul class="nav-menu"', $siteAmenu_json['html']); // Display menu on screen/in browser echo $siteAmenu_json['html'];