Skip Navigation

[Resolved] Split: Rename uploaded images

This support ticket is created 5 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 10 replies, has 2 voices.

Last updated by jasonw-2 5 years, 6 months ago.

Assisted by: Nigel.

Author
Posts
#1099444

The one thing I also must have is to be able to automatically re-name these images on upload, for SEO. I was hoping by turning off the ajax that my image re-namer would work, but alas.. nope. Is there any possible way to re-name these images with the post title on upload? I'd love for Cred to be able to do that instead of having to have another plugin (I'm trying to use Meow Media Re-Namer Pro but it's not working. Any ideas on how I can make this happen at all?

#1103818

I think this is what I need for it to work?

"you simply need to call this function available globally:

mfrh_rename( $mediaId ) 

Media File Renamer will rename this media entry depending on your settings".

Thank you again for looking into this. I very much appreciate it!!

#1104345

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Jason

I will get back to this, it's just the queue is very busy at the moment...

#1104679

No worries at all. Take your time. 🙂

#1112972

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

OK, I had a little time to look at this and came up with a solution.

Remember, this requires that ajax uploads for the images are disabled (otherwise the image is uploaded before the form is submitted, so there is no post to get the title from yet).

You'll need to add the following to your site (note that Types 3.1 includes a code snippets editor where you can add code without having to edit your theme files, at Toolset > Settings):

/**
 *  Disable ajax uploads of images in Toolset Forms (global)
 */
add_filter( 'cred_file_upload_disable_progress_bar', '__return_true' );


/**
 *  For specified forms set up file rename to post title
 */
function tssupp_prepare_to_rename_upload( $form_data ){

	if ( in_array( $form_data['id'], array( 103 ) ) ) { // Edit Form IDs

		add_filter( 'wp_handle_upload', 'tssupp_handle_rename_upload', 10, 2 );

	}
}
add_action( 'cred_before_save_data', 'tssupp_prepare_to_rename_upload' );


/**
 *  Handle file rename taking title from $_REQUEST object
 */
function tssupp_handle_rename_upload( $upload, $context ){

	$post_title = $_REQUEST['post_title'];
	$post_slug = sanitize_title( $post_title, 'empty title' );


    $path = pathinfo( $upload['file'] );
    $newfile = $path['dirname']."/".$post_slug.".".$path['extension'];
	rename( $upload['file'], $newfile );
	$upload['file'] = $newfile;

	$dir = wp_upload_dir();
	$upload['url'] = $dir['url'].'/'.$post_slug.".".$path['extension'];

	return $upload;
}

You'll need to edit the ID(s) of the Forms you want to apply this to.

#1113064

Testing it now.

#1113178

Nigel,

It doesn't appear to be working. That as well as it's still bouncing out the featured image and I'm getting ximg-1-23.jpg.pagespeed.ic.V4vPGX1X6u.webp for the images at different times? Hopefully I'm not doing something wrong...

#1114902

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Screen Shot 2018-09-25 at 09.07.20.png

Hi Jason

I tested the code on my local copy of your site and it seems to be working okay, except I didn't allow for the possibility of uploading multiple images (including the featured image).

So I made a few edits to the code to handle that. This is the code that works on the copy of your site (including Form IDs):

<?php
/**
 *  Disable ajax uploads of images in Toolset Forms (global)
 */
add_filter( 'cred_file_upload_disable_progress_bar', '__return_true' );
 
 
/**
 *  For specified forms set up file rename to post title
 */
function tssupp_prepare_to_rename_upload( $form_data ){
 
    if ( in_array( $form_data['id'], array( 49, 141 ) ) ) { // Edit Form IDs
 
        add_filter( 'wp_handle_upload', 'tssupp_handle_rename_upload', 10, 2 );
 
    }
}
add_action( 'cred_before_save_data', 'tssupp_prepare_to_rename_upload' );
 
 
/**
 *  Handle file rename taking title from $_REQUEST object
 */
