Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

Underdawgq
Copy link

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

Also made a correction to the get_menu() function the $rest_url was getting the api namespace instead of the plugin name space
Didn't test the REST API v1 version of this.
…endered in html from the location for the API v1. Also changed "render_html" to "html" in the json output for API v1.

Code hasn't been tested
@fahrradflucht
Copy link
Contributor

fahrradflucht commented Nov 21, 2016

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

@Underdawgq
Copy link
Author

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'];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants