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

WPML support #50

Open laustdeleuran opened 6 years ago

laustdeleuran commented 6 years ago

WP-API supports WPML out of the box, so API queries with the parameter lang=CODE or similar just works. This doesn't seem to work in this plugin. Are there any plans to implement this support or am I missing some way it's already implemented?

isuke01 commented 6 years ago

Yes that would be nice, now you must find correct navi ID and get this navi by ID

sarahannnicholson commented 5 years ago

Hey @laustdeleuran and @isuke01, I came across the same thing earlier today! I'm using V2 of this plugin. Here's how I solved it.

  1. In WordPress, localize your English menu WPML translating menus docs
  2. I created a custom plugin in WordPress. WordPress plugins tutorial
  3. In the my plugin file I added this code:
    // Add Locale to Menu endpoint      (eg /wp-api-menus/v2/menus/:id )
    add_filter('rest_menus_format_menu', 'add_locale_to_menu', 100, 1);
    if (!function_exists('add_locale_to_menu')) {
    function add_locale_to_menu($rest_menu) {
        $args = array('element_id' => $rest_menu['ID'], 'element_type' => 'nav_menu' );
        $rest_menu['locale'] = apply_filters( 'wpml_element_language_code', null, $args );
        return $rest_menu;
    }
    }
retzion commented 3 years ago

Would love to use /wp-api-menus/v2/menus?lang=en when you get a chance. Is it in the roadmap?