• Resolved M@X

    (@cityz)


    Hello!
    First of all, thank you for the great plugin and awesome documentation.

    I’m trying to add an extra field, which is the file type field and I want to allow attaching only PDF files.

    What I did:
    1. Added the extra field in function.php

    add_filter( 'submit_job_form_fields', 'frontend_add_extra_fields' );
    
    function frontend_add_extra_fields( $fields ) {
       $fields['job']['job_attachment'] = array(
        'label'       => __( 'Attachment', 'job_manager' ),
        'type'        => 'file',
        'required'    => false,
        'priority'    => 10,
        'description' => '*pdf only'
      );
      return $fields;
    }

    2. Then I’m trying to filter it:

    add_filter( 'job_manager_mime_types', 'filter_allowed_mime_types' );
    
    function filter_allowed_mime_types( $allowed_mime_types ) {
    if ( 'job_attachment' === $field ) {
    $allowed_mime_types = [
    'pdf' => 'application/pdf'
    ];
    } else {
    $allowed_mime_types = [
    'jpg|jpeg|jpe' => 'image/jpeg',
    'gif' => 'image/gif',
    'png' => 'image/png',
    'pdf' => 'application/pdf',
    'doc' => 'application/msword',
    'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
    'mp4' => 'video/mp4',
    ];
    }

    like at tutorial

    But it does not work, the system still allows to attach All files (*.*)
    I’ve already spent the whole day but can not find out what am I doing wrong?
    Please help.
    Thank you.

    • This topic was modified 1 year, 12 months ago by M@X.
    • This topic was modified 1 year, 12 months ago by M@X.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi @cityz!

    Could you please try using only this code:

    
    add_filter( 'submit_job_form_fields', 'frontend_add_extra_fields' );
    
    function frontend_add_extra_fields( $fields ) {
       $fields['job']['job_attachment'] = array(
        'label'       => __( 'Attachments', 'job_manager' ),
        'type'        => 'file',
        'required'    => false,
        'priority'    => 10,
        'description' => '*pdf only',
        'allowed_mime_types' => [
    		'pdf' => 'application/pdf'
    	]
      );
      return $fields;
    }
    

    Let us know how that goes!

    Thread Starter M@X

    (@cityz)

    Hi Aicee,
    thank you for your reply, but unfortunately, it doesn’t work, it still allows you to upload all files 🙁

    I also tried to do the same with the admin part in

    add_filter( 'job_manager_job_listing_data_fields', 'admin_add_extra_fields' );

    But it doesn’t work either.

    Thread Starter M@X

    (@cityz)

    *Upd:
    it still allows you to choose all files in pop up window:

    screen 1

    But the good news is it restricts to post a job when you click preview button:

    screen 2

    Is it anyway also to choose specific files in the popup window?

    I also tried to do the same with the admin part in

    add_filter( 'job_manager_job_listing_data_fields', 'admin_add_extra_fields' );
    
    function admin_add_extra_fields( $fields ) {
      $fields['_job_attachment'] = array(
        'label'       => __( 'Attachment', 'job_manager' ),
        'type'        => 'file',
        'description' => '*pdf only',
        'allowed_mime_types' => ['pdf' => 'application/pdf']
      );
      return $fields;
    }

    But it doesn’t work at all. In the admin panel, user can upload any file type 🙁

    Plugin Contributor Cena (a11n)

    (@cena)

    Hi @cityz ,

    // Add "Attachment" field to Admin Panel.  
    
    function admin_add_extra_fields( $fields ) {
      $fields['_job_attachment'] = array(
        'label'       => __( 'Attachment', 'job_manager' ),
        'type'        => 'file',
        'description' => '*pdf only',
      );
      return $fields;
    }
    
    add_filter( 'job_manager_job_listing_data_fields', 'admin_add_extra_fields' );
    
    // Add "Attachment" field to Frontend.
    
    function frontend_add_extra_fields( $fields ) {
      $fields['job']['job_attachment'] = array(
        'label'       => __( 'Attachment', 'job_manager' ),
        'type'        => 'file',
        'required'    => false,
        'priority'    => 10,
    	'description' => '*pdf only',
    	'ajax'        => true,
    	'allowed_mime_types' => [
    		'pdf' => 'application/pdf'
    	]
      );
      return $fields;
    }
    add_filter( 'submit_job_form_fields', 'frontend_add_extra_fields' );

    Please note the following caveats:

    The frontend will only “accept” PDF files but when a non-PDF file is selected, no feedback to the user is shown (simply ignores the file selection).

    When a PDF file is selected the user is shown a progress-bar and the file is displayed above the selector. This is done by adding the 'ajax' => true part to the frontend_add_extra_fields method.

    If the user prefers having the error message after submitting the job (in the frontend case) just remove the ‘ajax’ => true line.

    The Admin panel will accept any file – in order to improve this we need to change our implementation.

    As a result of this, we have created two issues:

    Enhance the experience in admin panel to allow to limit the accepted file types

    “Invalid file type” error now shown in ajax upload

    Normally we don’t provide custom code like this, but in this case testing of the original snippet revealed some areas where we can improve WPJM! 🙂

    Best,
    Cena

    Thread Starter M@X

    (@cityz)

    Cena, thank you a lot for the quick and expanded reply!
    Great, great plugin and support!

    Plugin Contributor Cena (a11n)

    (@cena)

    Thank you for the kind words, much appreciated. 🙂

    Best,
    Cena

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘File type’ is closed to new replies.