PHP

WordPress Remote Posting with PHP

CK CK 3788 
Created at
Updated at  
1,704 0 0

To remotely write or post content to a WordPress.com site using PHP, you can leverage the WordPress REST API. Below is a PHP example that shows how to remotely write or create a post on a WordPress.com site.

Prerequisites:

  1. You must have a WordPress.com account.
  2. You need to get a WordPress.com API key or OAuth2 token for authentication.
  3. The Jetpack plugin must be installed and connected on a self-hosted WordPress site if you're posting to a self-hosted site.

Steps:

  1. Get API Token:
    • Go to WordPress.com Developer page and create an application to get the OAuth2 credentials.
  2. Use PHP cURL to Make API Requests:
    The example below shows how to create a new post.

PHP Example:

<?php

// Your WordPress.com OAuth2 token (get this from WordPress Developer console)
$access_token = 'your_access_token';

// The WordPress.com blog/site ID (or you can use the domain)
$site_id = 'your_site_id';

// The REST API endpoint for creating a post
$api_url = "https://public-api.wordpress.com/wp/v2/sites/$site_id/posts";

// Post data
$post_data = array(
    'title'   => 'Remote Post Title',
    'content' => '<p>This is the content of the post created via PHP remotely.</p>',
    'status'  => 'publish' // 'draft' for draft post or 'publish' to publish immediately
);

// Setup cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Authorization: Bearer ' . $access_token,
    'Content-Type: application/json'
));

// Execute the request
$response = curl_exec($ch);

// Check for cURL errors
if(curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
} else {
    // Decode the response
    $response_data = json_decode($response, true);

    // Output the post ID and URL of the newly created post
    echo "Post ID: " . $response_data['id'] . "\n";
    echo "Post URL: " . $response_data['link'] . "\n";
}

// Close cURL session
curl_close($ch);

?>

Explanation:

  • $access_token: The OAuth2 token you get after registering your application on WordPress.com.
  • $site_id: This is either the WordPress.com site ID or the domain name (e.g., example.wordpress.com).
  • $api_url: The endpoint for posting new content. For a self-hosted site, the URL would be different.
  • curl_setopt: This configures the cURL request. The Authorization header includes the Bearer token for authentication.

Notes:

  • Replace the your_access_token and your_site_id with the actual values from your WordPress.com account.
  • You can also modify the status parameter to draft if you don’t want the post to be published immediately.

This will remotely create a new post on your WordPress.com site using PHP.

 

Below is another example based in Linux CLI:

curl -X POST https://public-api.wordpress.com/wp/v2/sites/<your_site_id>/posts \
-H "Authorization: Bearer <your_access_token>" \
-H "Content-Type: application/json" \
-d '{
  "title": "Post Title from CLI",
  "content": "<p>This is the content of the post created using Linux curl command.</p>",
  "status": "publish"
}'

 

Tags PHP WordPress API Remote WordPress Posting WordPress API Integration WordPress Automation WordPress REST API WordPress Remote Posting WordPress.com API Facebook X
Comments 0
  1. When you Postgresql Module is not able to open connection : Doubt your libpq version
    480
  2. How to Get a YouTube Channel ID from a URL in PHP
    1,030
  3. Building a MCP server in PHP
    639
  4. PHP 7 vs PHP 8
    1,254
  5. Upgrading PHP 7.4 to PHP 8.1 in CentOS 7
    653
  6. gRPC Server with PHP
    509
  7. PHP Code to Fetch current Coinbase Assets (Balances)
    444
  8. PHP Code to Fetch PEPE coin pricing based in USD from Coinbase
    367
  9. Google Single Sign-On implementation with PHP
    1,382
  10. PHP Superglobal Variables do not require additional syntax to access it
    2,658
  11. Run Python Code with Virtual Environment in PHP
    676
  12. PHP Simplified Operators
    318
  13. PHP Heredoc (aka Nowdoc)
    449
  14. PHP Post Processing by disconnecting HTTP connection and continuing the remaining logics
    1,825
  15. Google BigQuery Example to converted Nested FIelds in JSON
    461
  16. Verifying Apple In-App Purchase validation receipt with PHP
    1,806
  17. Useful code for file-based Unit Test in PHP
    508
  18. Function Overloading Implementation in PHP with Magic Mathods
    1,766
  19. PHP Magic - the Null Coalescing Operator (??) - return the value if the give variable is NULL
    1,683
  20. How do I find php.ini on Mac?
    496
  21. Mastering Excel Data Importation in PHP
    558
  22. Create Blob Image in HTML based on the given Text, Width and Height in the Center of the Image without saving file
    986
  23. How do I determine the client IP type (IPv4/IPv6) in PHP
    840
  24. What is 302 Found Redirection in HTTP 1.1?
    497
  25. Implementing a Versatile DNS Server in PHP: Handling A, AAAA, CNAME, and TXT Records
    624
  26. Building a Basic DNS Server in PHP/Python: A Beginner's Guide
    747
  27. CentOS 7 PHP Apache Kafka: Installation, Examples, and System Diagram
    311
  28. Apache Kafka Explained: Powering Netflix, Building with PHP
    495
  29. Array of arguments passed to script in PHP
    98
Recently updated
  1. Google OAuth Token Expiration: Causes and Solutions
    31
  2. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    57
  3. Why SPF and DKIM Are Essential for Linux Email Servers?
    51
  4. How do I create animated background for Google Meet?
    23,194
  5. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    273
  6. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    104
  7. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    170
  8. Invalid Auction Storms in Ad Tech
    107
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    208
  10. The Two Clocks of Ad Server Architecture
    78
  11. Frequency Cap Architecture Overview
    527
  12. Collecting Proof-of-Play from 100,000 Screens
    153
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    303
  14. What is RedisVL?
    116
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    247
  16. TPU & GPU Cluster Management Tools
    258
  17. The Concept of Scaffolding in AI Agents
    245
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    598
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    268
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    354
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    207
  22. Machine Learning: Training vs. Ranking
    229
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    228
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    203
  25. Designing a Data Curation System Around a Vector Database
    230
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    263
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    674
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    201
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    233
  30. Understanding Data Lineage
    272