Skip to main content

A simple wordpress api

Project description

WordPress REST API Client

A Python client for interacting with the WordPress REST API to manage content, media, and categories.

Features

  • Create posts/pages
  • Upload images from URLs
  • Create categories
  • Secure authentication handling
  • Error handling for API requests

Installation

Requirements:

  • Python 3.6+
  • WordPress site with REST API enabled (v4.7+)
  • Application password (WordPress > Users > Profile > Application Passwords)

Install dependencies:

   pip install requests WordPRest

Initialize client

from wpAPI import WordPress
wp = WordPress(
    domain='yourdomain.com',
    username='admin',
    password='your_application_password'
)

Create a category

new_category = wp.category("Tech News")
print(f"Created category ID: {new_category['id']}")

Upload an image

   image_url = "https://example.com/image.jpg"
   media = wp.upload_image(image_url)
   print(f"Uploaded media ID: {media['id']}")

Create a post

post_data = {
    "title": "My First API Post",
    "content": "This post was created programmatically!",
    "status": "publish",
    "categories": [new_category['id']],
    "featured_media": media['id']
}
post = wp.publish(post_data)
print(f"Published post at: {post['link']}")

API Methods

category(data) Purpose: Create a new category

Parameters: content_data (str): Category name

Returns: Category object or None on failure

upload_image(image_url)

Purpose: Upload image from URL to media library

Parameters:

image_url (str): Publicly accessible image URL

Returns: Media object or None on failure

publish(content_data, content_type="post")

Purpose: Create posts/pages

Parameters:

content_data (dict): Post/page content (see WP REST API docs)

content_type (str): "post" (default) or "page"

Returns: Post/Page object or None on failure

Content Data Structure

Typical post structure:

{
    "title": "Post Title",
    "content": "Post content",
    "status": "publish",  # or "draft"
    "categories": [1, 2], # Array of category IDs
    "featured_media": 123, # Media ID from upload_image()
    "tags": [3, 4],       # Array of tag IDs
    "excerpt": "Summary text"
}

Error Handling

Methods return None on failure

Errors are printed to console with details

Common error sources:

  • Invalid credentials

  • Network issues

  • Invalid image URLs

  • Insufficient user permissions

Important Security Practices:

  • Always use application passwords (not regular user passwords)

  • Store credentials in environment variables (not in code)

  • Use HTTPS for your WordPress site

  • Restrict API access through permissions/plugins

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wordprest-0.0.3.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wordprest-0.0.3-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file wordprest-0.0.3.tar.gz.

File metadata

  • Download URL: wordprest-0.0.3.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for wordprest-0.0.3.tar.gz
Algorithm Hash digest
SHA256 ba4cf4931cf3118717211530269738b848c7546833073a741542b47bc9d951a0
MD5 df7627584d9a50e2c1ddc077bb8ee470
BLAKE2b-256 f3a2a1a932bac5ccb9b1df457ec7b29f3c76deb0001348c0cf20a2f6ae61b3d5

See more details on using hashes here.

File details

Details for the file wordprest-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: wordprest-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for wordprest-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 5a0cffc633e9d1383ad3678912389af19c02332480f9427682039dfa7a56aa09
MD5 69973600220c90299636875c1fe11ebd
BLAKE2b-256 42e736a618135d724afe37f001a70fc2d68bfb54e58d1f307bdb3360b116b6bf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page