[WordPress] 外掛分享: WP REST Menus

首頁外掛目錄 › WP REST Menus
WordPress 外掛 WP REST Menus 的封面圖片
100+
安裝啟用
★★★★★
5/5 分(4 則評價)
1487 天前
最後更新
問題解決
WordPress 5.0+ PHP 5.6+ v1.0.4 上架:2018-07-03

內容簡介

此外掛為 WordPress 註冊選單新增端點。
建置 Vuejs、React 或任何前端框架 SPA 時相當有用。
與 Advacned Custom Fields、WPML、Polylang 相容。
新增的端點如下:
取得所有選單
GET /menus/v1/menus

// 回應範例
{
term_id: 2,
name: "主選單",
slug: "main-menu",
term_group: 0,
term_taxonomy_id: 2,
taxonomy: "nav_menu",
description: "",
parent: 0,
count: 8,
filter: "raw"
},
...

藉由編號 (term_id) 取得選單項目
GET /menus/v1/menus/<id>

// 回應範例
{
ID: 5,
post_author: "1",
post_date: "2018-07-03 06:42:18",
post_date_gmt: "2018-07-03 06:42:18",
filter: "raw",
db_id:5,
menu_item_parent: "0",
object_id: "5",
object: "custom",
type: "custom",
type_label: "Custom Link",
title: "首頁",
url: "https:\/\/wp-rest-menu.local\/",
target: "",
attr_title: "",
description: "",
classes: [
""
],
xfn: "",
meta: null
},
...

取得所有選單位置
顯示在 /wp-admin/nav-menus.php?action=locations 分派的所有選單位置
GET /menus/v1/menus/locations 已被廢棄,請使用以下端點:
GET /menus/v1/locations

// 回應範例
{
slug: "top",
description: "頂部選單"
},
{
slug: "social",
description: "社交欄目"
}
...

取得所有選單位置項目
在 /wp-admin/nav-menus.php?action=locations 中分派的所有選單位置
GET /menus/v1/menus/locations/<slug> 已被廢棄,請使用以下端點:
GET /menus/v1/locations/<slug>

// 回應範例
{
ID: 5,
post_author: "1",
post_date: "2018-07-03 06:42:18",
post_date_gmt: "2018-07-03 06:42:18",
filter: "raw",
db_id:5,
menu_item_parent: "0",
object_id: "5",
object: "custom",
type: "custom",
type_label: "Custom Link",
title: "首頁",
url: "https:\/\/wp-rest-menu.local\/",
target: "",
attr_title: "",
description: "",
classes: [
""
],
xfn: "",
meta: null
},
...

兩種篩選器皆可用:
依欄位篩選器
// 回傳只有指定欄位
GET /menus/v1/menus/<id>/?fields=ID,title,meta

// 回應範例
// Response sample
{
ID: 5,
title: "首頁",
meta: null
},
...

巢狀項目篩選器
// 回傳選單項目之父項目和巢狀兒子在 'children' 欄位中
// 目前只支援一層
GET /menus/v1/menus/<id>/?nested=1

// 回應範例
{
ID: 1716,
menu_item_parent: "0",
object_id: "174",
object: "page",
title: "第 1 層",
meta: {
vue_component: "LevelComponent",
menu-item-field-01: "Field 1 value",
menu-item-field-02: "Field 2 value"
},
children:[
{
ID: 1717,
menu_item_parent: "1716",
object_id: "744",
object: "page",
title: "第 2 層 b",
meta : {
vue_components: "Level 1 Component"
}
}
]
},
...

外掛標籤

開發者團隊

⬇ 下載最新版 (v1.0.4) 或搜尋安裝

① 下載 ZIP → 後台「外掛 › 安裝外掛 › 上傳外掛」
② 後台搜尋「WP REST Menus」→ 直接安裝(推薦)
📦 歷史版本下載

原文外掛簡介

This plugin adds new endpoints for WordPress registered menus.
Usefull when building SPAs with Vuejs, React or any front-end framework.
Works with Advacned Custom Fields, WPML, Polylang
The new endpoints available:
Get all menus
GET /menus/v1/menus

// Response sample
{
term_id: 2,
name: "Main Menu",
slug: "main-menu",
term_group: 0,
term_taxonomy_id: 2,
taxonomy: "nav_menu",
description: "",
parent: 0,
count: 8,
filter: "raw"
},
...

Get a menus items by id (term_id)
GET /menus/v1/menus/

// Response sample
{
ID: 5,
post_author: "1",
post_date: "2018-07-03 06:42:18",
post_date_gmt: "2018-07-03 06:42:18",
filter: "raw",
db_id:5,
menu_item_parent: "0",
object_id: "5",
object: "custom",
type: "custom",
type_label: "Custom Link",
title: "Home",
url: "https:\/\/wp-rest-menu.local\/",
target: "",
attr_title: "",
description: "",
classes: [
""
],
xfn: "",
meta: null
},
...

Get all menu locations
All menu locations assigned in /wp-admin/nav-menus.php?action=locations
GET /menus/v1/menus/locations is deprecated and will be removed in newer versions use:
GET /menus/v1/locations

