Unicode C
Unicode C
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 Unicode C Downloads
#include <C_CkHttpW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpResponseW.h>
void ChilkatSample(void)
{
BOOL success;
HCkHttpW http;
HCkJsonObjectW json;
HCkHttpResponseW resp;
HCkJsonObjectW jResp;
success = FALSE;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http = CkHttpW_Create();
// Use your WordPress login, such as "admin", not the application name.
CkHttpW_putLogin(http,L"wp_username");
// Use the application password, such as "Nths RwVH eDJ4 weNZ orMN jabq"
// See WordPress Application Passwords Plugin
CkHttpW_putPassword(http,L"app_password");
CkHttpW_putBasicAuth(http,TRUE);
// Create the tag "ChatGPT" if it does not already exist.
json = CkJsonObjectW_Create();
CkJsonObjectW_UpdateString(json,L"name",L"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.
resp = CkHttpResponseW_Create();
success = CkHttpW_HttpJson(http,L"POST",L"https://cknotes.com/wp-json/wp/v2/tags",json,L"application/json",resp);
if (success == FALSE) {
wprintf(L"%s\n",CkHttpW_lastErrorText(http));
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(json);
CkHttpResponseW_Dispose(resp);
return;
}
jResp = CkJsonObjectW_Create();
CkJsonObjectW_Load(jResp,CkHttpResponseW_bodyStr(resp));
// Check if the tag already exists..
if (CkHttpResponseW_getStatusCode(resp) == 400) {
if (CkJsonObjectW_HasMember(jResp,L"code") == TRUE) {
if (CkJsonObjectW_StringOfEquals(jResp,L"code",L"term_exists",TRUE) == TRUE) {
// The tag already exists.
wprintf(L"The tag already exists.\n");
wprintf(L"Tag ID: %d\n",CkJsonObjectW_IntOf(jResp,L"data.term_id"));
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(json);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(jResp);
return;
}
}
// Fall through to check for errors.
}
// Check for errors.
if (CkHttpResponseW_getStatusCode(resp) != 201) {
wprintf(L"%s\n",CkHttpResponseW_bodyStr(resp));
wprintf(L"status code = %d\n",CkHttpResponseW_getStatusCode(resp));
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(json);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(jResp);
return;
}
// We get here if the tag was created..
wprintf(L"The tag was created.\n");
wprintf(L"Tag ID = %d\n",CkJsonObjectW_IntOf(jResp,L"id"));
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(json);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(jResp);
}