[Snippet] rename the images in wordpress after upload
-
hello everyone, please read carefully this could be helpful to a lot of people.
I’ll make it short,
i developed a real estate site, so far nothing special, until i ran into the reality of things, uploading images to wordpress.since version 5.3, wordpress reduces the images (sacled) it’s super useful it allows me to avoid plugins again. I use this snippet code to avoid excess images and reduce them
// disable generated image sizes function shapeSpace_disable_image_sizes($sizes) { unset($sizes['thumbnail']); // disable thumbnail size (150px) unset($sizes['large']); // disable large size (1024px) unset($sizes['1536x1536']); // disable 2x medium-large size (1536px) //unset($sizes['medium']); // disable medium size (300px) //unset($sizes['medium_large']); // disable medium-large size (768px) //unset($sizes['2048x2048']); // disable 2x large size return $sizes; (2048px) } add_action('intermediate_image_sizes_advanced', 'shapeSpace_disable_image_sizes'); // disable scaled image size (2560px) add_filter('big_image_size_threshold', '__return_false'); //Increases the threshold for scaling big images to max 2048px (width or height) function dg_big_image_size_threshold( $threshold ) { return 2048; // your new size threshold here } add_filter('big_image_size_threshold', 'dg_big_image_size_threshold', 100, 1);
but I decided to go even further by trying for example to rename the images according to the posts. here is an example
imagine that I want to publish a property that I named “new worpdress office”/*Renaming attachment files to the post title*/ function file_renamer( $filename ) { $info = pathinfo( $filename ); $ext = empty( $info['extension'] ) ? '' : '.' . $info['extension']; $name = basename( $filename, $ext ); if( $post_id = array_key_exists("post_id", $_POST) ? $_POST["post_id"] : null) { if($post = get_post($post_id)) { return $post->post_title . $ext; } } $my_image_title = $post; $file['name'] = $my_image_title . - uniqid() . $ext; // uniqid method // $file['name'] = md5($name) . $ext; // md5 method // $file['name'] = base64_encode($name) . $ext; // base64 method return $filename; } add_filter( 'sanitize_file_name', 'file_renamer', 10, 1 ); /* Automatically set the image Title, Alt-Text, Caption & Description upon upload*/ add_action( 'add_attachment', 'my_set_image_meta_upon_image_upload' ); function my_set_image_meta_upon_image_upload( $post_ID ) { // Check if uploaded file is an image, else do nothing if ( wp_attachment_is_image( $post_ID ) ) { // Get the parent post ID, if there is one if( isset($_REQUEST['post_id']) ) { $post_id = $_REQUEST['post_id']; } else { $post_id = false; } if ($post_id != false) { $my_image_title = get_the_title($post_id); } else { $my_image_title = get_post( $post_ID )->post_title; } // Sanitize the title: remove hyphens, underscores & extra spaces: $my_image_title = preg_replace( '%\s*[-_\s]+\s*%', ' ', $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', $my_image_title ); // Set the image meta (e.g. Title, Excerpt, Content) wp_update_post( $my_image_meta ); } }
my question ,
-Is it possible to go further and improve/ optimize this code, for example add instead of (-scaled) a [width x height] or other suffix.
-Add a line which allows us to delete the original images. (For specific cases)
-Any Ideas or suggestions to make it more efficient and SEO friendly?
- The topic ‘[Snippet] rename the images in wordpress after upload’ is closed to new replies.