Automattic / Automattic/WP-Job-Manager
Allow to limit upload file types in admin panel for new job publications.
- Dominant language
- PHP
- Stars
- 899
- Forks
- 369
- Avg merge
- 11h 37m
- Merged PRs (30d)
- 12
Description
### Is your feature request related to a problem? Please describe
Currently we can't limit the file type in a file upload through the admin panel.
```
// 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' );
```
### Describe the solution you'd like
Allow to limit the file type accepted for fields with 'file' type.
Something like:
```
// 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',
'allowed_mime_types' => [
'pdf' => 'application/pdf'
]
);
return $fields;
}
add_filter( 'job_manager_job_listing_data_fields', 'admin_add_extra_fields' );
```
### Describe prososals/ideas
#### Check file types on backend when processing the data fields
When persisting the data fields do some validation against the `allowed_mime_types` property which would be included – this would be similar to what is done in the frontend (when submitting job):
```
// 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',
'allowed_mime_types' => [
'pdf' => 'application/pdf'
]
);
return $fields;
}
add_filter( 'submit_job_form_fields', 'frontend_add_extra_fields' );
```
#### Limit file type in UI
Another option would be to limit the ui to select only the accepted file types.
An initial approximation could be adding `library: { type: "application/pdf" }` when creating the media frame in `assets/js/admin.js`. But this solution has some caveats: this change only applies to the media library selector but user can still upload and use a new file with whatever type.
This is also happening, for example when selecting the company logo from the admin panel – you can only select images from the library but if you upload a new file any file will do. Looks like wp media library does not have this covered.
### Additional context
User report: p1651065850784059-slack-C6GGX896G
Contributor guide
Research direction
Start by tracing how file-type validation is handled for frontend job submissions and how admin data fields are persisted. Inspect assets/js/admin.js for the media-frame behavior, then determine whether completion requires backend validation, UI filtering, or both. Done means admin file fields consistently enforce the configured allowed MIME types.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- backend
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100