Last active
February 26, 2025 16:44
-
-
Save petenelson/828a1fdb390973aedebe0396abc31234 to your computer and use it in GitHub Desktop.
WordPress: Upload image to REST API via multipart/form-data body
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Creates a multipart/form-data body for an image and form fields. | |
* | |
* @param string $file_path The path to the file. | |
* @param string $filename The base filename, if different from the file path. | |
* @param array $fields The form fields. | |
* @return array The boundary ID and the body. | |
*/ | |
function create_multipart_form_data( string $file_path, string $filename = '', array $fields = [] ): array { | |
$boundary = wp_generate_password( 24, false, false ); | |
$body = ''; | |
foreach ( $fields as $name => $value ) { | |
$body .= '--' . $boundary . "\r\n"; | |
// Such as the "meta" field for post meta. | |
if ( is_array( $value ) ) { | |
foreach ( $value as $sub_name => $sub_value ) { | |
$body .= '--' . $boundary . "\r\n"; | |
$body .= 'Content-Disposition: form-data; name="' . $name . '[' . $sub_name . ']"' . "\r\n\r\n"; | |
$body .= $sub_value . "\r\n"; | |
} | |
} else { | |
$body .= '--' . $boundary . "\r\n"; | |
$body .= 'Content-Disposition: form-data; name="' . $name . '"' . "\r\n\r\n"; | |
$body .= $value . "\r\n"; | |
} | |
} | |
if ( empty( $filename ) ) { | |
$filename = basename( $file_path ); | |
} | |
$body .= '--' . $boundary . "\r\n"; | |
$body .= 'Content-Disposition: form-data; name="file"; filename="' . $filename . '"' . "\r\n"; | |
$body .= 'Content-Type: ' . mime_content_type( $file_path ) . "\r\n\r\n"; | |
$body .= file_get_contents( $file_path ) . "\r\n"; | |
$body .= '--' . $boundary . '--'; | |
return [ | |
'boundary' => $boundary, | |
'body' => $body, | |
]; | |
} | |
// Example code. | |
// Post meta can be sent in the "meta" field, but it needs to be registered via | |
// register_post_meta(), see example. | |
$form_fields = [ | |
'post_title' => 'Image title', | |
'post_name' => 'custom-slug', | |
'meta' => [ | |
'test_hello' => 'world', | |
], | |
]; | |
$multipart = create_multipart_form_data( $image_filename, $filename, $form_fields ); | |
$headers['Content-Type'] = 'multipart/form-data; boundary=' . $multipart['boundary']; | |
$args = [ | |
'headers' => $headers, | |
'body' => $multipart['body'], | |
'sslverify' => false, | |
'timeout' => 30, | |
]; | |
$response = wp_remote_post( $image_upload_endpoint, $args ); | |
// Example of register_post_meta(). | |
// register_post_meta( | |
// 'attachment', | |
// 'test_hello', [ | |
// 'type' => 'string', | |
// 'description' => 'Test meta field.', | |
// 'single' => true, | |
// 'show_in_rest' => [ | |
// 'schema' => [ | |
// 'type' => 'string', | |
// ], | |
// ], | |
// ] | |
// ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment