如何从 Wordpress 上排队的任何 javascript 文件访问 WP REST-API? [英] How to access the WP REST-API from any javascript file enqueued on Wordpress?

查看:29
本文介绍了如何从 Wordpress 上排队的任何 javascript 文件访问 WP REST-API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何直接访问 WP REST-API 以从 Wordpress 中排队的任何 javascript 文件读取和写入任何公共/私有数据?

解决方案

NODE-WPAPI 客户端身份验证和全局句柄为这个问题提供了一个简单而轻松的答案.

查看:https://somefunagba.github.io/node-wpapi-auth/

  • 转到插件仪表板.激活NODE-WPAPI JS-CLIENT 安装和身份验证"
  • 插件现已激活,其下方是链接:设置 |停用 |编辑
  • 点击此设置链接
  • 输入任何管理员用户名和密码,以验证基本身份验证.
  • 转到您的实时制作或开发站点.
  • 检查您的浏览器控制台:如果您的管理员名为 Matt,那么您应该会看到控制台输出:

    马特!我准备访问 WP REST-API!"

一切就绪!

<块引用>

打开任何 .js 脚本,将其放入 Wordpress 中,然后开始使用 WPAPI 的 wp 对象直接访问 WORDPRESS REST-API.

 例如:获取您的 wordpress 模型站点上的所有帖子.

<块引用>

示例:在主题目录中的get_wp_post.js"脚本中.直接插入这个代码片段即可.

 wp.posts().embed().get().then(function( response ) {控制台日志(响应);//对返回的数据做一些事情});

<块引用>

查看 http://wp-api.org/node-wpapi/ 更多信息.使用客户端与 WP REST-API 交互.

注意:

<块引用>

正在寻找您的浏览器控制台?它应该是这样的:

  • Mozilla 浏览器控制台:工具 > Web 开发人员 > Web 控制台
  • Chrome 浏览器控制台:更多工具 > 开发者工具 > 控制台

How to access the WP REST-API directly to read and write any public/private data from any javascript file enqueued in Wordpress?

解决方案

The NODE-WPAPI Client Authentication and Global Handle provides an easy hassle-free answer to this problem.

Check out: https://somefunagba.github.io/node-wpapi-auth/

  • Go to Plugins Dashboard. Activate ‘NODE-WPAPI JS-CLIENT Install and Authentication’
  • The Plugin is now activated, under it are the links: Settings | Deactivate | Edit
  • Click on this Settings link
  • Enter any Administrator username and password, to validate Basic Authentication.
  • Go to your live production or development site.
  • Check your browser console: If your administrator’s name is Matt, then you should see a console output:

    "Matt! I am set to access the WP REST-API!"
    

You are all set!

Open any .js script, enqueue it in Wordpress and start accessing the WORDPRESS REST-API using the WPAPI's, wp object directly.

    e.g : To get all the posts on your wordpress model site.

Example: In something like a 'get_wp_post.js' script in your theme directory. Just insert this code snippets directly.

    wp.posts().embed().get().then(function( response ) {
        console.log( response );
        //do something with returned data
        }); 

See http://wp-api.org/node-wpapi/ more info. on using the client to interact with WP REST-API.

N.B:

Looking for your Browser Console? It should be something like:

  • Mozilla Browser Console: Tools > Web Developer > Web Console
  • Chrome Browser Console: More Tools > Dveloper Tools > Console

这篇关于如何从 Wordpress 上排队的任何 javascript 文件访问 WP REST-API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