1
0

More than 1 year has passed since last update.

WordPress REST API を使用して WordPressに記事を投稿する(JavaScript)

Posted at

headers

  • アプリケーションパスワードはwordpress管理画面のuserページより取得可能
  const headers = {
      "Content-Type": "application/json",
      Authorization:
        "Basic " + Buffer.from(`${wordpressのユーザーネーム}:${wordpressのアプリケーションパスワード}`).toString("base64"),
    };

url

const url =  `${wordpressのURL}/wp-json/wp/v2/posts`;

articleData(投稿内容)

  • 公開ステータスは以下
    • publish(公開)
    • future(公開予約)
    • draft(下書き)
    • pending(非公開)
    • private(限定公開)
   const articleData = {
      title: 記事タイトル,
      content: 記事本文,
      status: 公開ステータス,
    };

API実行

 const result = await axios.post(url, articleData, { headers });

レスポンス内から投稿ページリンクを参照

const postedURL = result.data.link

以上

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0