| 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: - You must have a WordPress.com account.
- You need to get a WordPress.com API key or OAuth2 token for authentication.
- The Jetpack plugin must be installed and connected on a self-hosted WordPress site if you're posting to a self-hosted site.
Steps: - Get API Token:
- Go to WordPress.com Developer page and create an application to get the OAuth2 credentials.
- 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"
}'
|