Skip to content

Instantly share code, notes, and snippets.

@m-thomson
Last active June 23, 2023 14:12
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save m-thomson/9c890a3e93a63295da39e2b7f7169922 to your computer and use it in GitHub Desktop.
Save m-thomson/9c890a3e93a63295da39e2b7f7169922 to your computer and use it in GitHub Desktop.
WP All Import duplicate image problem

When an image is imported, the name used in the media library is derived from the URL "basename". This can cause problems, especially when using the option "Search through the Media Library for existing images before importing new images". For example, even though they are different images, these URLs all have the same "basename" (in bold). They will generate the same file my-image.jpg in the WordPress library:

http:// example.com/ my-image.jpg
http:// example.com/ my-image?id=1
http:// example.com/subfolder/ my-image.jpg

The solution is to put this PHP code in your function editor:

function fix_img_url($url){
 return empty($url) ? "" : $url."#/".substr(md5($url),0,8).".jpg";
}

And put the following in your image field, substituting the correct field name for {image[1]}:

[fix_img_url({image[1]})]

This forces WP All Import to generate an image name derived from the entire URL instead of just the basename.

@m-thomson
Copy link
Author

If all your images are together in a single field, separated by a comma or other character. Use this instead:

// Loop through each image url and apply fix.
function fix_img_urls($urls) {
	$output = array();
        $separator = ","; // Change this to your separator character
	foreach (explode($separator,$urls) as $url) {
                $url = trim($url);
		$output[] = empty($url) ? "" : $url."#/".substr(md5($url),0,8).".jpg";
	}
	return implode(",",$output);
}

Example invocation:

screenshot_c53b01

@Webber2014
Copy link

Thanks for this, very helpful!

As setting you'll also need to check 'Set the first image to the Featured Image (_thumbnail_id)'

@webelis
Copy link

webelis commented May 2, 2018

You clearly saved my day !

Kudos+++ on this one :)

@maclordaj
Copy link

Wow, this was hard to find! In my case I believe it had something to do with the filename format and length (using a large uid). THANKS

@adtomlinson
Copy link

This is amazing, thanks so much!

However, I do have a question.... I have JPGs, PNGs and PDFs with the file naming issue so how can I apply this too all 3 please?

Big thumbs up here :-)

@m-thomson
Copy link
Author

To anyone reading this:

I haven't worked for the company that makes WP All Import in several years and I don't use their plugin currently (as I'm not doing WordPress work). So unfortunately, I won't be able to answer your questions.

However, the support the company offers is pretty great. In fact it's some of the best I've ever seen. I encourage you to reach out to them on https://www.wpallimport.com with any questions.

I am leaving these gists up however since they do seem to help for quick reference.

Best of luck.
Mark

@solaceten
Copy link

How does this work with external images downloaded from elsewhere?

e.g. in my settings > image box I have
https://www.example.com/folder/{listingnumber[1]}-1.jpg,
https://www.example.com/folder/{listingnumber[1]}-2.jpg,
https://www.example.com/folder/{listingnumber[1]}-3.jpg

I am finding duplicate images in media library after each import..... WPAI seems to add each file name ok e.g.
listingnumber-8-3.jpg

but the previous import named it:

File name: listingnumber-8-2.jpg

so how can I use the technique with my usecase?

Thanks

@chetanupare
Copy link

hey,
how can we do this for content images in tag?

@buttonsbond
Copy link

Thanks for that, a must if you are importing properties which potentially have same filenames too even from multiple sources.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment