Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
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 Pascal (Lazarus/Delphi) Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.Http,
Chilkat.HttpResponse,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
http: THttp;
json: TJsonObject;
resp: THttpResponse;
jResp: TJsonObject;
begin
success := False;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http := THttp.Create;
// Use your WordPress login, such as "admin", not the application name.
http.Login := 'wp_username';
// Use the application password, such as "Nths RwVH eDJ4 weNZ orMN jabq"
// See WordPress Application Passwords Plugin
http.Password := 'app_password';
http.BasicAuth := True;
// Create the tag "ChatGPT" if it does not already exist.
json := TJsonObject.Create;
json.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.
resp := THttpResponse.Create;
success := http.HttpJson('POST','https://cknotes.com/wp-json/wp/v2/tags',json,'application/json',resp);
if (success = False) then
begin
WriteLn(http.LastErrorText);
Exit;
end;
jResp := TJsonObject.Create;
jResp.Load(resp.BodyStr);
// Check if the tag already exists..
if (resp.StatusCode = 400) then
begin
if (jResp.HasMember('code') = True) then
begin
if (jResp.StringOfEquals('code','term_exists',True) = True) then
begin
// The tag already exists.
WriteLn('The tag already exists.');
WriteLn('Tag ID: ' + jResp.IntOf('data.term_id'));
Exit;
end;
end;
// Fall through to check for errors.
end;
// Check for errors.
if (resp.StatusCode <> 201) then
begin
WriteLn(resp.BodyStr);
WriteLn('status code = ' + resp.StatusCode);
Exit;
end;
// We get here if the tag was created..
WriteLn('The tag was created.');
WriteLn('Tag ID = ' + jResp.IntOf('id'));
http.Free;
json.Free;
resp.Free;
jResp.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.