Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save benlk/c1de8fdfc5c38b4a5a1a2e49c844b0a2 to your computer and use it in GitHub Desktop.

Select an option

Save benlk/c1de8fdfc5c38b4a5a1a2e49c844b0a2 to your computer and use it in GitHub Desktop.

Revisions

  1. benlk revised this gist May 2, 2025. 1 changed file with 14 additions and 17 deletions.
    31 changes: 14 additions & 17 deletions wp-multipart-image-upload.php
    Original file line number Diff line number Diff line change
    @@ -1,44 +1,41 @@
    <?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.
    * Creates a multipart/form-data body for an HTML file and some form fields.
    *
    * @link https://gist.github.com/petenelson/828a1fdb390973aedebe0396abc31234
    * @param string $file_contents The file contents
    * @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 {
    function create_multipart_form_data( string $file_contents, array $fields = [] ): array {

    $boundary = wp_generate_password( 24, false, false );

    $body = '';
    $body = '--' . $boundary . "\r\n";

    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 .= '--' . $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 .= 'Content-Disposition: form-data; name="file"; filename="' . 'file.html' . '"' . "\r\n";

    // this is hardcoded because we know we're doing HTML here.
    $body .= 'Content-Type: text/html; charset=utf-8' . "\r\n\r\n";

    $body .= $file_contents . "\r\n";
    $body .= '--' . $boundary . '--';

    return [
  2. @petenelson petenelson revised this gist Dec 18, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion wp-multipart-image-upload.php
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    * 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 filenamem, if different from the file path.
    * @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.
    */
  3. @petenelson petenelson revised this gist Dec 18, 2024. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions wp-multipart-image-upload.php
    Original file line number Diff line number Diff line change
    @@ -20,10 +20,12 @@ function create_multipart_form_data( string $file_path, string $filename = '', a
    // 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";
    }
  4. @petenelson petenelson revised this gist Dec 17, 2024. 1 changed file with 20 additions and 0 deletions.
    20 changes: 20 additions & 0 deletions wp-multipart-image-upload.php
    Original file line number Diff line number Diff line change
    @@ -46,9 +46,14 @@ function create_multipart_form_data( string $file_path, string $filename = '', a
    }

    // 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 );
    @@ -63,3 +68,18 @@ function create_multipart_form_data( string $file_path, string $filename = '', a
    ];

    $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',
    // ],
    // ],
    // ]
    // );
  5. @petenelson petenelson revised this gist Dec 17, 2024. 1 changed file with 11 additions and 2 deletions.
    13 changes: 11 additions & 2 deletions wp-multipart-image-upload.php
    Original file line number Diff line number Diff line change
    @@ -16,8 +16,17 @@ function create_multipart_form_data( string $file_path, string $filename = '', a

    foreach ( $fields as $name => $value ) {
    $body .= '--' . $boundary . "\r\n";
    $body .= 'Content-Disposition: form-data; name="' . $name . '"' . "\r\n\r\n";
    $body .= $value . "\r\n";

    // Such as the "meta" field for post meta.
    if ( is_array( $value ) ) {
    foreach ( $value as $sub_name => $sub_value ) {
    $body .= 'Content-Disposition: form-data; name="' . $name . '[' . $sub_name . ']"' . "\r\n\r\n";
    $body .= $sub_value . "\r\n";
    }
    } else {
    $body .= 'Content-Disposition: form-data; name="' . $name . '"' . "\r\n\r\n";
    $body .= $value . "\r\n";
    }
    }

    if ( empty( $filename ) ) {
  6. @petenelson petenelson created this gist Dec 17, 2024.
    56 changes: 56 additions & 0 deletions wp-multipart-image-upload.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,56 @@
    <?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 filenamem, 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";
    $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.
    $form_fields = [
    'post_title' => 'Image title',
    'post_name' => 'custom-slug',
    ];

    $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 );