【发布时间】:2018-02-23 14:37:34
【问题描述】:
我正在开发一个前端应用程序,该应用程序与位于单独域中的 wordpress API 相关联。
我想检索一个帖子草稿,因此当用户在 wordpress 管理面板中单击“预览帖子”时,它会打开我的应用程序并显示正确的内容。
我正在将一些 Javascript 加载到 WP Admin 中,因此我可以使用 wp_nonce 令牌修改管理面板中的所有“预览帖子”链接以进行身份验证。这是使用下面的 sn-p 完成的,它将预览帖子链接调整为例如:http://example.com/blog?p=127&preview=true&auth=16045802ee
function admin_inline_js(){
// Grab URL
echo '
<script>
document.addEventListener("DOMContentLoaded", function() {
// Grab all preview anchors
var anchors = document.querySelectorAll("a[href*=\'preview=true\']"), i;
// Loop through and amend to remove the trailing slash, as WP doesnt provide any easy method to achieve this.
for(i = 0; i < anchors.length; i++) {
anchors[i].href = anchors[i].href.replace("blog/", "blog") + \'&auth=' . wp_create_nonce('wp_rest') .'\';
}
});
</script>
';
}
add_action( 'admin_print_scripts', 'admin_inline_js' );
在http://example.com/blog?p=127&preview=true&auth=16045802ee,然后使用auth 参数将请求发回wordpress,以检索ID 为127 的草稿,随机数标记为16045802ee。但是这不起作用,我收到了以下回复:
object(stdClass)#459 (3) { ["code"]=> string(25) "rest_cookie_invalid_nonce" ["message"]=> string(23) "Cookie nonce is invalid" ["data"]=> object(stdClass)#532 (1) { ["status"]=> int(403) } }
谁能发现我在这里做错了什么? :/
谢谢。
【问题讨论】: