Skip to content

Commit 5246984

Browse files
luizbillsk1sul1
authored andcommitted
Add filters to edit uploaded file name and path (#134)
1 parent b19fd27 commit 5246984

2 files changed

Lines changed: 31 additions & 6 deletions

File tree

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,25 @@ add_filter('wplf_dynamic_values', function($values) {
198198
// <input type="text" placeholder="%SOMETHING%" name="something">
199199
```
200200

201+
202+
### Filter: wplf_uploaded_file_name
203+
If you choose to not add uploaded files to the media library, you can change the file upload name.
204+
205+
```php
206+
add_filter('wplf_uploaded_file_name', function($name, $file, $id) {
207+
return "my_".$name;
208+
}, 10, 3);
209+
```
210+
211+
### Filter: wplf_uploaded_file_path
212+
If you choose to not add uploaded files to the media library, you can change the file upload path.
213+
214+
```php
215+
add_filter('wplf_uploaded_file_path', function($name, $file, $id) {
216+
return $name.".userfile";
217+
}, 10, 3);
218+
```
219+
201220
## Plugins
202221
1.5 exposes a new function, `wplf()`. It simply returns the class instance of WP Libre Form.
203222

inc/wplf-ajax.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ function wplf_ajax_submit_handler() {
1717
// @see: wplf-form-validation.php
1818
$return = apply_filters( 'wplf_validate_submission', $return );
1919

20-
if ( $return->ok ) {
21-
// form existence has already been validated via filters
22-
$form = get_post( intval( $_POST['_form_id'] ) );
20+
// form existence has already been validated via filters
21+
$form = get_post( intval( $_POST['_form_id'] ) );
2322

23+
if ( $return->ok ) {
2424
// form-specific validation
2525
$return->slug = $form->post_name;
2626
$return->title = $form->post_title;
@@ -93,10 +93,16 @@ function wplf_ajax_submit_handler() {
9393
add_post_meta( $post_id, $key . '_attachment', $attach_id );
9494
}
9595
} else {
96-
$name = 'lf_' . date( 'ymdhs' ) . '-' . $counter . '-' . sanitize_file_name( $file['name'] );
96+
$file['field_name'] = $key;
97+
98+
$default_file_name = 'lf_' . date( 'ymdhs' ) . '-' . $counter . '-' . $file['name'];
99+
$file_name = sanitize_file_name( apply_filters( 'wplf_uploaded_file_name', $default_file_name, $file, $post_id ) );
100+
101+
$file_path = $uploads_path['path'] . '/' . $file_name;
102+
$file_path = apply_filters( 'wplf_uploaded_file_path', $file_path, $file, $post_id );
97103

98-
move_uploaded_file( $file['tmp_name'], $uploads_path['path'] . '/' . $name );
99-
add_post_meta( $post_id, $key . '_attachment', $uploads_path['url'] . '/' . $name );
104+
move_uploaded_file( $file['tmp_name'], $file_path );
105+
add_post_meta( $post_id, $key . '_attachment', $file_path );
100106
$counter++;
101107
}
102108
}

0 commit comments

Comments
 (0)