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

Can you provide a small example on how to use the hook for menu item #35

Open
Metin-Ljapo opened this issue Nov 1, 2016 · 1 comment
Labels

Comments

@Metin-Ljapo
Copy link

I tried to use the mentioned hook but didnt have any success an example would be nice thanks

@sarahannnicholson
Copy link
Contributor

Hey @Metin-Ljapo,

Here's how I used the hooks in V2.
In my custom WordPress plugin, I added this code

// Add ACF Image Field to Menu Item endpoint	(eg /wp-api-menus/v2/menus/:id items[])
add_filter('rest_menus_format_menu_item', 'add_acf_to_menu_item', 100, 1);
if (!function_exists('add_acf_to_menu_item')) {
    function add_acf_to_menu_item($item) {
		$acf_field = get_field( 'Image_Field',  $item['id'] );

		$item['acf'] = $acf_field;
		return $item;
    }
}

// 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;
    }
}

There's a 3rd hook in V2, which is rest_menus_format_menus to alter the list of menu items returned (e.g from wp-json/wp-api-menus/v2/menus/)

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

No branches or pull requests

3 participants