wp-rest-api-v2-menus icon indicating copy to clipboard operation
wp-rest-api-v2-menus copied to clipboard

Translations

Open millerf opened this issue 4 years ago • 6 comments

Hi there,

First of all, thanks for your work. It is an awesome plugin!

I am trying to retrieve my translated menus through the API, and I don't seem to be able to do it.

I am using TranslatePress for the translation, and although the translation works on WP front, I don't get any translated text via the API.

I am using /{lang}/wp-json/menus/v1/menus/primary to retrieve the menus...

Could someone help out?

millerf avatar Jun 10 '20 12:06 millerf

Hi, I'm sorry but this plugin is not compatible with TranslatePress.

thebatclaudio avatar Jun 16 '20 12:06 thebatclaudio

For those interested, I made it byt overriding wp_api_v2_menus_get_menu_items like this:

function wp_api_v2_menus_get_menu_items( $id ) {
	$menu_items = wp_get_nav_menu_items( $id );
	// check if there is acf installed
	if ( class_exists( 'acf' ) ) {
		foreach ( $menu_items as $menu_key => $menu_item ) {
			$fields = get_fields( $menu_item->ID );
			if ( ! empty( $fields ) ) {
				foreach ( $fields as $field_key => $item ) {
					// add all acf custom fields
					$menu_items[ $menu_key ]->$field_key = $item;
				}
			}
		}
	}

	// wordpress does not group child menu items with parent menu items
	$child_items = [];
	// pull all child menu items into separate object
	foreach ( $menu_items as $key => $item ) {
		$item->data = array(
			'title' => array(
				'text'=>$item->title,
				'rendered' => $item->title
				)
			);
		$item = apply_filters('rest_prepare_nav_menu_item', $item);
		if($item->type == 'post_type') {
			// add slug to menu items
			$slug = basename( get_permalink($item->object_id) );
			$item->slug = $slug;
		} else if($item->type == 'taxonomy') {
			$cat = get_term($item->object_id);
			$item->slug = $cat->slug;
		} else if($item->type == 'post_type_archive') {
			$post_type_data = get_post_type_object($item->object);

			if ($post_type_data->has_archive) {
				$item->slug = $post_type_data->rewrite['slug'];
			}
		}

		if (isset($item->thumbnail_id) && $item->thumbnail_id) {
			$item->thumbnail_src = wp_get_attachment_image_url(intval($item->thumbnail_id), 'post-thumbnail');
		}
		if (isset($item->thumbnail_hover_id) && $item->thumbnail_hover_id) {
			$item->thumbnail_hover_src = wp_get_attachment_image_url(intval($item->thumbnail_hover_id), 'post-thumbnail');
		}

		if ( $item->menu_item_parent ) {
			array_push( $child_items, $item );
			unset( $menu_items[ $key ] );
		}

	}

	// push child items into their parent item in the original object
	do {
		foreach($child_items as $key => $child_item) {
			if(tq_wp_api_v2_menus_dna_test($menu_items, $child_item)) {
				unset($child_items[$key]);
			}
		}
	} while(count($child_items));

	return array_values($menu_items);
}

Note the:

		$item->data = array(
			'title' => array(
				'text'=>$item->title,
				'rendered' => $item->title
				)
			);
		$item = apply_filters('rest_prepare_nav_menu_item', $item);

That is what translates it.

millerf avatar Jun 16 '20 12:06 millerf

I leave it here, maybe we can integrate it. Thank you

thebatclaudio avatar Jun 16 '20 12:06 thebatclaudio

Can you help me to understand? Where is defined this filter: rest_prepare_nav_menu_item?

thebatclaudio avatar Jun 16 '20 12:06 thebatclaudio

You can see the documentation here

millerf avatar Jun 16 '20 12:06 millerf

Great! Thank you!

thebatclaudio avatar Jun 16 '20 13:06 thebatclaudio