搜尋
首頁後端開發Python教學使用 WordPress API 的綜合指南:身份驗證和後期調度

A Comprehensive Guide to Using the WordPress API: Authentication and Post Scheduling

在本指南中,我們將探索如何使用 WordPress API 進行驗證並安排特定發佈時間的貼文。這些步驟將幫助您以程式設計方式安全地管理您的 WordPress 內容。

使用 WordPress API 進行身份驗證

要安全地與 WordPress API 交互,您需要對您的要求進行身份驗證。讓我們深入研究兩種常見的方法:

應用程式密碼

應用程式密碼是 WordPress 中的內建功能,可讓您產生用於 API 存取的安全密碼,而不會洩露您的主帳戶密碼。

  1. 登入您的 WordPress 管理儀表板。
  2. 導覽至使用者→個人資料
  3. 向下捲動到「應用程式密碼」部分。
  4. 輸入應用程式的名稱(例如「API 存取」)。
  5. 點選「新增應用程式的密碼」。
  6. 複製產生的密碼(您將無法再次看到它)。

使用應用程式密碼:


<p>import requests</p>

<p>url = "https://your-wordpress-site.com/wp-json/wp/v2/posts"<br>
username = "your_username"<br>
app_password = "your_application_password"</p>

<p>headers = {<br>
    "Content-Type": "application/json"<br>
}</p>

<p>response = requests.get(url, auth=(username, app_password), headers=headers)</p>




基本驗證外掛程式

對於較舊的 WordPress 版本或如果您喜歡替代方法:

  1. 從 WordPress.org GitHub 儲存庫下載基本驗證外掛程式。
  2. 在您的 WordPress 網站上安裝並啟用該外掛程式。
  3. 使用您的常規 WordPress 使用者名稱和密碼進行身份驗證。

<p>import requests</p>

<p>url = "https://your-wordpress-site.com/wp-json/wp/v2/posts"<br>
username = "your_username"<br>
password = "your_password"</p>

<p>headers = {<br>
    "Content-Type": "application/json"<br>
}</p>

<p>response = requests.get(url, auth=(username, password), headers=headers)</p>




在特定時間發布貼文

要安排貼文在特定時間發布,請在建立或更新貼文時使用日期參數。方法如下:

建立預定帖子


<p>import requests<br>
from datetime import datetime, timedelta</p>

<p>url = "https://your-wordpress-site.com/wp-json/wp/v2/posts"<br>
username = "your_username"<br>
app_password = "your_application_password"</p>

<p># Schedule the post for 2 days from now at 10:00 AM<br>
scheduled_time = datetime.now() + timedelta(days=2)<br>
scheduled_time = scheduled_time.replace(hour=10, minute=0, second=0, microsecond=0)<br>
scheduled_time_str = scheduled_time.isoformat()</p>

<p>data = {<br>
    "title": "Scheduled Post Example",<br>
    "content": "This is the content of the scheduled post.",<br>
    "status": "future",<br>
    "date": scheduled_time_str<br>
}</p>

<p>response = requests.post(url, auth=(username, app_password), json=data)</p>

<p>if response.status_code == 201:<br>
    print("Post scheduled successfully!")<br>
else:<br>
    print("Error scheduling post:", response.text)</p>




更新現有貼文的時間表

要重新安排現有帖子,您需要其帖子 ID:


<p>import requests<br>
from datetime import datetime, timedelta</p>

<p>post_id = 123  # Replace with the actual post ID<br>
url = f"https://your-wordpress-site.com/wp-json/wp/v2/posts/{post_id}"<br>
username = "your_username"<br>
app_password = "your_application_password"</p>

<p># Reschedule the post for 1 week from now at 2:00 PM<br>
new_scheduled_time = datetime.now() + timedelta(weeks=1)<br>
new_scheduled_time = new_scheduled_time.replace(hour=14, minute=0, second=0, microsecond=0)<br>
new_scheduled_time_str = new_scheduled_time.isoformat()</p>

<p>data = {<br>
    "status": "future",<br>
    "date": new_scheduled_time_str<br>
}</p>

<p>response = requests.post(url, auth=(username, app_password), json=data)</p>

<p>if response.status_code == 200:<br>
    print("Post rescheduled successfully!")<br>
else:<br>
    print("Error rescheduling post:", response.text)</p>




重要提示

  • 確保您的 WordPress 網站使用 HTTPS 進行安全通訊。
  • 妥善保管您的應用程式密碼或常規密碼,切勿分享。
  • 日期參數應採用 ISO 8601 格式 (YYYY-MM-DDTHH:MM:SS)。
  • WordPress API 使用 UTC 時間,因此請相應地調整您的計劃時間。
  • 將預定貼文的貼文狀態設定為「未來」。
  • 您也可以使用 date_gmt 參數直接指定 GMT/UTC 時間。

