Home › Forums › General Issues › Force an Image/File upload to a particular directory › Reply To: Force an Image/File upload to a particular directory
John, thanks for the add_attachment
tip.
I also found similar code at https://wordpress.stackexchange.com/a/310689/39300
Whilst it assumes Post is the connected object, it also works for User.
The following code now takes the connected User’s name and uses that also for Title and Alt fields.
All told, now I have:
Thanks!
/**
* ==============================================================================
* ALSO SET IMAGE TITLE & ALT TO USER/POST NAME
* When an image is uploaded, use the attached Post (User) edtails
* to set Title, Alt fields etc.
* cf. https://wordpress.stackexchange.com/a/310689/39300
* cf. https://support.advancedcustomfields.com/forums/topic/force-an-image-file-upload-to-a-particular-directory/page/2/
* ==============================================================================
*/
// https://wordpress.stackexchange.com/a/310689/39300
function my_set_image_meta_upon_image_upload( $post_ID ) {
// "the first thing that your action should do is to remove
// itself so that it does not run again."
remove_filter('add_attachment', 'your_function_name_here');
// Check if uploaded file is an image, else do nothing
if ( wp_attachment_is_image( $post_ID ) ) {
$my_image_title = get_post( $post_ID )->post_title;
// Added by Robert Andrews
// Get user name to use in image details
$user = get_user_by( 'slug', $my_image_title );
$user_name = $user->display_name;
$my_image_title = $user_name;
// Sanitize the title: remove hyphens, underscores & extra spaces:
// $my_image_title = preg_replace( '%[-_]+%', ' ', $my_image_title );
// Sanitize the title: capitalize first letter of every word (other letters lower case):
// $my_image_title = ucwords( strtolower( $my_image_title ) );
// Create an array with the image meta (Title, Caption, Description) to be updated
// Note: comment out the Excerpt/Caption or Content/Description lines if not needed
$my_image_meta = array(
'ID' => $post_ID, // Specify the image (ID) to be updated
'post_title' => $my_image_title, // Set image Title to sanitized title
// 'post_excerpt' => $my_image_title, // Set image Caption (Excerpt) to sanitized title
// 'post_content' => $my_image_title, // Set image Description (Content) to sanitized title
);
// Set the image Alt-Text
update_post_meta( $post_ID, '_wp_attachment_image_alt', 'Photo of '.$my_image_title );
// Set the image meta (e.g. Title, Excerpt, Content)
wp_update_post( $my_image_meta );
}
}
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.