Ecommerce Shopify WordPress Discussion

Rename product image as product title in wordpress

i am tryig to rename images in my woocommerce store. They are uploading in the library with names like 123456.png or main.jpg etc. My goal is to make image name, description and alt text be the same as the product title with a php snippet. I found out that in media library of wordpress there is a field called "Uploaded to" whitch is the post-product that the image attached on and i want to keep the title of this product and set it as title in my image , as shown here. But i dont know how to get the wordpress "Uploaded to" value and use it as a name in images. After some research i found this question (Wordpress rename uploaded images based on the post_title) but the code doesnt worked for me. This is the code that i tryed to use. Also i tryed some plugins and other codes that didnt helped me to find out a solution. Im new in php ,if someone can help i would be glad! 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 ); } }
TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.