Home Forums Backend Issues (wp-admin) Auto rename a picture of a custom field


Auto rename a picture of a custom field

  • Hello, I’m asking for your help because I want to automatically change the name and title of an image when I upload. I wrote a function that identifies the type of post and add a term, but I would like for my custom field “cover” write specific code to indicate that it is just a cover, such as: gears-of-war-cover-xbox-360.jpg

    here is my code

    function wpsx_5505_modify_uploaded_file_names($arr) {
        // 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 && is_numeric($post_id)) {
            $post_title = get_the_title($post_id);
    		if (get_post_type($post_id) != 'post') {
    			$post = get_post_type($post_id);	
    			$object = get_post_type_object($post);
    			$value = $object->labels->singular_name;
    			$post_title = $post_title.'-'.$value;
    		} else {
    			$cat = get_category_by_slug('category-name');	
    			$post_title = $post_title.'-'.$cat->term_id;
    		if (get_post_type($post_id) == 'tests' || 'impressions' == get_post_type($post_id))  {
    			$post_title = $post_title.'-'.get_the_term('support', $post_id);
    		} elseif ('cosplay-gallery' == get_post_type($post_id)) {
    			foreach(get_field('card', $post_id) as $post_object) {
    				$value = get_field('pseudo', $post_object->ID);
    				$post_title = $value.'-'.$post_title;
            if($post_title) {
    			$post_title = strtolower($post_title);
    			$post_title = strtr($post_title, 'áàâäãåçéèêëíìîïñóòôöõúùûüýÿ', 'aaaaaaceeeeiiiinooooouuuuyy');
    			$post_title = str_replace(' ', '-', $post_title);
                $random_number = rand(10000,99999);
                $arr['name'] = $post_title . '-' . $random_number . '.jpg';
        return $arr;
    add_filter('wp_handle_upload_prefilter', 'wpsx_5505_modify_uploaded_file_names', 1, 1);

    This code is not perfect, also if you have any tips to improve it. I’m interested. Thank you a lot for your help.

  • Hi @shingo-san

    I’m finding the question hard to understand.

    Can you be very specific: eg.

    When I upload any image, I would like to save a custom field value to the image itself. This field is called … and it would be populated by this logic…


  • Thank you for your reply. I have a custom field image, but I want the image file is renamed during the upload. For example, when I upload an image to a custom post type review, it automatically renames the file “gears-of-war xbox-360-review-4565.jpg” or “gears-of-war-xbox-360-preview.jpg “. So I would like for a cover, the file is automatically renamed as “gears-of-war-xbox-360-cover.” Should therefore add a piece of code that determines if the upload is done from the custom field image “cover” and add “cover” in the new filename.

  • Hi @shingo-san

    I’m not sure if this is possible.

    The WP uploader has no idea what field it is being uploaded from, as this data is not posted to the upload script.

    Sorry, but I am unable to recommend a solution for this. Hopefully someone reading this can offer their 2 cents.


Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Auto rename a picture of a custom field’ is closed to new replies.