File upload problem

0

I’ve got a developer working on my site and he is trying to add an upload file function to the Question form but it isn’t working. When adding the following code to the function.php he says that only a text field appears instead of the file upload:

function my_custom_question_field( $form ) {
// Upload field.
$form['fields']['screenshot'] = array(
'fields' => array(
'pdf' => array( // This is a unique key for field.
'label' => __( 'PDF File' ),
'desc' => __( 'Allow single PDF file upload.' ),
'type' => 'upload',
'upload_options' => array(
'multiple' => false, // Only allow single image upload.
'allowed_mimes' => array(
'pdf' => 'application/pdf', // This will allow uploading of PDF file.
),
),
),
),
);
return $form;
}
add_filter( 'ap_question_form_fields', 'my_custom_question_field' );


Any ideas what the problem could be? Another issue is that when ‘Allow image upload’ is checked in the settings, the image upload isn’t visible in the form either. Could these issues be related?

Answered question

Hello, I also received an email from you but wasn’t able to respond to it due to lack of time. I will answer you tomorrow with an example.

Thanks, Rahul!

1

function my_custom_question_field( $form ) {
    // Upload field.
    $form['fields']['screenshot'] = array(
        'label' => __( 'Image', 'anspress-question-answer' ),
        'desc' => __( 'Select image(s) to upload. Only .jpg, .png and .gif files allowed.', 'anspress-question-answer' ),
        'type' => 'upload',
        'save' => [ __CLASS__, 'image_upload_save' ],
        'upload_options' => array(
            'multiple' => false,
            'max_files' => 1,
            'allowed_mimes' => array(
                'jpg|jpeg' => 'image/jpeg',
                'gif' => 'image/gif',
                'png' => 'image/png',
            ),
        ),
        'validate' => 'required',
    );
    return $form;
}
add_filter( 'ap_question_form_fields', 'my_custom_question_field' );

Posted new comment

Hi Rahul. Thank you for this code. The upload function is now working but the file just disappears after it is uploaded. I need the file to be added to the posted question so anyone can download it. How is this achieved?

Image file and uploads are uploaded in anspress-temp and once verified its moved to anspress-upload.

How to add a pdf to the Post Answer form?

0

Hi Rahul. Here is the snippet you asked to see:

function my_custom_question_field( $form ) {
// Upload field.
$form['fields']['screenshot'] = array(
'submit_label' => __( 'Upload & insert', 'anspress-question-answer' ),
'fields' => array(
'image' => array(
'label' => __( 'Image', 'anspress-question-answer' ),
'desc' => __( 'Select image(s) to upload. Only .jpg, .png and .gif files allowed.', 'anspress-question-answer' ),
'type' => 'upload',
'save' => [ __CLASS__, 'image_upload_save' ],
'upload_options' => array(
'multiple' => false,
'max_files' => 1,
'allowed_mimes' => array(
'jpg|jpeg' => 'image/jpeg',
'gif' => 'image/gif',
'png' => 'image/png',
),
),
'validate' => 'required',
),
),
);
return $form;
}
add_filter( 'ap_question_form_fields', 'my_custom_question_field' );

Posted new comment

Hi Rahul. Does this snippet help with a solution to why there is no file upload showing on my form?

0

Hi Rahul,
Thanks for the code. My developer has come back with this:
‘Whenever I add any code for extra field functionality it generates one text filed ‘AnsPress Input Field’. It does not belong to the code and has different label code.
Everytime it say text filed. no upload field displays and the label is also not changing.
I found where it is comes form ‘/lib/form/class-input.php’
when i give this function subtype => ‘file’
not working
I have attached 3 screenshots. I tried with the newest code you provided but no luck.’

Posted new comment

Please share snippet of your custom field.

Hi Rahul. I have shared the snippet below. Thanks for your help

1

array(
   'submit_label' => __( 'Upload & insert', 'anspress-question-answer' ),
   'fields' => array(
    'image' => array(
     'label' => __( 'Image', 'anspress-question-answer' ),
     'desc'  => __( 'Select image(s) to upload. Only .jpg, .png and .gif files allowed.', 'anspress-question-answer' ),
     'type'  => 'upload',
     'save'  => [ __CLASS__, 'image_upload_save' ],
     'upload_options' => array(
      'multiple'  => false,
      'max_files' => 1,
      'allowed_mimes' => array(
       'jpg|jpeg' => 'image/jpeg',
       'gif'      => 'image/gif',
       'png'      => 'image/png',
      ),
     ),
     'validate' => 'required',
    ),
   ),
  );

Answered question