Home › Forums › General Issues › Force an Image/File upload to a particular directory › Reply To: Force an Image/File upload to a particular directory
Interestingly, get_source_field()
is in a different place in my version of ACF Pro – media.php
– and it’s a <em>private</em> function
(any way around that?)
1)
So, if I straight-up attempt to get the source field using that inside my WP upload pre-filter…
add_filter('wp_handle_upload_prefilter', 'custom_upload_filter' );
function custom_upload_filter( $file ) {
// Attempt to get the ACF field
$field = get_source_field();
// Begin testing rename - not the finished article
$file['name'] = 'and-everything-is-awesome-' . $file['name'];
return $file;
}
… then the Media uploader warns:
“Post-processing of the image likely failed because the server is busy or does not have enough resources. Uploading a smaller image may help. Suggested maximum size is 2500 pixels.”
2)
But, if I ostensibly copy the function code into my filter function…
add_filter('wp_handle_upload_prefilter', 'custom_upload_filter' );
function custom_upload_filter( $file ) {
// 1. GET SOURCE FIELD, FROM MEDIA.PHP
$field = false;
// Search for field key within available data.
// Case 1) Media modal query.
if ( isset( $_POST['query']['_acfuploader'] ) ) {
$field_key = (string) $_POST['query']['_acfuploader'];
// Case 2) Media modal upload.
} elseif ( isset( $_POST['_acfuploader'] ) ) {
$field_key = (string) $_POST['_acfuploader'];
}
// Attempt to load field.
// Note the <code>acf_get_field()</code> function will return false if not found.
if ( isset( $field_key ) ) {
$field = acf_get_field( $field_key );
}
// 2. GET USER'S ID AND USERNAME
// TEST RENAME
$file['name'] = $user_id . 'and-everything-is-awesome-' . $file['name'];
return $file;
}
… it successfully gets the field key at that point, and therefore supports getting the field, too.
So, let me think, what logic should I be using here… ?
* If this is an ACF field with the specified key (since wp_handle_upload_prefilter
will otherwise fire on every upload),
* Get the User ID for the User in the current Edit page
* Get User object and then Username slug
* Use that to recompose the filename, but retain the original extension
If so, I’m now hitting a non-ACF snag actually obtaining the current user from user-edit.php
ie. the following and another method…
// 2. GET USER'S ID AND USERNAME
// If is current user's profile (profile.php)
if ( defined('IS_PROFILE_PAGE') && IS_PROFILE_PAGE ) {
$user_id = get_current_user_id();
// If is another user's profile page
} elseif (! empty($_GET['user_id']) && is_numeric($_GET['user_id']) ) {
$user_id = $_GET['user_id'];
// Otherwise something is wrong.
} else {
die( 'No user id defined.' );
}
… provoke “An error occurred in the upload. Please try again later.” in the upload window… maybe because, the browser is still on the same page, user_id
is not available at the point the “Select Image” modal has appeared?? Hmm…
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.