使用 Wordpress REST API 创建带有自定义 url / 元标记的页面
Creating page with custom url / meta tags using Wordpress REST API
我正在尝试在我的域上创建一个包含 url 'http://example.com/test/slug' 的自定义页面,但是 slug 似乎不支持斜杠(slug '/test/slug' 变成 '/testslug')。是否可以使用其余 api 在自定义 url 上创建页面?
此外,我填充了 'meta' 字典,但是创建的页面在页眉中不包含元 'description' 标记。如何正确创建自定义元标记?
import requests
url = 'http://example.com/wp-json/wp/v2/pages'
data = {'content': '<h2>test content</h2>',
'meta': {'description': 'this is a test meta field'},
'slug': 'test/slug',
'status': 'publish',
'title': 'test title'}
resp = requests.post(url, json=data, auth=('user','pass'), headers={'Content-Type':'application/json'})
要获得您想要的 URL 结构,最直接的方法是创建一个子页面,该子页面引用一个已存在的页面作为其父页面。
修改上面的示例:
import requests
url = 'http://example.com/wp-json/wp/v2/pages'
parent_page = 43 # assuming that page with URL '/test' has id of 42
data = {'content': '<h2>test content</h2>',
'meta': {'description': 'this is a test meta field'},
'slug': 'slug',
'status': 'publish',
'title': 'test title'
'parent': parent_page} # <--
resp = requests.post(url, json=data, auth=('user','pass'), headers={'Content-Type':'application/json'})
会在 example.com/test/slug
给你一个新页面
参考 WordPress rest "Create a Page"、(POST /wp/v2/pages
) 的参数 API 手册:https://developer.wordpress.org/rest-api/reference/pages/#create-a-page
the slug does not seem to support slashes (the slug '/test/slug' turns
into '/testslug').
是的,因为无论您是使用 REST API 还是管理员 UI 创建页面,别名都限制为 字母数字 字符,外加破折号(-
) 和下划线 (_
)。参见 https://developer.wordpress.org/rest-api/reference/pages/#schema-slug。
您可以 通过将 remove_filter( 'sanitize_title', 'sanitize_title_with_dashes', 10, 3 );
添加到主题的 functions.php
文件(或自定义插件)来移除限制;但是,访问页面 (URL) 默认会抛出 404(未找到)错误。有很多方法可以解决这个问题,但简而言之,您不应该删除 filter.
Is it possible using the rest api to create a page on a custom url?
不,不是,默认情况下不是。
不过,如果你想http://example.com/test/slug
到serve/show一个Page/Post/etc。无论是否通过 REST API 创建,您都可以使用自定义 URL 重写规则,例如通过 add_rewrite_rule()
.
the page created does not contain a meta 'description' tag in the
header. How can I correctly create custom meta tags?
您需要注册元密钥,在您的情况下是description
。
见
您可以使用 wp-includes/meta.php.
中的 register_meta()
函数注册它
您的 description
元示例:
<?php
// The object type. For custom post types, this is 'post';
// for custom comment types, this is 'comment'. For user meta,
// this is 'user'.
$object_type = 'post'; // 'post' even for Pages
$args1 = array( // Validate and sanitize the meta value.
// Note: currently (4.7) one of 'string', 'boolean', 'integer',
// 'number' must be used as 'type'. The default is 'string'.
'type' => 'string',
// Shown in the schema for the meta key.
'description' => 'A meta key associated with a string meta value.',
// Return a single value of the type.
'single' => true,
// Show in the WP REST API response. Default: false.
'show_in_rest' => true,
);
register_meta( $object_type, 'description', $args1 );
要快速测试元 description
是否已成功注册并且可以从 REST API 获得,请向 http://example.com/wp-json/wp/v2/pages/<id>
执行 GET
请求,其中 <id>
是页面 ID。
首先,确保您的永久链接设置为 "Post Name"。
这可以在http://<yoursite.com>/wp-admin/options-permalink.php
下配置
我研究了 "Custom Permalinks" 插件代码以了解如何回答这个问题,它保存了一个名为 "custom_permalink" 的元数据并进行了大量的 INNER JOINS 以使 WordPress 核心与 "fake" 子一起工作-鼻涕虫。
我想出了一个不同的解决方案。这里的技巧是创建一个假的父页面作为子页面的基础URL。我写在PHP,因为我不知道Python。
<?php
require_once('helpers.php');
define('API_URL', 'http://temp.localhost');
# Let's get all pages in an array
$pages = curlGet(API_URL.'/wp-json/wp/v2/pages');
# Your usual $args
$args = array(
'title' => 'API TEST',
'status' => 'draft',
'content' => 'content',
'slug' => 'some/thing'
);
# We intercept it here, before sending to WordPress
$args = maybe_add_parent($args);
$response = curlPost( API_URL.'/wp-json/wp/v2/pages/', $args );
/**
* Receives an $args array that would be sent to WordPress API and checks if we need to add a parent page
*/
function maybe_add_parent(array $args) {
if (array_key_exists('slug', $args)) {
# Has parent?
if (strpos($args['slug'], '/') !== false) {
$parent = explode('/', $args['slug']);
# For simplicity sake let's do it parent/chidren slugs only
if (count($parent) !== 2) {
die('This script can only run parent/children slugs');
}
$slug = array_pop($parent);
# Here, we will check the parent to see if it exists.
$parent_id = slug_exists($parent[0]);
if ($parent_id === false) {
# If it does not, it will create it and return it's ID
$parent_id = create_parent($parent[0]);
}
# Add parent ID to $args.
$args['parent'] = $parent_id;
# Rename the slug
$args['slug'] = $slug;
}
}
return $args;
}
/**
* Checks if a given slug exists in $pages
*/
function slug_exists(string $slug) {
global $pages;
foreach ($pages as $page) {
# Checks if a "Parent" page with this slug exists
if ($page['slug'] == $slug && $page['parent'] == 0) {
return true;
}
}
return false;
}
/**
* Creates a parent page
*/
function create_parent(string $slug) {
$args = array(
'title' => $slug,
'status' => 'draft',
'content' => '',
'slug' => $slug
);
$response = json_decode(curlPost( API_URL.'/wp-json/wp/v2/pages/', $args ));
return $response->id;
}
该脚本执行以下操作:
- 为网站上的所有页面发送 GET 请求。
- 拦截将发送到 WordPress 的 $args 数组 API。
- 检查此 $args 是否包含
parent/child
格式的 slug
- 如果存在,请检查
parent
页面是否存在
- 如果没有,它将创建此页面作为草稿并且return它的 ID
- 如果父级已经存在,它将只是 return 它的 ID
- 使用父 ID,它将修改 $args 添加
parent
键和 return $args
- 当 WordPress 发现该页面有父页面时,它会自动将其添加到 url 中,如
parent/child
有效:
我正在尝试在我的域上创建一个包含 url 'http://example.com/test/slug' 的自定义页面,但是 slug 似乎不支持斜杠(slug '/test/slug' 变成 '/testslug')。是否可以使用其余 api 在自定义 url 上创建页面? 此外,我填充了 'meta' 字典,但是创建的页面在页眉中不包含元 'description' 标记。如何正确创建自定义元标记?
import requests
url = 'http://example.com/wp-json/wp/v2/pages'
data = {'content': '<h2>test content</h2>',
'meta': {'description': 'this is a test meta field'},
'slug': 'test/slug',
'status': 'publish',
'title': 'test title'}
resp = requests.post(url, json=data, auth=('user','pass'), headers={'Content-Type':'application/json'})
要获得您想要的 URL 结构,最直接的方法是创建一个子页面,该子页面引用一个已存在的页面作为其父页面。
修改上面的示例:
import requests
url = 'http://example.com/wp-json/wp/v2/pages'
parent_page = 43 # assuming that page with URL '/test' has id of 42
data = {'content': '<h2>test content</h2>',
'meta': {'description': 'this is a test meta field'},
'slug': 'slug',
'status': 'publish',
'title': 'test title'
'parent': parent_page} # <--
resp = requests.post(url, json=data, auth=('user','pass'), headers={'Content-Type':'application/json'})
会在 example.com/test/slug
参考 WordPress rest "Create a Page"、(POST /wp/v2/pages
) 的参数 API 手册:https://developer.wordpress.org/rest-api/reference/pages/#create-a-page
the slug does not seem to support slashes (the slug '/test/slug' turns into '/testslug').
是的,因为无论您是使用 REST API 还是管理员 UI 创建页面,别名都限制为 字母数字 字符,外加破折号(-
) 和下划线 (_
)。参见 https://developer.wordpress.org/rest-api/reference/pages/#schema-slug。
您可以 通过将 remove_filter( 'sanitize_title', 'sanitize_title_with_dashes', 10, 3 );
添加到主题的 functions.php
文件(或自定义插件)来移除限制;但是,访问页面 (URL) 默认会抛出 404(未找到)错误。有很多方法可以解决这个问题,但简而言之,您不应该删除 filter.
Is it possible using the rest api to create a page on a custom url?
不,不是,默认情况下不是。
不过,如果你想http://example.com/test/slug
到serve/show一个Page/Post/etc。无论是否通过 REST API 创建,您都可以使用自定义 URL 重写规则,例如通过 add_rewrite_rule()
.
the page created does not contain a meta 'description' tag in the header. How can I correctly create custom meta tags?
您需要注册元密钥,在您的情况下是description
。
见
您可以使用 wp-includes/meta.php.
中的register_meta()
函数注册它
您的 description
元示例:
<?php
// The object type. For custom post types, this is 'post';
// for custom comment types, this is 'comment'. For user meta,
// this is 'user'.
$object_type = 'post'; // 'post' even for Pages
$args1 = array( // Validate and sanitize the meta value.
// Note: currently (4.7) one of 'string', 'boolean', 'integer',
// 'number' must be used as 'type'. The default is 'string'.
'type' => 'string',
// Shown in the schema for the meta key.
'description' => 'A meta key associated with a string meta value.',
// Return a single value of the type.
'single' => true,
// Show in the WP REST API response. Default: false.
'show_in_rest' => true,
);
register_meta( $object_type, 'description', $args1 );
要快速测试元 description
是否已成功注册并且可以从 REST API 获得,请向 http://example.com/wp-json/wp/v2/pages/<id>
执行 GET
请求,其中 <id>
是页面 ID。
首先,确保您的永久链接设置为 "Post Name"。
这可以在http://<yoursite.com>/wp-admin/options-permalink.php
我研究了 "Custom Permalinks" 插件代码以了解如何回答这个问题,它保存了一个名为 "custom_permalink" 的元数据并进行了大量的 INNER JOINS 以使 WordPress 核心与 "fake" 子一起工作-鼻涕虫。
我想出了一个不同的解决方案。这里的技巧是创建一个假的父页面作为子页面的基础URL。我写在PHP,因为我不知道Python。
<?php
require_once('helpers.php');
define('API_URL', 'http://temp.localhost');
# Let's get all pages in an array
$pages = curlGet(API_URL.'/wp-json/wp/v2/pages');
# Your usual $args
$args = array(
'title' => 'API TEST',
'status' => 'draft',
'content' => 'content',
'slug' => 'some/thing'
);
# We intercept it here, before sending to WordPress
$args = maybe_add_parent($args);
$response = curlPost( API_URL.'/wp-json/wp/v2/pages/', $args );
/**
* Receives an $args array that would be sent to WordPress API and checks if we need to add a parent page
*/
function maybe_add_parent(array $args) {
if (array_key_exists('slug', $args)) {
# Has parent?
if (strpos($args['slug'], '/') !== false) {
$parent = explode('/', $args['slug']);
# For simplicity sake let's do it parent/chidren slugs only
if (count($parent) !== 2) {
die('This script can only run parent/children slugs');
}
$slug = array_pop($parent);
# Here, we will check the parent to see if it exists.
$parent_id = slug_exists($parent[0]);
if ($parent_id === false) {
# If it does not, it will create it and return it's ID
$parent_id = create_parent($parent[0]);
}
# Add parent ID to $args.
$args['parent'] = $parent_id;
# Rename the slug
$args['slug'] = $slug;
}
}
return $args;
}
/**
* Checks if a given slug exists in $pages
*/
function slug_exists(string $slug) {
global $pages;
foreach ($pages as $page) {
# Checks if a "Parent" page with this slug exists
if ($page['slug'] == $slug && $page['parent'] == 0) {
return true;
}
}
return false;
}
/**
* Creates a parent page
*/
function create_parent(string $slug) {
$args = array(
'title' => $slug,
'status' => 'draft',
'content' => '',
'slug' => $slug
);
$response = json_decode(curlPost( API_URL.'/wp-json/wp/v2/pages/', $args ));
return $response->id;
}
该脚本执行以下操作:
- 为网站上的所有页面发送 GET 请求。
- 拦截将发送到 WordPress 的 $args 数组 API。
- 检查此 $args 是否包含
parent/child
格式的 slug
- 如果存在,请检查
parent
页面是否存在 - 如果没有,它将创建此页面作为草稿并且return它的 ID
- 如果父级已经存在,它将只是 return 它的 ID
- 使用父 ID,它将修改 $args 添加
parent
键和 return $args - 当 WordPress 发现该页面有父页面时,它会自动将其添加到 url 中,如
parent/child
有效: