Skip to content

TomOdw/WordpressAPI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WordpressAPI

A modern C++23 client library for the WordPress REST API.

Provides type-safe entity classes and a generic CRUD client so you can create, read, update, and delete WordPress content — posts, pages, categories, and media — directly from C++ code.


Features

  • Full CRUD — create, retrieve, list, update, and destroy any entity type
  • Type-safe entitiesWp::Post, Wp::Page, Wp::Category, Wp::Media
  • Fluent query builder — filter, paginate, and sort collection requests with method chaining
  • Media upload — multipart file upload to the /media endpoint
  • HTTPS — TLS support via OpenSSL
  • Header-only API clientApi.hpp requires no separate compilation
  • No external package manager — dependencies are bundled as single-header files

Quick Start

#include "Api.hpp"
#include "Post.hpp"

int main()
{
    Wp::Api api({
        .url      = "https://your-site.com",
        .user     = "wpUser",
        .password = "application-password"
    });

    // Create a post
    auto post = std::make_unique<Wp::Post>();
    post->setTitle("Hello from C++");
    post->setContent(Wp::Item::TextType::RAW, "This post was created by WordpressAPI.");
    post->setStatus(Wp::Item::Status::PRIVATE);
    api.create(post);

    // Retrieve it back
    auto fetched = api.retrieve<Wp::Post>(post->getId());
    // Update it
    fetched->setTitle("Hello from C++ (edited)");
    api.update(fetched);

    // Delete it permanently
    api.destroy(fetched, /*force=*/true);

    return 0;
}

Listing with a query

Wp::Post::Query q;
q.search("release notes")
 .status({Wp::Item::Status::PUBLISH})
 .perPage(5)
 .page(1);

auto posts = api.list(q);
for (auto const& p : posts)
{
    // p->getId(), p->getTitle(), p->getLink(), …
}

Uploading media

auto media = api.uploadMedia("/path/to/photo.jpg", "image/jpeg");
// media->getSourceUrl(), media->getMimeType(), …

Entity Overview

Class WordPress endpoint Inherits
Wp::Post /wp-json/wp/v2/posts ContentItemItemEntity
Wp::Page /wp-json/wp/v2/pages ContentItemItemEntity
Wp::Category /wp-json/wp/v2/categories Entity
Wp::Media /wp-json/wp/v2/media ItemEntity

Every entity exposes a ::Query builder for use with Wp::Api::list().


Building

Requirements

Tool Minimum version
CMake 3.31
C++ compiler C++23 (GCC 13+ / Clang 17+)
OpenSSL any recent version
cmake -S . -B build
cmake --build build

The static library is written to build/lib/libWordpressAPI.a.

Link in your own project

add_subdirectory(WordpressAPI/api)
target_link_libraries(your_target PRIVATE WordpressAPI)

Running the Tests

The test suite contains live integration tests against a real WordPress instance. They require a valid test/credentials.hpp (not included in the repository).

Copy the placeholder that is checked in and fill it in:

// test/credentials.hpp
#include "Api.hpp"

const Wp::Api::Credentials wpCreds =
{
    .url      = "https://your-site.com",
    .user     = "wpUser",
    .password = "application-password",
};

Tell git to keep your local credentials private:

git update-index --skip-worktree test/credentials.hpp

Build and run:

cmake -S . -B build
cmake --build build
ctest --test-dir build --output-on-failure

Individual test suites can be run by name:

ctest --test-dir build -R ConnectionTest
ctest --test-dir build -R PostTest
ctest --test-dir build -R PageTest
ctest --test-dir build -R CategoryTest
ctest --test-dir build -R MediaTest

Project Structure

api/
├── src/          # Library headers and source files
│   ├── Api.hpp   # Header-only CRUD client (main entry point)
│   ├── Entity.*  # Abstract base class for all entities
│   ├── Item.*    # Timestamped entity base (posts, pages, media)
│   ├── ContentItem.*  # Content-bearing base (posts, pages)
│   ├── Post.*    # WordPress post entity
│   ├── Page.*    # WordPress page entity
│   ├── Category.* # WordPress category entity
│   └── Media.*   # WordPress media attachment entity
└── ext/          # Bundled third-party headers
    ├── httplib/  # cpp-httplib
    └── json/     # nlohmann/json
test/             # Integration test suite (Google Test)
doc/              # UML class diagram and sample API responses

Dependencies

WordpressAPI bundles the following excellent single-header libraries — no package manager required.

Library Version Author License
cpp-httplib 0.29.0 Yuji Hirose MIT
nlohmann/json 3.12.0 Niels Lohmann MIT

The test suite uses Google Test (v1.14.0), fetched automatically by CMake at build time.


License

This project is released under the MIT License.

About

A C++ Wordpress-REST API

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors