5ucms论坛

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 10618|回复: 1
打印 上一主题 下一主题

[教程] WordPress 自定义 REST 接口

[复制链接]

670

主题

785

帖子

8273

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
8273
跳转到指定楼层
楼主
发表于 2021-5-23 20:51:10 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
新版本的 WordPress 自动加入了 REST 接口,方便我们通过 API 的方式去获取网站的数据,但是如果有一些自定义的数据那就比较麻烦了,不然还需要我们自己再定义一套 REST 规则,然后自定义一些路由方法。

WordPress 自定义 REST 接口

基本方法
所幸的是 WordPress 已经考虑到这一点,提供了方法让我们可以自定义 REST 接口:先创建一个函数挂载到 rest_api_init 钩子上,这个函数执行 register_rest_route 方法添加接口路由 wtheme/v1/topBottomMenu,然后有一个回调函数 wtheme_rest_base_cb 设置要返回的数据。

我们只需要修改 wtheme_rest_base_cb 函数返回的数据,就能实现响应不同的数据。

传递参数
往往我们的接口可能需要通过参数去响应不同的内容,比如一个自定义的列表,需要有显示多少条、分页页码、排序方式等参数,通过自定义接口我们一样可以完成。

  1. add_action( 'rest_api_init', 'wtheme_rest_register_route' );
  2. function wtheme_rest_register_route() {
  3.   register_rest_route( 'wtheme/v1', 'topBottomMenu/(?P[\d]+)', [
  4.     'methods'  => 'GET',
  5.     'callback' => 'wtheme_rest_base_cb'
  6.   ] );
  7. }
  8. function wtheme_rest_base_cb($request) {
  9.   $params = $request->get_params();
  10.   $id = $params['id'];
  11.   $out = array(
  12.     'top-menu'           => get_top_menu(),
  13.     'bottom-menu'        => get_footer_menu(),
  14.     'footer_copyright'   => get_option( 'footer_copyright' ),
  15.     'zh_cn_l10n_icp_num' => get_option( 'zh_cn_l10n_icp_num' )
  16.   );
  17.   return $out;
  18. }
复制代码

再通过 register_rest_route 注册一个新的路由,配置我们的参数,通过正则表达式去匹配,在回调函数里面使用 $request->get_params() 获取参数。

可以通过下面的函数获取到不同部分的参数值:

  1. $request->get_url_params();
  2. $request->get_query_params();
  3. $request->get_body_params();
  4. $request->get_json_params();
  5. $request->get_default_params();
  6. $request->get_file_params(); // 上传的文件
复制代码

检查权限
在使用接口的时候,可以检查用户的权限。注册接口的时候添加一个权限检查的回调(permission_callback),在方法里用 WordPress 的 current_user_can 去检查对应的权限。

  1. register_rest_route( 'wtheme/v1', 'topBottomMenu/(?P<id>[\d]+)', [
  2.   'methods' => 'GET',
  3.   'callback' => 'wtheme_rest_base_cb',
  4.   'permission_callback' => 'wtheme_rest_base_validate_callback'
  5. ]);
复制代码

权限检查回调:

  1. function wtheme_rest_base_validate_callback() {
  2.   return current_user_can( 'edit_others_posts' );
  3. }
复制代码
回复

使用道具 举报

670

主题

785

帖子

8273

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
8273
沙发
 楼主| 发表于 2021-5-24 21:44:20 | 只看该作者
简易版

  1. //增加API
  2. function qss_getchannel_callback() {
  3.   //return array(get_the_category_list(1));
  4.         global $wpdb;
  5.         $querystr = "SELECT wp_terms.term_id,wp_terms.name,wp_terms.slug,wp_term_taxonomy.description,wp_term_taxonomy.count FROM `wp_term_taxonomy` LEFT JOIN wp_terms ON wp_term_taxonomy.term_id=wp_terms.term_id WHERE wp_term_taxonomy.taxonomy='category'";
  6.         $results = $wpdb->get_results($querystr);  
  7.         return $results;
  8. }

  9. function qss_getchannel_register_route() {
  10.   register_rest_route( 'qss/v1', 'getchannel', [
  11.     'methods'   => 'GET',
  12.     'callback'  => 'qss_getchannel_callback'
  13.   ] );
  14. }

  15. add_action( 'rest_api_init', 'qss_getchannel_register_route');
复制代码
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|5ucms.com

GMT+8, 2024-6-16 12:38 , Processed in 0.062500 second(s), 28 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表