Plugin relocate-upload – Not quite working in 2.7
-
I’m afraid this plugin is not working (at least for me) in WP 2.7 because when a file is first uploaded to the default folder the corresponding row in db table wp_postmeta holds just the filename, not path and filname. So
$attachment_path=get_post_meta($id,"_wp_attached_file",true);
does not get the right value. That is why
if (rename($attachment_path,$new_path))
does not work and returns false.Maybe you should check if the filename is stored with or without path in wp_postmeta and construct $attachment_path accordingly.
https://www.ads-software.com/extend/plugins/relocate-upload/
-
you are absolutely right – the _wp_attached_file data used to be the full path, and now it looks like its just the path relative to something like WP_CONTENT_DIR/uploads.
let me check through the 2.7 code – it has to be back compatible with a full path starting with a /, as upgrading doesn’t update old values.
so i probably just need to detect these new relative paths and tack on the base. Let me have a bash at that.
(trouble is i don’t have a 2.7 on a standard setup to test this on – just a local MAMP setup that sets up some global PHP vars a little awkwardly.)
[edit update]
well that was easy enough to find! the get_attached_file function has been updated in 2.7 to reflect this – and as i should have been using that all along in my code. it’s a simple change that will stay compatible with existing versions. I’ll update the repository soon, but if you want to try it yourself…
find line 38:
$attachment_path=get_post_meta($id,"_wp_attached_file",true);
replace with:
$attachment_path=get_attached_file( $id, true);
[edit update 2]
well that half fixes 2.7ness. There is at least one more issue i can see — the relocate menu doesn’t work on the page you get when you have just uploaded a new asset.that needs an extra line about line 86
if ( strpos($_SERVER['REQUEST_URI'], "/wp-admin/media-upload.php")===false && strpos($_SERVER['REQUEST_URI'], "/wp-admin/upload.php")===false && strpos($_SERVER['REQUEST_URI'], "/wp-admin/media.php")===false ) return;
needs an extra line
if ( strpos($_SERVER['REQUEST_URI'], "/wp-admin/media-upload.php")===false && strpos($_SERVER['REQUEST_URI'], "/wp-admin/upload.php")===false && strpos($_SERVER['REQUEST_URI'], "/wp-admin/media-new.php")===false && strpos($_SERVER['REQUEST_URI'], "/wp-admin/media.php")===false ) return;
basically i need to look at this properly to get it 2.7’d up. Feel free to post more issues!
Firstly, Happy new year to you.
Secondly, thanks very much for this plugin- I am setting up a blog for a mate which is to have header images selected randomly at pageload. He needs to be able to upload new images into the header image pool folder, ideally through WP’s admin. This is the only way I’ve found to do that.
I’m running 2.7 and made the change to line 38 OK, but I can’t find the 2nd edit anywhere in the 0.11 file- there’s only one mention of strpos and that’s on line 135 of the plugin. Or are you talking about editing a core file?
Before and after making the edit, when trying to define a new folder in the Locations control panel page I get a page returned with just “Cannot load relocate-upload/relocate-upload.php.” in a box in the middle.
Any help getting this going’d be mach appreciated.
hi – yes, sorry that is confusing. those lines are in the development version which you can get here:
https://www.ads-software.com/extend/plugins/relocate-upload/download/
not sure about the source of the “cannot load relocate-upload/relocate-upload.php” error. sorry.
i have been dragging my heels on making this 2.7 i realise — been working on another plugin more. will get on this soon.
Excellent-
That worked a treat, thanks a lot!
The error page didn’t show again once on the dev version & having made the above edits.
Now I’ve discovered another problem- WP automatically creates and dumps its default set of sized images in the same folder which pretty much screws up what I’m trying to do as I only want the original file to be in the special headers folder I created.
Be great if you could add an option to stop WP creating its assorted image sizes on upload. Or is there another way around this that I’m missing?
cool, glad that worked out for you ??
as for the thumbnail images that WP makes by default, relocate upload does move them to follow the original item. just so you know.
stopping WP making those thumbnails seems like an odd thing to want to do – they don’t do any harm, and are quite useful when browsing the media library for instance.
but you could inhibit their creation with a bit of code if you *really* needed to i suppose. i might dig around and see what sort of code could be written for you, but i wouldn’t add it to this plugin though, sorry.
the thumbnail code in WP core code (in the wp_generate_attachment_metadata function) is:
$sizes = array('thumbnail', 'medium', 'large'); $sizes = apply_filters('intermediate_image_sizes', $sizes); foreach ($sizes as $size) { [code to make thumbnails] }
so your theme's functions.php could call that filter and return an empty array.
add_filter('intermediate_image_sizes', 'intermediate_image_sizes_suppress'); function intermediate_image_sizes_suppress($sizes) { return (array) null;}
or something LIKE that. or does foreach fail with an empty array? i can't recall right now. but you get the general idea there.
i'd just let it go ahead and make them TBH
Thanks, I’ll have a play around with that code, but don’t think I’ve got the skills to do what I need.
The reason the resized files can’t be there is that there’s a PHP script which chooses an image at random from any of the JPEGs that are in that folder for use as a header graphic.
I do want the normal thumbnail creation elsewhere, just not in that folder. I guess that is a pretty unusual request and can see why you wouldn’t be interested in adding the feature ??
Looks like I’ll have to manually upload the images into the right folder. Ah well, was worth a try.
Thanks anyway
“there’s a PHP script which chooses an image at random from any of the JPEGs that are in that folder”
if you post that code, i could suggest changes to filter out the thumbnails – as they have a very specific filename and could be easy to identify and filter out.
Wow- thanks muchly. Really appreciated. Hope this formats OK…
<?php $folder = '.'; $extList = array(); $extList['gif'] = 'image/gif'; $extList['jpg'] = 'image/jpeg'; $extList['jpeg'] = 'image/jpeg'; $extList['png'] = 'image/png'; $img = null; if (substr($folder,-1) != '/') { $folder = $folder.'/'; } if (isset($_GET['img'])) { $imageInfo = pathinfo($_GET['img']); if ( isset( $extList[ strtolower( $imageInfo['extension'] ) ] ) && file_exists( $folder.$imageInfo['basename'] ) ) { $img = $folder.$imageInfo['basename']; } } else { $fileList = array(); $handle = opendir($folder); while ( false !== ( $file = readdir($handle) ) ) { $file_info = pathinfo($file); if ( isset( $extList[ strtolower( $file_info['extension'] ) ] ) ) { $fileList[] = $file; } } closedir($handle); if (count($fileList) > 0) { $imageNumber = time() % count($fileList); $img = $folder.$fileList[$imageNumber]; } } if ($img!=null) { $imageInfo = pathinfo($img); $contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ]; header ($contentType); readfile($img); } else { if ( function_exists('imagecreate') ) { header ("Content-type: image/png"); $im = @imagecreate (100, 100) or die ("Cannot initialize new GD image stream"); $background_color = imagecolorallocate ($im, 255, 255, 255); $text_color = imagecolorallocate ($im, 0,0,0); imagestring ($im, 2, 5, 5, "IMAGE ERROR", $text_color); imagepng ($im); imagedestroy($im); } } ?>
the important bit there is
if ( isset( $extList[ strtolower( $file_info['extension'] ) ] ) ) { $fileList[] = $file; }
which adds a file to the list of random candidate under the condition that the file is in the list of extensions defined at the top of the code. All you need do is be more picky and add in a AND the filename doesn’t match a certain pattern.
Now I think i’m right in saying that all the WP thumbnails end in a suffix like “-nnnxmmm.” which is to say a dash then the dimensions separated by an ‘x’ and then the usual .gif/.png/.jpg rider. So adding something like:
&& !preg_match ( "/-\d+x\d+\./" , $file_info['basename'] )
should extend the IF to only add the file if the original condition is true AND (&&) there is NOT (!) a match in the filename for: ‘-‘, some decimal digits, ‘x’, some more decimal digits, ‘.’
??
Thanks for explaining- I almost get it ??
And it works an absolute treat. Many thanks to you Alan.
Hi Alan, I just downloaded the version 0.1 but unfortunately, your plugin doesn’t seem to work… Did you update the file with the modifications you indicate in this thread or is the file an old one?
Let me know, your plugin is the one I was looking for for a long time, I really need it to work ??
still not quite right for 2.7. however my live site is now 2.7 so I need to get on the updates asap.
see how the development version works for you in the meantime
Hi there
This is exactly what I need as well – how is it going? It looks to be nearly there for 2.7 as I see a folder option but it only shows the default folder.
I was really surprised to find this doesn’t come as a standard option with WordPress – but who am I, as a clueless user, to complain?!
I am much in awe of all of you who work on open source software for our benefit. Hope you have a breakthrough soon!
All the best
Lynnei’ve got a bit distracted of late cos our hosts have decided to throttle our service dramatically. but I *will* be getting on this soon. promise.
did you try the development version?
- The topic ‘Plugin relocate-upload – Not quite working in 2.7’ is closed to new replies.