// Response example
{
slug: "top",
description: "Top Menu"
},
{
slug: "social",
description: "Social Links Menu"
}
...

Get all menu location items
All menu locations assigned in /wp-admin/nav-menus.php?action=locations
GET /menus/v1/menus/locations/ is deprecated and will be removed in newer versions use:
GET /menus/v1/locations/

// Response samexampleple
{
ID: 5,
post_author: "1",
post_date: "2018-07-03 06:42:18",
post_date_gmt: "2018-07-03 06:42:18",
filter: "raw",
db_id:5,
menu_item_parent: "0",
object_id: "5",
object: "custom",
type: "custom",
type_label: "Custom Link",
title: "Home",
url: "https:\/\/wp-rest-menu.local\/",
target: "",
attr_title: "",
description: "",
classes: [
""
],
xfn: "",
meta: null
},
...

There are two filters availiable:
Fields Filter
// it will return only the fields specified
GET /menus/v1/menus//?fields=ID,title,meta

// Response sample
// Response sample
{
ID: 5,
title: "Home",
meta: null
},
...

Nested Items Filter
// it will return menu items parents and nested children in a 'children' field
// Currently only one level deep is supported
GET /menus/v1/menus//?nested=1

// Response sample
{
ID: 1716,
menu_item_parent: "0",
object_id: "174",
object: "page",
title: "Level 1",
meta: {
vue_component: "LevelComponent",
menu-item-field-01: "Field 1 value",
menu-item-field-02: "Field 2 value"
},
children:[
{
ID: 1717,
menu_item_parent: "1716",
object_id: "744",
object: "page",
title: "Level 2b",
meta : {
vue_component: null
}
},
...
]
},
...

WP filter hooks
This plugin is quite configurable and provides lots of wp filter hooks from returned data in responses for each endpoint to params validation and endpoint permissions.
add_filter( 'wprm/get_menus/wp_get_nav_menus/args', 'my_wp_get_nav_menus', 10, 1 );
(used in GET /menus/v1/menus)

function my_wp_get_nav_menus( $args ) {
// do something with wp_get_nav_menus $args array
return $args;
}

add_filter( 'wprm/get_menus', 'my_get_menus', 10, 1 );
(used in GET /menus/v1/menus)

function my_get_menus( $menus ) {
// do something with $menus array

return $menus; // WP_Error|WP_HTTP_Response|WP_REST_Response|mixed
}

add_filter( 'wprm/get_menu_locations', 'my_get_menu_locations', 10, 1 );
(used in GET /menus/v1/locations)

function my_get_menu_locations( $locations ) {
// You can modify the $locations array response (get_registered_nav_menus())

return $locations; // WP_Error|WP_HTTP_Response|WP_REST_Response|mixed
}

add_filter( 'wprm/get_menu_items', 'my_get_menu_items', 10, 1 );
(used in GET /menus/v1/menus/)

function my_rest_menu_item_fields( $menu ) {
// You can modify the $menu items

return $menu;
}

add_filter( 'wprm/get_location_menu_items', 'my_get_location_menu_items', 10, 1 );
(used in GET /menus/v1/menus/)

function my_get_location_menu_items( $menu ) {
// You can modify the locations $menu items

return $menu;
}

add_filter( 'wprm/get_item_fields/filter_fields', 'my_filter_fields', 10, 1 );
(used to filter return field -node edges-)

function my_filter_fields( $fields ) {
// You can modify the $fields array so
// you can filter the return fields for all endpoints
// without using the url param ?fields

$fields = array( 'ID', 'title' );
return $fields;
}

More filters
apply_filters('wprm/get_menus/permissions', '__return_true', $request );
apply_filters('wprm/get_menu_locations/permissions', '__return_true', $request );
apply_filters('wprm/get_menu_items/permissions', '__return_true', $request );
apply_filters('wprm/get_menu_items/validate/args/id', is_numeric( $param ), $param, $request, $key );
apply_filters('wprm/get_menu_items/validate/args/fields', is_string( $param ), $param, $request, $key );
apply_filters('wprm/get_menu_items/validate/args/nested', absint( $param ), $param, $request, $key );
apply_filters('wprm/get_location_menu_items/permissions', '__return_true', $request );
apply_filters('wprm/get_location_menu_items/validate/args/slug', is_string( $param ), $param, $request, $key );
apply_filters('wprm/get_location_menu_items/validate/args/fields', is_string( $param ), $param, $request, $key );
apply_filters('wprm/get_location_menu_items/validate/args/nested', absint( $param ), $param, $request, $key );
apply_filters('wprm/get_location_menu_items/permissions', '__return_true', $request );
apply_filters('wprm/get_location_menu_items/validate/args/slug', is_string( $param ), $param, $request, $key );
apply_filters('wprm/get_location_menu_items/validate/args/fields', is_string( $param ), $param, $request, $key );
apply_filters('wprm/get_location_menu_items/validate/args/nested', absint( $param ), $param, $request, $key );

Supports custom fields and Advanced Custom Fields
If ACF is installed the response node edge is acf else meta
In newer version these two edges will co exist and the plugin will separate natively registered custom fields ad acf registered ones.

文章
Filter
Mastodon