Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Modify Video Thumbnails Plugin to save filename = post ID WordPress

  • SOLVED

Hi, I am struggling with an urgent problem. I went live with my site at the beginning of the week and there was a lot of traffic, but there is a problem preventing the site from working properly, which is costing traffic and interest in the site.

I am using a modified version of video thumbnails plugin http://wordpress.org/extend/plugins/video-thumbnails/ (which saves different sized versions of the generated thumbnail and sets it as featured image, each different size, has the dimensions appended to it's filename.)
I am using WP Read-Only plugin http://wordpress.org/extend/plugins/wpro/ to save the generated thumbnails to AWS S3.

So now my problem is that when for eg. a youtube video is added to the site, the video thumbnails plugin pulls the original thumbanil from youtube which has a default filename called maxresdefault.jpg.
If I were to use the server to store these files, wordpress would check the uploads folder fore duplicate filenames, and if found, append a number to the newly generated file. eg maxresdefault1.jpg, maxresdefault2.jpg, ...
I can't do this do to multiple reasons (non persistant storage on server, limited space, s3 is faster, ...)
So when the above wpro plugin pushes the first generated file to S3, the next time it genereates a file with the same name, wordpress just checks the uploads folder on the server and because it's empty, doesn't append a number to it, so it saves it as maxresdefault as well and wpro pushes the new file to S3 which then <strong>overrites</strong> the old one.

A nother problem which might have to do with sotring the files on AWS S3 is that when I copy a link to facebook for eg, it never fetches the right thumbnail, just some radnom ones... how to fix this? must I somehow tell wordpress to look for the files on S3 instead of on it's server?

thanks a lot for you help, much appreciated!
cheers, David

Answers (1)

2013-03-20

Daniel Yoen answers:

Try this in functions.php :-)

function node_images_filename()
{
$alphabet = "abcdeghijklmnoqrsuvwxyz";
$pass = array();
$alphaLength = strlen($alphabet) - 1;
for ($i = 0; $i < 10; $i++)
{
$n = rand(0, $alphaLength);
$pass[] = $alphabet[$n];
}
return implode($pass);
}

add_filter('wp_handle_upload_prefilter', 'custom_upload_filter' );
function custom_upload_filter( $file )
{
$ext = substr(strrchr($file['name'],'.'),1);
$file['name'] = 'prefix-' . node_images_filename() . "." . $ext;

return $file;
}


hopt this help :-)


davbrau comments:

thanks, for the quick response, will try that in a couple of mins. can you please briefly describe what exactly that will do?


Daniel Yoen comments:

This script will change media filename with random string shortly after upload(steam) :-)

hope this help :-)


davbrau comments:

but it doesn't include a duplicate-check right? so the random prefix could be repeated on a future file, which would then overrite the original. is it possible to prepend the post ID to the file because that's a unique number that never repeats itself. (to clarify, each "post" is a custom post called "video" and contains only the generated video thumbnail, titel, description etc. so there aren't going to be multiple thumbnails in a single post, which makes the post ID a vayable piece of information to prepend in order to make the thumbnail unique)


Daniel Yoen comments:

this script add attachment ID(unique) in filename "89-filename.jpg".

function filename_postid($filename) {

if( isset($_REQUEST['post_id']) ) {
$post_id = (int)$_REQUEST['post_id'];
}else{
$post_id=0;
}

$info = pathinfo($filename);
$ext = empty($info['extension']) ? '' : '.' . $info['extension'];
$name = basename($filename, $ext);

if($post_id>0){
return $post_id."_".$name . $ext;
}else{
return $name . $ext;
}
}
add_filter('sanitize_file_name', 'filename_postid', 10);


In my previous answer, add uniqid() to make file is unique.

function dnode_images_filename()
{
$alphabet = "abcdeghijklmnoqrsuvwxyz";
$pass = array();
$alphaLength = strlen($alphabet) - 1;
for ($i = 0; $i < 10; $i++)
{
$n = rand(0, $alphaLength);
$pass[] = $alphabet[$n];
}
return implode($pass);
}

add_filter('wp_handle_upload_prefilter', 'custom_upload_filter' );
function custom_upload_filter( $file )
{
$ext = substr(strrchr($file['name'],'.'),1);
$file['name'] = uniqid() . 'node-' . dnode_images_filename() . "." . $ext; //here

return $file;
}


hope this help


Daniel Yoen comments:

note: you only need to use one of these two scripts :-)


davbrau comments:

just tested both scripts, unfortunately nothing happened. am I doing anything wrong? have you tested this yourself?


Daniel Yoen comments:

yes, tested by me :-)

if image already uploaded, you need to regenerate with this plugin : http://wordpress.org/extend/plugins/regenerate-thumbnails/


davbrau comments:

I tried it but its not doing anything. Does it matter where I put it in functions.php? I tried it at the bottom and the top, - no difference... Could there be a conflict with some other code in functions.php? I tried adding a video and letting the plugins automatically create the thumbnail, but the prefix your code should have added wasn't added. I then also tried just manually uploading an image but your code didn't trigger here either. Any suggestions/alternatives?


Daniel Yoen comments:

Sorry for my stupit think,

I think you need to Inject code to video-thumbnails.php in plugin folder not in functions.php

search this line(maybe in line : 314)

$upload = wp_upload_bits(basename( $new_thumbnail ), null, $image_contents );

replace with :

$upload = wp_upload_bits( uniqid() . basename( $new_thumbnail ), null, $image_contents );

aslo, you can add custom prefix, like :

$upload = wp_upload_bits( "prefix-" . uniqid() . basename( $new_thumbnail ), null, $image_contents );

hope this help :-)


davbrau comments:

Great! that worked and is a valid option, would it be possible though, to have the prefix be the post title instead of "prefix-"? and how to add the uniqid to all uploads? because if one would manually upload a file, it won't get processed by video-thumbnails.php and will bypass getting the uniqid prepended.


Daniel Yoen comments:

For prefix post title, I'm working on right now, I will let you know as soon as possible. :)

If media upload manually, you can use my previous script.

function dnode_images_filename()

{

$alphabet = "abcdeghijklmnoqrsuvwxyz";

$pass = array();

$alphaLength = strlen($alphabet) - 1;

for ($i = 0; $i < 10; $i++)

{

$n = rand(0, $alphaLength);

$pass[] = $alphabet[$n];

}

return implode($pass);

}



add_filter('wp_handle_upload_prefilter', 'custom_upload_filter' );

function custom_upload_filter( $file )

{

$ext = substr(strrchr($file['name'],'.'),1);

$file['name'] = uniqid() . 'node-' . dnode_images_filename() . "." . $ext; //here



return $file;

}


hope this help :-)


Daniel Yoen comments:

this code add title into filename :

$upload = wp_upload_bits(get_the_title($post_id) . "-" . uniqid() . "-" . basename( $new_thumbnail ), null, $image_contents );

Hope this help

:-)


davbrau comments:

Daniel, thanks a lot for your excellent work. Your code is almost perfect :) The above code for manual media uploading removes the original file name and replaces it with "<uniqid><node-><some-random-letters>". would it be possible to keep the filename and have it just "<file name>-<uniqid>" ?


Daniel Yoen comments:

try this :-)

add_filter('wp_handle_upload_prefilter', 'custom_upload_filter');
function custom_upload_filter($file)
{
$ext = substr(strrchr($file['name'], '.'), 1);
$file['name'] = $file['name'] . "-" . uniqid() . "." . $ext;
return $file;
}


hope this help :-)


davbrau comments:

Thanks a lot for your help Daniel, can definately see us working together in the future again!