
Client library facilitates interaction with the WordPress REST API, featuring JSON parsing, coroutine support, and cross-platform compatibility, leveraging Ktor client and kotlinx libraries.
WPKotlin is a Kotlin client library for the WordPress REST API, for Kotlin/JVM projects and Android apps. For iOS, macOS and other Apple platforms use WPSwift.
// build.gradle.kts
dependencies {
implementation("io.github.rocxteady:wpkotlin:2.0.0")
}val wp = WPClient(route = "https://example.com/wp-json") // namespace defaults to "wp/v2"
val info = wp.site.get()
val posts = wp.posts.list(page = 1, perPage = 10, categories = listOf(3))
val post = wp.posts.get(1234)
val results = wp.posts.search("kotlin")
val categories = wp.categories.list()
println("${post.decodedTitle} ${post.embedded?.featuredMedia?.thumbnailURL}")
wp.close() // when you no longer need the clientWrites need credentials, for example a WordPress application password:
val admin = WPClient(
route = "https://example.com/wp-json",
credentials = Credentials.ApplicationPassword("admin", "xxxx xxxx xxxx xxxx"),
)
val draft = admin.posts.create(PostInput(title = "Hello", content = "<p>Hi</p>", status = "draft"))
admin.posts.update(draft.id, PostInput(status = "publish")) // only set fields change
admin.categories.create(PostCategoryInput(name = "News"))Errors are WPError (InvalidRoute, InvalidNamespace, Http, Decoding, Network):
try {
wp.posts.get(1)
} catch (e: WPError.Http) {
println("${e.statusCode} ${e.code} ${e.serverMessage}") // 404 rest_post_invalid_id Invalid post ID.
}All service functions are suspend.
| 0.0.x | 2.0 |
|---|---|
| Kotlin Multiplatform (iOS, macOS, JS, Wasm, Linux, Windows) and the SPM XCFramework | JVM and Android only; use WPSwift on Apple platforms |
wpkotlin-android artifact |
wpkotlin (same jar for JVM and Android) |
WPKotlin.initialize(route, namespace) |
val wp = WPClient(route, namespace) and keep it |
PostsRepository().getPosts(...) |
wp.posts.list(...) (categoriesToExclude → excludedCategories, tagsToExclude → excludedTags) |
GetPostRepository().getPost(id), SearchPostsRepository().searchPosts(term)
|
wp.posts.get(id), wp.posts.search(term)
|
CategoriesRepository().getCategories(), GetCategoryRepository().getCategory(id)
|
wp.categories.list() (default perPage 100), wp.categories.get(id)
|
APIInfoRepository().getAPIInfo(route) |
wp.site.get() (other route: another WPClient) |
getPost(baseURL, id) |
one WPClient per site |
| read-only |
posts.create/update, categories.create/update with Credentials
|
APIInfo, SimplePost (link), OrderType
|
SiteInfo, PostSearchResult (url), SortOrder
|
LocalDateTime dates |
kotlin.time.Instant (GMT) |
htmlDecodedTitle, embeddedContent
|
decodedTitle, embedded
|
CommentStatus.CLOSE, isOpen
|
CommentStatus.CLOSED (close still decodes), compare with OPEN
|
io.github.rocxteady.wpkotlin.models.*, .helpers.OrderType
|
everything is in io.github.rocxteady.wpkotlin
|
WPError.UrlMalformed / Api / Unknown
|
WPError.InvalidRoute / InvalidNamespace / Http / Decoding / Network
|
Apache License 2.0. See LICENSE.
Ulaş Sancak (@rocxteady)
WPKotlin is a Kotlin client library for the WordPress REST API, for Kotlin/JVM projects and Android apps. For iOS, macOS and other Apple platforms use WPSwift.
// build.gradle.kts
dependencies {
implementation("io.github.rocxteady:wpkotlin:2.0.0")
}val wp = WPClient(route = "https://example.com/wp-json") // namespace defaults to "wp/v2"
val info = wp.site.get()
val posts = wp.posts.list(page = 1, perPage = 10, categories = listOf(3))
val post = wp.posts.get(1234)
val results = wp.posts.search("kotlin")
val categories = wp.categories.list()
println("${post.decodedTitle} ${post.embedded?.featuredMedia?.thumbnailURL}")
wp.close() // when you no longer need the clientWrites need credentials, for example a WordPress application password:
val admin = WPClient(
route = "https://example.com/wp-json",
credentials = Credentials.ApplicationPassword("admin", "xxxx xxxx xxxx xxxx"),
)
val draft = admin.posts.create(PostInput(title = "Hello", content = "<p>Hi</p>", status = "draft"))
admin.posts.update(draft.id, PostInput(status = "publish")) // only set fields change
admin.categories.create(PostCategoryInput(name = "News"))Errors are WPError (InvalidRoute, InvalidNamespace, Http, Decoding, Network):
try {
wp.posts.get(1)
} catch (e: WPError.Http) {
println("${e.statusCode} ${e.code} ${e.serverMessage}") // 404 rest_post_invalid_id Invalid post ID.
}All service functions are suspend.
| 0.0.x | 2.0 |
|---|---|
| Kotlin Multiplatform (iOS, macOS, JS, Wasm, Linux, Windows) and the SPM XCFramework | JVM and Android only; use WPSwift on Apple platforms |
wpkotlin-android artifact |
wpkotlin (same jar for JVM and Android) |
WPKotlin.initialize(route, namespace) |
val wp = WPClient(route, namespace) and keep it |
PostsRepository().getPosts(...) |
wp.posts.list(...) (categoriesToExclude → excludedCategories, tagsToExclude → excludedTags) |
GetPostRepository().getPost(id), SearchPostsRepository().searchPosts(term)
|
wp.posts.get(id), wp.posts.search(term)
|
CategoriesRepository().getCategories(), GetCategoryRepository().getCategory(id)
|
wp.categories.list() (default perPage 100), wp.categories.get(id)
|
APIInfoRepository().getAPIInfo(route) |
wp.site.get() (other route: another WPClient) |
getPost(baseURL, id) |
one WPClient per site |
| read-only |
posts.create/update, categories.create/update with Credentials
|
APIInfo, SimplePost (link), OrderType
|
SiteInfo, PostSearchResult (url), SortOrder
|
LocalDateTime dates |
kotlin.time.Instant (GMT) |
htmlDecodedTitle, embeddedContent
|
decodedTitle, embedded
|
CommentStatus.CLOSE, isOpen
|
CommentStatus.CLOSED (close still decodes), compare with OPEN
|
io.github.rocxteady.wpkotlin.models.*, .helpers.OrderType
|
everything is in io.github.rocxteady.wpkotlin
|
WPError.UrlMalformed / Api / Unknown
|
WPError.InvalidRoute / InvalidNamespace / Http / Decoding / Network
|
Apache License 2.0. See LICENSE.
Ulaş Sancak (@rocxteady)