透過遵循本指南,您應該能夠使用 WordPress API 進行身份驗證,並以程式設計方式安排特定發佈時間的貼文。

引用:

  1. 驗證 – REST API 手冊 | Developer.WordPress.org
  2. WordPress REST API:如何存取、使用和保護它(完整教學)
  3. WordPress REST API 驗證 – WordPress 外掛 | WordPress.org
  4. WordPress API 基礎知識初學者指南 - GetDevDone 部落格
  5. 什麼是 WP REST API 以及如何保護它 | WordPress Rest API
  6. WordPress REST API 驗證 | WordPress 外掛程式

以上是使用 WordPress API 的綜合指南:身份驗證和後期調度的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
Python的混合方法:編譯和解釋合併Python的混合方法:編譯和解釋合併May 08, 2025 am 12:16 AM

pythonuseshybridapprace,ComminingCompilationTobyTecoDeAndInterpretation.1)codeiscompiledtoplatform-Indepententbybytecode.2)bytecodeisisterpretedbybythepbybythepythonvirtualmachine,增強效率和通用性。

了解python的' for”和' then”循環之間的差異了解python的' for”和' then”循環之間的差異May 08, 2025 am 12:11 AM

theKeyDifferencesBetnewpython's“ for”和“ for”和“ loopsare:1)” for“ loopsareIdealForiteringSequenceSquencesSorkNowniterations,而2)”,而“ loopsareBetterforConterContinuingUntilacTientInditionIntionismetismetistismetistwithOutpredefinedInedIterations.un

Python串聯列表與重複Python串聯列表與重複May 08, 2025 am 12:09 AM

在Python中,可以通過多種方法連接列表並管理重複元素:1)使用 運算符或extend()方法可以保留所有重複元素;2)轉換為集合再轉回列表可以去除所有重複元素,但會丟失原有順序;3)使用循環或列表推導式結合集合可以去除重複元素並保持原有順序。

Python列表串聯性能:速度比較Python列表串聯性能:速度比較May 08, 2025 am 12:09 AM

fasteStmethodMethodMethodConcatenationInpythondependersonListsize:1)forsmalllists,operatorseffited.2)forlargerlists,list.extend.extend()orlistComprechensionfaster,withextendEffaster,withExtendEffers,withextend()withextend()是extextend()asmoremory-ememory-emmoremory-emmoremory-emmodifyinginglistsin-place-place-place。

您如何將元素插入python列表中?您如何將元素插入python列表中?May 08, 2025 am 12:07 AM

toInSerteLementIntoApythonList,useAppend()toaddtotheend,insert()foreSpificPosition,andextend()formultiplelements.1)useappend()foraddingsingleitemstotheend.2)useAddingsingLeitemStotheend.2)useeapecificindex,toadapecificindex,toadaSpecificIndex,toadaSpecificIndex,blyit'ssssssslorist.3 toaddextext.3

Python是否列表動態陣列或引擎蓋下的鏈接列表?Python是否列表動態陣列或引擎蓋下的鏈接列表?May 07, 2025 am 12:16 AM

pythonlistsareimplementedasdynamicarrays,notlinkedlists.1)他們areStoredIncoNtiguulMemoryBlocks,mayrequireRealLealLocationWhenAppendingItems,EmpactingPerformance.2)LinkesedlistSwoldOfferefeRefeRefeRefeRefficeInsertions/DeletionsButslowerIndexeDexedAccess,Lestpypytypypytypypytypy

如何從python列表中刪除元素?如何從python列表中刪除元素?May 07, 2025 am 12:15 AM

pythonoffersFourmainMethodStoreMoveElement Fromalist:1)刪除(值)emovesthefirstoccurrenceofavalue,2)pop(index)emovesanderturnsanelementataSpecifiedIndex,3)delstatementremoveselemsbybybyselementbybyindexorslicebybyindexorslice,and 4)

試圖運行腳本時,應該檢查是否會遇到'權限拒絕”錯誤?試圖運行腳本時,應該檢查是否會遇到'權限拒絕”錯誤?May 07, 2025 am 12:12 AM

toresolvea“ dermissionded”錯誤Whenrunningascript,跟隨台詞:1)CheckAndAdjustTheScript'Spermissions ofchmod xmyscript.shtomakeitexecutable.2)nesureThEseRethEserethescriptistriptocriptibationalocatiforecationAdirectorywherewhereyOuhaveWritePerMissionsyOuhaveWritePermissionsyYouHaveWritePermissions,susteSyAsyOURHomeRecretectory。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。