Hello. In today’s article, we’ll look at uploading images to the WordPress media manager using the API and its core functions.
Today we’ll add some complexity by using a cron job. Our code will need to execute every 10 seconds.
Let’s get started. First, we’ll create a custom time interval for the cron job using the WordPress “cron_schedules” filter:
add_filter('cron_schedules', function ($schedules)
{
$schedules['10sec'] = [
'interval' => 10,
'display' => __('Every 10 sec'),
];
return $schedules;
});In our case, we’re adding another array to the “$schedules” variable. The key is our custom interval. Inside the array:
“interval” — duration in seconds, “display” — label for the interval.
In the following snippet:
add_action('wp', function(){
if(!wp_next_scheduled('rest_api_media'))
{
wp_schedule_event( time(), '10sec', 'rest_api_media');
}
});We trigger the hook that we’ll define later, set to fire every 10 seconds.
The WordPress function “wp_next_scheduled('rest_api_media')” checks whether the specified hook should run at this moment.
If it should — we schedule it with “wp_schedule_event” and set the timestamp.
Now the main code responsible for uploading an image to our or another WordPress site via API:
add_action('rest_api_media', function ()
{
$username = 'admin';
$password = 'qweqwe';
$headers = [
'Authorization' => 'Basic ' . base64_encode($username . ':' . $password),
];
$url = мой-сайт/wp-json/wp/v2/media/';
$file = __DIR__ . '/data/book.jpg';
$headers['Content-Disposition'] = 'attachment; filename="' . basename($file) . '"';
$response = wp_remote_post($url, [
'headers' => $headers,
'body' => file_get_contents($file)
]);
$records = json_decode(
wp_remote_retrieve_body($response),
true
);
echo '<strong>Code:</strong> ' . wp_remote_retrieve_response_code($response);
echo '<br>';
echo '<strong>Message:</strong> ' . wp_remote_retrieve_response_message($response);
echo '<br>';
echo '<strong>Records:</strong> ' . count($records);
echo '<br>';
echo '<strong>Answer:</strong>';
echo"<pre>\$records = ";print_r($records);echo"</pre>";
// die;
});Uploading images through the API was the hardest part for me when working with REST.
There’s almost no information online, and the little I found didn’t help.
Let’s break down the code above. First, we prepare the request headers for authentication.
This is essential — without it, we won’t be able to upload anything.
Here:
$url = мой-сайт/wp-json/wp/v2/media/'; $file = __DIR__ . '/data/book.jpg'; $headers['Content-Disposition'] = 'attachment; filename="' . basename($file) . '"'; $response = wp_remote_post($url, [ 'headers' => $headers, 'body' => file_get_contents($file) ]);
We specify the site URL where the file will be uploaded.
The “$file” variable contains the full path to the file.
And this is the header that declares the name of the uploaded file:
$headers['Content-Disposition'] = 'attachment; filename="' . basename($file) . '"';
Then, using “wp_remote_post”, we send the file contents through the “body” parameter after reading them:
$response = wp_remote_post($url, [ 'headers' => $headers, 'body' => file_get_contents($file) ]);
The site’s API will return a detailed response about the uploaded file.
We can debug and inspect it using this code:
$records = json_decode( wp_remote_retrieve_body($response), true ); echo '<strong>Code:</strong> ' . wp_remote_retrieve_response_code($response); echo '<br>'; echo '<strong>Message:</strong> ' . wp_remote_retrieve_response_message($response); echo '<br>'; echo '<strong>Records:</strong> ' . count($records); echo '<br>'; echo '<strong>Answer:</strong>'; echo"<pre>\$records = ";print_r($records);echo"</pre>";
That’s it — image upload complete.