function tssupp_handle_rename_upload( $upload, $context ){
 
    $post_title = $_REQUEST['post_title'];
    $post_slug = sanitize_title( $post_title, 'empty title' );
 
 
    $path = pathinfo( $upload['file'] );
    $newfile = $path['dirname']."/".$post_slug.".".$path['extension'];
    $dir = wp_upload_dir();
    $newurl = $dir['url'].'/'.$post_slug.".".$path['extension'];

    // handle multiple uploads or if filename already exists
    $index = 1;
    while ( file_exists($newfile) ) {
        $newfile = $path['dirname']."/".$post_slug.'-'.($index ++).".".$path['extension'];
        $newurl = $dir['url'].'/'.$post_slug.'-'.$index.".".$path['extension'];
    }

    rename( $upload['file'], $newfile );
    $upload['file'] = $newfile;
    $upload['url'] = $newurl;
 
    return $upload;
}

You can see the results in the uploads folder in the screenshot.

The first time I uploaded just a featured image to a post titled "Farmyard equipment".

The second time I uploaded a bunch of images to a post titled "Construction kit", where each additional image had an index number appended.

I checked in the backend for the newly created listing and everything appears correct in terms of featured image and image fields assigned.

That's about as far as I can take this. Writing custom code falls outside our support policy, so if you can't get it working on your own install you'll need to see if you can work out why yourself, or hire a developer to work on it.

As I say, I double-checked that the featured images were correctly assigned.

If you are still having problems with this, could you comment on the original thread, and keep this one about renaming the file uploads, thanks.

#1115100

You rock, Nigel !!

One last issue with this. There's a glitch with the featured image. When I am uploading these pictures, I am using the same picture for the featured image and then it is the first picture in the slider as well. So it ends up like this:

(featured image) 1.jpg

(more images) 1.jpg 2.jpg 3.jpg

The featured is basically becoming a 404 broken image on upload.

I can't thank you enough for you help with this. I'm confident there will be more people that will be just as happy to have this feature you've created. 🙂 (of course I seem to always be the guy who can break anything. lol).

#1116491

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Jason

OK, I did some more thorough testing, and there was a glitch in my code, which I've rectified below:

<?php
/**
 *  Disable ajax uploads of images in Toolset Forms (global)
 */
add_filter( 'cred_file_upload_disable_progress_bar', '__return_true' );
 
 
/**
 *  For specified forms set up file rename to post title
 */
function tssupp_prepare_to_rename_upload( $form_data ){
 
    if ( in_array( $form_data['id'], array( 49, 141 ) ) ) { // Edit Form IDs
 
        add_filter( 'wp_handle_upload', 'tssupp_handle_rename_upload', 10, 2 );
 
    }
}
add_action( 'cred_before_save_data', 'tssupp_prepare_to_rename_upload' );
 
 
/**
 *  Handle file rename taking title from $_REQUEST object
 */
function tssupp_handle_rename_upload( $upload, $context ){
 
    $post_title = $_REQUEST['post_title'];
    $post_slug = sanitize_title( $post_title, 'empty title' );
 
 
    $path = pathinfo( $upload['file'] );
    $newfile = $path['dirname']."/".$post_slug.".".$path['extension'];
    $dir = wp_upload_dir();
    $newurl = $dir['url'].'/'.$post_slug.".".$path['extension'];

    // handle multiple uploads or if filename already exists
    $index = 0;
    while ( file_exists($newfile) ) {
        $index ++;
        $newfile = $path['dirname']."/".$post_slug.'-'.$index.".".$path['extension'];
        $newurl = $dir['url'].'/'.$post_slug.'-'.$index.".".$path['extension'];
    }

    rename( $upload['file'], $newfile );
    $upload['file'] = $newfile;
    $upload['url'] = $newurl;
 
    return $upload;
}

I've tested it pretty thoroughly and I'm fairly sure it works as expected now.

Let me know if you find otherwise.

A colleague is writing something for clients about uses of the APIs and was asking for examples. I mentioned your project to him, I hope that's okay.

#1118520

Nigel,

You and the entire Toolset team are incredible. Thank you so much!!!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.