Xbase++
Xbase++
WordPress Create Tag
See more WordPress Examples
Demonstrates how to create a new tag in Wordpress, or to find the ID of an existing tag.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oHttp
LOCAL oJson
LOCAL oResp
LOCAL oJResp
nSuccess := 0
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
oHttp := CreateObject("Chilkat.Http")
// Use your WordPress login, such as "admin", not the application name.
oHttp:Login := "wp_username"
// Use the application password, such as "Nths RwVH eDJ4 weNZ orMN jabq"
// See WordPress Application Passwords Plugin
oHttp:Password := "app_password"
oHttp:BasicAuth := 1
// Create the tag "ChatGPT" if it does not already exist.
oJson := CreateObject("Chilkat.JsonObject")
oJson:UpdateString("name", "ChatGPT")
// This will create the tag if it does not yet exist.
// If the tag already exists, then a 400 status code is returned.
// If the tag deoes not yet exist, then a 201 status code is returned.
oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpJson("POST", "https://cknotes.com/wp-json/wp/v2/tags", oJson, "application/json", oResp)
IF (nSuccess == 0)
? oHttp:LastErrorText
oHttp:destroy()
oJson:destroy()
oResp:destroy()
RETURN
ENDIF
oJResp := CreateObject("Chilkat.JsonObject")
oJResp:Load(oResp:BodyStr)
// Check if the tag already exists..
IF (oResp:StatusCode == 400)
IF (oJResp:HasMember("code") == 1)
IF (oJResp:StringOfEquals("code", "term_exists", 1) == 1)
// The tag already exists.
? "The tag already exists."
? "Tag ID: " + Str(oJResp:IntOf("data.term_id"))
oHttp:destroy()
oJson:destroy()
oResp:destroy()
oJResp:destroy()
RETURN
ENDIF
ENDIF
// Fall through to check for errors.
ENDIF
// Check for errors.
IF (oResp:StatusCode != 201)
? oResp:BodyStr
? "status code = " + Str(oResp:StatusCode)
oHttp:destroy()
oJson:destroy()
oResp:destroy()
oJResp:destroy()
RETURN
ENDIF
// We get here if the tag was created..
? "The tag was created."
? "Tag ID = " + Str(oJResp:IntOf("id"))
oHttp:destroy()
oJson:destroy()
oResp:destroy()
oJResp:destroy()