Title: Headless Converter
Author: Atte Liimatainen
Published: <strong>2021 年 10 月 8 日</strong>
Last modified: 2022 年 1 月 28 日

---

搜索插件

**该插件尚未通过WordPress的最新3个主要版本进行测试**。 当与较新版本的WordPress一起
使用时，可能不再受到维护或支持，并且可能会存在兼容性问题。

![](https://s.w.org/plugins/geopattern-icon/headless-converter.svg)

# Headless Converter

 作者：[Atte Liimatainen](https://profiles.wordpress.org/attlii/)

[下载](https://downloads.wordpress.org/plugin/headless-converter.1.0.6.zip)

 * [详情](https://cn.wordpress.org/plugins/headless-converter/#description)
 * [评价](https://cn.wordpress.org/plugins/headless-converter/#reviews)
 * [开发进展](https://cn.wordpress.org/plugins/headless-converter/#developers)

 [支持](https://wordpress.org/support/plugin/headless-converter/)

## 描述

Converts frontend to JSON response when request is done with certain conditions.

### Inspiration

After creating a bunch of headless WordPress + Web app stacks, I wanted to find 
a standard and a all-round solution for fetching per page information for web applications
inside WordPress. Built-in rest api works fine in basic cases, but it doesn’t support
querying by path. Usually this meant that for each project developers would create
a custom rest endpoint which would return expected content using content type and
slug parameters.

WordPress has few built-in functions to retrieve content by path, [url_to_postid](https://developer.wordpress.org/reference/functions/url_to_postid/)
and [get_page_by_path](https://developer.wordpress.org/reference/functions/get_page_by_path/),
but they don’t seem to work with multilanguage plugins, taxonomy or archive pages,
which means that WP doesn’t have a reliable way to fetch content this way through
rest api.

This plugin converts frontend to JSON which seems after above solution the best 
way to do things, with added layer of security through application passwords (WordPress
v5.6 feature) and a filter, which let’s developers alter outgoing data.

### How to start using the plugin

 * Install and activate this plugin in your environment
 * Create application password for a user with administrator role

After above steps have been made, make a request to a page with added [Authorization header](https://en.wikipedia.org/wiki/Basic_access_authentication#Client_side).
See data fetching examples at the end of this documentation for more help

### Modifying the output

Plugin outputs current page’s Post object or null. This can be modified using `headless-
converter-modify-data`-filter. You can either modify passed in post object or do
your own logic like in the example below.

    ```
    /**
     * Modifies Headless Converter plugin's output.
     * 
     * @param WPPost|null $post - Current template's post object 
     */
    function modify_headless_converter_output($post) {
      if(is_404()) {
        return "this block renders 404 page content";
      } else if(is_page()) {
        return "this block renders page post types content";
      } else if (is_singular('post')) {
        return "this block renders single post content";
      } else if(is_home()) {
        return "this block renders post archive";
      } else {
        return $post;
      }
    }

    add_filter('headless-converter-modify-data', 'modify_headless_converter_output');
    ```

### Data fetching examples

#### Fetch

    ```
    const username = "admin"
    const password = "1111 1111 1111 1111 1111"
    const url = "http://localhost:3000/?page_id=2"
    const opts = {
      headers: {
         'Authorization': 'Basic ' + btoa(username + ":" + password) 
      }, 
    }
    fetch(url, opts).then(r => r.json()).then(console.log)
    ```

#### Axios

    ```
    const axios = require("axios")

    const username = "admin"
    const password = "1111 1111 1111 1111 1111"
    const url = "http://localhost:3000/?page_id=2"
    const opts = {
      auth: {
        username,
        password
      }
    }

    axios(url, opts).then(r => r.data).then(console.log)
    ```

## 常见问题

### Application Password doesn’t show up, even though I use WordPress version 5.6 or newer. What do?

This seems to be a feature that is opt-in on some environments. Following hook enables
it.

    ```
    add_filter( 'wp_is_application_passwords_available' , '__return_true' );
    ```

## 评价

此插件暂无评价。

## 贡献者及开发者

「Headless Converter」是开源软件。 以下人员对此插件做出了贡献。

贡献者

 *   [ Atte Liimatainen ](https://profiles.wordpress.org/attlii/)

[帮助将「Headless Converter」翻译成简体中文。](https://translate.wordpress.org/projects/wp-plugins/headless-converter)

### 对开发感兴趣吗?

您可以[浏览代码](https://plugins.trac.wordpress.org/browser/headless-converter/)，
查看[SVN仓库](https://plugins.svn.wordpress.org/headless-converter/)，或通过[RSS](https://plugins.trac.wordpress.org/log/headless-converter/?limit=100&mode=stop_on_copy&format=rss)
订阅[开发日志](https://plugins.trac.wordpress.org/log/headless-converter/)。

## 更新日志

#### 1.0.0 (2020-10-08):

 * Initial release

#### 1.0.1 (2020-10-08):

 * Remove unnecessary stuff from the plugin folder

#### 1.0.2 (2020-10-20):

 * Add FAQ about enabling application passwords

#### 1.0.3 (2020-11-15):

 * Update and prune repository dependencies

#### 1.0.4 (2020-11-15):

 * Run repository through a linter

#### 1.0.5 (2020-11-16):

 * Fix documentation

#### 1.0.6 (2021-01-28):

 * update tested up to version to 5.9

## 额外信息

 *  版本 **1.0.6**
 *  最后更新：**4 年前**
 *  活跃安装数量 **不到10**
 *  WordPress 版本 ** 5.6 或更高版本 **
 *  已测试的最高版本为 **5.9.13**
 *  PHP 版本 ** 7.4 或更高版本 **
 *  语言
 * [English (US)](https://wordpress.org/plugins/headless-converter/)
 * 标签
 * [converter](https://cn.wordpress.org/plugins/tags/converter/)[headless](https://cn.wordpress.org/plugins/tags/headless/)
   [JSON](https://cn.wordpress.org/plugins/tags/json/)
 *  [高级视图](https://cn.wordpress.org/plugins/headless-converter/advanced/)

## 评级

尚未提交反馈。

[Your review](https://wordpress.org/support/plugin/headless-converter/reviews/#new-post)

[查看全部评论](https://wordpress.org/support/plugin/headless-converter/reviews/)

## 贡献者

 *   [ Atte Liimatainen ](https://profiles.wordpress.org/attlii/)

## 支持

有话要说吗？是否需要帮助？

 [查看支持论坛](https://wordpress.org/support/plugin/headless-converter/)