Custom attachment_upload field in custom form not working

Hi,
I succesfully created a modal / form and a link button in booking view page, and the form class is correctly shown when clicking the button.
The form class code is:

<?php
namespace HivePress\Forms;

use HivePress\Helpers as hp;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;

class Send_Receipt extends Form {
	public function __construct( $args = [] ) {
        $args = hp\merge_arrays(
            [
                // Override the listing search form parameters.
                'parameters' => [

					'parent' => 213
				],
				// Set the field parameters.
				'fields'  => [
					'attachment' => [
						'label'    => 'Custom field',
						'type'     => 'attachment_upload',
						'required' => true,
						'_order'   => 123,
					],
				],
			],
			$args
		);

        error_log( print_r(( $args ), true) );
		parent::__construct( $args );
	}
}

and in the website when clicking th button looks like this:

THE PROBLEM:
When clicking “Select File” and I upload a image, the buttons makes the loading animation (loading circle rotating) but then they stop loading and i dont get any uplaoded file preview above the “Select File” Button (as should be, as it is in send message form).
So the process looks like this:

In the browser console I can see that the http request to upload the image fails:

I found out that, comparing it to the send message form, 2 attributes are missing: ‘parent’, ‘parent_modal’

How can i make this “attachment_upload” field work properly?
Thank you a lot in advance, hope is all clear.
Emilio Micali

Sorry for the delay. Unfortunately, there’s no simple solution because the attachment field in its current implementation requires some model (e.g. post) to be attached to, so the form should be based on some model. You can check how the attachment upload is implemented here hivepress/includes/controllers/class-attachment.php at master · hivepress/hivepress · GitHub

HI,
Thank you Ihor for your reply.

If someone else is struggling with that, a workaround I did can be:

  1. create a custom form in a custom plugin
  2. in the form add a field with ‘file’ type
  3. in the php file where is the form redirecting add at the beginning something like that to upload the attachment in the wordpress media library and get the url:
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Your other form fields processing

    // Check if the file is uploaded successfully
    if (isset($_FILES["attachment"])) {
        $file_error = $_FILES["attachment"]["error"];

        if ($file_error == UPLOAD_ERR_OK) {
            $file_tmp = $_FILES["attachment"]["tmp_name"];
            $file_name = $_FILES["attachment"]["name"];

            // Prepare the upload
            $upload = wp_upload_bits($file_name, null, file_get_contents($file_tmp));

            if (!$upload["error"]) {
                // File uploaded successfully to the media library
                $media_url = $upload["url"];

                $GLOBALS['receipt_image_url'] = $media_url;
                // Now you can use $media_url as needed
   
            } else {
                // Handle the upload error
                error_log( "File upload to media library failed.");
            }
        } else {
            // Handle the file upload error
            error_log( "File upload failed with error code: $file_error");
        }
    } else {
        // No file attachment received
        error_log( "No file uploaded");
    }

    if(isset($_POST['receipt_price_total'])){
        $GLOBALS['receipt_price_total'] = $_POST['receipt_price_total'];
    }
}

I hope it can be helpful.
Thanks.

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.