currently i'm trying to upload a Image to a Wordpress-Multisite at server side.
(The image is already on another path at the same server)
I've written a small PHP-script which should handle this:
$upload_dir = wp_upload_dir();
var_dump( $upload_dir);
$filename = basename($imagepath);
if (wp_mkdir_p($upload_dir['path'])) {
$file = $upload_dir['path'].'/'.$filename;
} else {
$file = $upload_dir['basedir'].'/'.$filename;
}
copy($imagepath, $file);
$wp_filetype = wp_check_filetype($filename, null);
$attachment = array(
'guid' => $upload_dir['url'].'/'.basename($filename),
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name($filename),
'post_content' => '',
'post_status' => 'inherit', );
$attach_id = wp_insert_attachment($attachment, $file, $postid);
Everything works fine, but the image is stored in the wrong folder.
wp_upload_dir(); returns the common path /wp-content/uploads/2016/03, not the path for the specific subsite, wich will be /wp-content/uploads/sites/SITE_ID/2016/03
Later when WP uses the image, the url of the image is set to http://example.com/wp-content/uploads/sites/SITE_ID/2016/03/IMAGE.JPG (which is not correct...)
Uploading a file with the built-in media-upload tool from wordpress works correct (files are stored in /wp-content/uploads/sites/SITE_ID/2016/03)
You see, wp_upload_dir() is not working correct..
Thanks..
i solved it!
For everyone with the same problem, the solution is simple:
When you're on multisite, register a filter for "upload_dir"
define a function, where you change temporarily the paths and urls
That's it!
if(is_multisite()){
add_filter('upload_dir', 'fix_upload_paths');
}
function fix_upload_paths($data)
{
$data['basedir'] = $data['basedir'].'/sites/'.get_current_blog_id();
$data['path'] = $data['basedir'].$data['subdir'];
$data['baseurl'] = $data['baseurl'].'/sites/'.get_current_blog_id();
$data['url'] = $data['baseurl'].$data['subdir'];
return $data;
}
Hope someone can use it. Hours of hours for me.
A simpler method is to use the switch_to_blog() function
if( is_multisite() ) {
switch_to_blog( get_current_blog_id() );
$imgdir = wp_upload_dir();
// do more process related to current blog
// close the swicth
restore_current_blog();
}
See details at https://developer.wordpress.org/reference/functions/switch_to_blog/
Related
I'm trying to write a function to rename the attachment file, both on upload, but also after upload.
I've written a nice function that can do this, but it results in missing thumbnails in the WP dashboard. I suspect it's because the GUID becomes wrong, but WP has made it impossible to change that GUID now (I think), so I'm not sure what else to do. Hoping someone here can help.
This is what I have, based on everything I could find online.
add_action("add_attachment", "rename_attachment", 10, 1);
function rename_attachment($post_id) {
custom_attachment_rename($post_id, "My name filename here");
}
function custom_attachment_rename( $post_id, $new_filename = 'new filename' ){
// Get path info of orginal file
$og_path = get_attached_file($post_id);
$path_info = pathinfo($og_path);
// Santize filename
$safe_filename = wp_unique_filename($path_info['dirname'], $new_filename);
// Build out path to new file
$new_path = $path_info['dirname']. "/" . $safe_filename . "." .$path_info['extension'];
// Rename the file and update it's location in WP
rename($og_path, $new_path);
update_attached_file( $post_id, $new_path );
// URL to the new file
$new_url = wp_get_attachment_url($post_id);
// Update attachment data
$id = wp_update_post([
'ID' => $post_id,
'post_title' => $new_filename,
'guid' => $new_url // Doesn't seem to work
]);
// Try this to reset the GUID for the attachment. Doesn't seem to actually reset it.
// global $wpdb;
// $result = $wpdb->update($wpdb->posts, ['guid' => $new_url], ['ID' => $post_id]);
// Update all links to old "sizes" files, or create it for new upload.
$metadata = get_post_meta($post_id, '_wp_attachment_metadata', true);
if( empty($metadata) ) {
// New upload.
$data = wp_generate_attachment_metadata($post_id, $new_path);
//update_post_meta($post_id, '_wp_attachment_metadata', $data); // Tried this. Doesn't work.
wp_update_attachment_metadata($post_id, $data); // Also doesn't work
} else {
// Regenerating an existing image
// TODO loop through $metadata and update the filename and resave? Maybe just delete it and regenerate instead?
}
// TODO Update use of the old filename in post_content throughout site
}
These have been the helpful posts I've gone over so far.
https://wordpress.stackexchange.com/questions/166943/how-to-rename-an-image-attachment-filename-using-php
https://wordpress.stackexchange.com/questions/30313/change-attachment-filename
https://wordpress.stackexchange.com/a/221254/4562
The weird thing is, if I die at the end of this function, then it works. So I suspect something else in WP is overwriting the _wp_attachment_metadata for this attachment. That is why I suspect the GUID is the issue. Something else is looking up the attachment via GUID and find a URL to a file that no longer exists (as I changed the filename) and running wp_generate_attachment_metadata on a bad file path. That is my hunch.
I don't have any other plugins installed.
The reason your code doesn't work is not related to the GUID.
It is because, in WP core, the function wp_update_attachment_metadata() is called with the original file name at the end of the media upload request handling. And the hook add_attachment is called inside wp_insert_attachment() function.
function media_handle_upload( $file_id, $post_id, $post_data = array(), $overrides = array( 'test_form' => false ) ) {
... ...
// Save the data.
$attachment_id = wp_insert_attachment( $attachment, $file, $post_id, true );
if ( ! is_wp_error( $attachment_id ) ) {
// Set a custom header with the attachment_id.
// Used by the browser/client to resume creating image sub-sizes after a PHP fatal error.
if ( ! headers_sent() ) {
header( 'X-WP-Upload-Attachment-ID: ' . $attachment_id );
}
// The image sub-sizes are created during wp_generate_attachment_metadata().
// This is generally slow and may cause timeouts or out of memory errors.
wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );
}
return $attachment_id;
}
We can use the "wp_update_attachment_metadata" filter to change the metadata with the new file name.
Please check the below code. I tested and it is working well.
class CustomAttachmentRename {
public $new_filename = 'custom file';
private $new_path;
function __construct () {
add_action("add_attachment", array($this, "rename_attachment"), 10, 1);
}
function rename_attachment($post_id) {
// Get path info of orginal file
$og_path = get_attached_file($post_id);
$path_info = pathinfo($og_path);
// Santize filename
$safe_filename = wp_unique_filename($path_info['dirname'], $this->new_filename);
// Build out path to new file
$this->new_path = $path_info['dirname']. "/" . $safe_filename . "." .$path_info['extension'];
// Rename the file and update it's location in WP
rename($og_path, $this->new_path);
update_attached_file( $post_id, $this->new_path );
// Register filter to update metadata.
add_filter('wp_update_attachment_metadata', array($this, 'custom_update_attachment_metadata'), 10, 2);
}
function custom_update_attachment_metadata($data, $post_id) {
return wp_generate_attachment_metadata($post_id, $this->new_path);
}
}
$customAttachmentRename = new CustomAttachmentRename();
$customAttachmentRename->new_filename = "test image" . time();
I turned #chengmin answer into a single function like this:
/**
* Renames a file. Will also regenerate all the thumbnails that go with the file.
* #SEE https://stackoverflow.com/questions/64990515/wordpress-rename-attachment-file-with-working-thumbnails
*
* #param string $post_id The WordPress post_id for the attachment
* #param string $new_file_name The filename (without extension) that you want to rename to
*/
function attachment_rename($post_id, $filename) {
// Get path info of orginal file
$og_url = wp_get_attachment_url($post_id);
$og_path = get_attached_file($post_id);
$path_info = pathinfo($og_path);
$og_meta = get_post_meta($post_id, '_wp_attachment_metadata', true);
// Santize filename
$safe_filename = wp_unique_filename($path_info['dirname'], $filename);
// Build out path to new file
$new_path = $path_info['dirname']. "/" . $safe_filename . "." .$path_info['extension'];
// Rename the file in the file system
rename($og_path, $new_path);
// Delete old image sizes if we have them
if( !empty($og_meta) ) {
delete_attachment_files($post_id);
}
// Now save new path to file in WP
update_attached_file( $post_id, $new_path );
// Register filter to update metadata
$new_data = wp_generate_attachment_metadata($post_id, $new_path);
return add_filter('wp_update_attachment_metadata', function($data, $post_id) use ($new_data) {
return $new_data;
}, 10, 2);
}
/**
* Delete all old image files, if they aren't used post_content anywhere.
* The dont-delete check isn't perfect, it will give a lot of false positives (keeping more files than it should), but it's best I could come up with.
* #SEE https://github.com/WordPress/WordPress/blob/f4cda1b62ffca52115e4b04d9d75047060d69e68/wp-includes/post.php#L5983
*
* #param string $post_id The WordPress post_id for the attachment
*/
function delete_attachment_files($post_id) {
$meta = wp_get_attachment_metadata( $post_id );
$backup_sizes = get_post_meta( $post_id, '_wp_attachment_backup_sizes', true );
$file = get_attached_file( $post_id );
$url = wp_get_attachment_url($post_id);
// Remove og image so it doesn't get deleted in wp_delete_attachment_files()
$meta['original_image'] = "";
// Check if image is used in a post somehwere
$url_without_extension = substr($url, 0 , (strrpos($url, ".")));
$args = [
"s" => $url_without_extension,
"posts_per_page" => 1,
"post_type" => "any"
];
$found = get_posts($args);
if( empty($found) ) {
return wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file );
}
return false;
}
We have created a custom post type which allows our client to upload files to a folder outside of the standard Wordpress uploads folder (wp-content/upload-assets). These files are to be handled seperately from the standard wp-content/uploads folder and this is why we CANNOT use
define( 'UPLOADS', 'mycustomfolder' );
in the wp-config.php.
Instead we use this to temporarily change the uploads folder to wp-content/upload-assets:
add_filter('upload_dir', 'my_upload_dir');
$uploaded_file = wp_handle_upload($_FILES['xxxx_image'], $upload_overrides);
remove_filter('upload_dir', 'my_upload_dir');
We are using this to remove all attachments from a particular post:
add_filter('upload_dir', 'my_upload_dir');
$attachments = get_posts( array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_status' => 'any',
'post_parent' => $pid
) );
foreach ( $attachments as $attachment ) {
if ( false === wp_delete_attachment( $attachment->ID, true ) ) {
echo 'Attachment could not be deleted.';
}
}
remove_filter('upload_dir', 'my_upload_dir');
wp_delete_attachment should also delete all associated files from disk but it doesn't work because our files are in our custom folder (wp-content/upload-assets).
Here's the code for our my_upload_dir function:
function my_upload_dir($upload) {
$upload['subdir'] = '';
$upload['basedir'] = WP_CONTENT_DIR;
$upload['baseurl'] = WP_CONTENT_URL;
$upload['path'] = $upload['basedir'] . '/upload-assets';
$upload['url'] = $upload['baseurl'] . '/upload-assets';
return $upload;
}
How do we make wp_delete_attachment remove the files in our custom wp-content/upload-assets folder?
Hi You can do this way if want to delete the file, but it will not remove all post meta fields, taxonomy, comments, etc. associated with the attachment.
Hope this help
foreach ( $attachments as $attachment ) {
if ( false === wp_delete_attachment( $attachment->ID, true ) ) {
$file = get_attached_file( $attachment->ID );
$file = str_replace( "uploads", "upload-assets", $file);
wp_delete_file( $file );
}
}
I have not tested this code, but i hope it should work
Staying in the domain of current question you can use "get_attached_file" filter to directly alter the file path string used to grab the path of file to be deleted. Specifically add this function to your functions.php
function sr_update_uploads_directory( $file )
{
return str_replace( "uploads", "upload-assets", $file) // Change path to upload-assets from uploads
}
add_filter( 'get_attached_file', 'sr_update_uploads_directory' );
Don't have time for testing currently. So, please excuse me for that. Hope it helps. :)
For some reason, this code I've been working with since the beginning seems to have stopped working on my dev environment which is set up on a free hosting service. However, it still works locally. Now, I can't recall if I've ever actually tested it on the dev environment so maybe it never worked there to begin with.
Basically, I'm using Gravity Forms to create custom posts (works just fine) and am also using multiple file uploads to add image attachments to the post. Since that's not entirely supported by GF yet I fiddled around and managed to create this little function here:
function create_post_attachment_from_gf($f, $from_url) {
//for image field, it sends content with a bunch of other parameters in a string with pipe delimiters
$split_image = explode('|', $from_url);
if( count($split_image) > 0 )
$from_url = $split_image[0];
if( !empty( $_FILES[$f]['name'] ) ) {
$file = $_FILES[$f];
$uploads = wp_upload_dir( null );
$filename = wp_unique_filename( $uploads['path'], $file['name'], null );
// Strip the query strings.
$filename = str_replace('?','-', $filename);
$filename = str_replace('&','-', $filename);
// Compute the URL
$url = $uploads['url'] . "/$filename";
$new_file = $uploads['path'] . "/$filename";
copy($from_url,$new_file);
// Move the file to the uploads dir
$stat = stat( dirname( $new_file ));
$perms = $stat['mode'] & 0000666;
# chmod( $new_file, $perms );
return array( 'file' => $new_file, 'url' => $url, 'type' => $file['type'], 'name' => $file['name'] );
}
return false;
}
And then call it this way to create the attachment:
$copied_file = create_post_attachment_from_gf('input_' . $file_uploads[$i], $entry[$file_uploads[$i]]);
$subdir = wp_upload_dir( null )["subdir"];
if( $copied_file ){
$attachment = array(
'guid' => $copied_file['url'],
'post_mime_type' => $copied_file['type'],
'post_title' => $copied_file['name'],
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $subdir . '/' .$copied_file['name'], $post_id );
$attach_data = wp_generate_attachment_metadata( $attach_id, $copied_file['file'] );
wp_update_attachment_metadata( $attach_id, $attach_data );
}
It looks like the copy is what fails here. It creates an empty file in my uploads folder with the right filename. I checked my folder permissions and even went ahead and set everything under uploads (including the gravity forms folders) to 777 and it still doesn't work for some reason.
Both my local and remote websites are running on WP 3.8.1 and all plugins are up to date on both. The databases are pretty much exactly the same aside from some test data which shouldn't interfere with this. Adding an attachment directly from the dashboard works fine. Is this something I should take up with the hosting company? Or is my code faulty? Or maybe there's a way to create an attachment without moving the file - that'd be fine with me.
Any help is appreciated.
Well, it looks like, while the copying still doesn't work remotely, I could've been uploading the image directly to the right folder from the get go.
//Have gform upload directly in the upload folder
add_filter("gform_upload_path", "change_upload_path", 10, 2);
function change_upload_path($path_info, $form_id){
$uploads = wp_upload_dir( null );
$path_info["path"] = $uploads['path'] . "/";
$path_info["url"] = $uploads['url'] . "/";
return $path_info;
}
So I removed the part that moved the file over and recreated the filename from my previous function and everything works as planned now. Can't believe I spent a few hours on this... Hopefully this helps someone else?
I am trying to post to a page and do some wordpress insertion in post but somehow the redirect is not working and after that. I tried following three options:
wp_redirect($location);
wp_safe_redirect($location);
header('Location:http://example.com');
here is what I want to do:
posting to another page.
look for file to be uploaded and upload directory
Upload the file.
Set post thumbnail using set_post_thumbnail
redirect to desired page.
the script work till setting thumbnail and then dies.
If anyone want to use it to upload thumbnail from template front end, they can use it.
// if you have this in a file you will need to load "wp-load.php" to get access to WP functions. If you post to "self" with this code then WordPress is by default loaded
require $_SERVER['DOCUMENT_ROOT'] . "/wp-load.php";
// require two files that are included in the wp-admin but not on the front end. These give you access to some special functions below.
require $_SERVER['DOCUMENT_ROOT'] . "/wp-admin/includes/file.php";
require $_SERVER['DOCUMENT_ROOT'] . "/wp-admin/includes/image.php";
// required for wp_handle_upload() to upload the file
$upload_overrides = array( 'test_form' => FALSE );
global $current_user;
get_currentuserinfo();
$logged_in_user = $current_user->ID;
//get user POST information
global $post;
$loop = new WP_Query( array(
'posts_per_page' => 1,
'post_type' => "vet-profile",
'order' => "ASC",
'orderby' => "menu_order",
'author'=>"$logged_in_user"
)
);
while ( $loop->have_posts() ): $loop->the_post();
$current_postID = $post->ID;
$current_posttitle = get_the_title();
endwhile;
// count how many files were uploaded
$upload_files = $_FILES[ 'upload_files' ];
// load up a variable with the upload direcotry
$uploads = wp_upload_dir();
// foreach file uploaded do the upload
//foreach ( range( 0, $count_files ) as $i )
//{
// create an array of the $_FILES for each file
$file_array = array(
'name' => $_FILES['upload_files']['name'],
'type' => $_FILES['upload_files']['type'],
'tmp_name' => $_FILES['upload_files']['tmp_name'],
'error' => $_FILES['upload_files']['error'],
'size' => $_FILES['upload_files']['size'],
);
// check to see if the file name is not empty
if ( !empty( $file_array['name'] ) )
{ ?>
<?php
// upload the file to the server
$uploaded_file = wp_handle_upload( $upload_files, $upload_overrides );
if ( $uploaded_file )
{
echo '<script>alert("Image successfully uploaded.\n");</script>';
}
else
{
echo '<script>alert("Fish! some error occured. Please try again.");</script>';
}
// checks the file type and stores in in a variable
$wp_filetype = wp_check_filetype( basename( $uploaded_file['file'] ), null );
// set up the array of arguments for "wp_insert_post();"
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename( $uploaded_file['file'] ) ),
'post_content' => '',
'post_author' => $logged_in_user,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_parent' => $current_postID,
'guid' => $uploads['url'] . '/' . $file_array['name']
);
// insert the attachment post type and get the ID
$attachment_id = wp_insert_attachment( $attachment, $uploaded_file['file'], $current_postID );
// generate the attachment metadata
$attach_data = wp_generate_attachment_metadata( $attachment_id, $uploaded_file['file'] );
// update the attachment metadata
wp_update_attachment_metadata( $attachment_id, $attach_data);
// set thumbnail to the current post
set_post_thumbnail( $current_postID, $attachment_id );
echo '<script>alert("set thumbnail")';
$location = empty($_POST['redirect_to']) ? get_redirect_link() : $_POST['redirect_to'];
wp_safe_redirect( $location );
echo '<script>alert("End Loop");</script>';
}
Thanks in Advance
I've found that the only place I can reliably call wp_redirect is in a callback function to the init action hook (http://codex.wordpress.org/Plugin_API/Action_Reference/init) as it clearly states in the documentation:
Runs after WordPress has finished loading but before any headers are
sent. Useful for intercepting $_GET or $_POST triggers.
You might want to look into this. The other thing you might want to try, if this doesn't work for you, is using output buffering. Here's a neat tutorial on how to do that:
http://tommcfarlin.com/wp_redirect-headers-already-sent/
echo '<script>alert("set thumbnail")';
$location = empty($_POST['redirect_to']) ? get_redirect_link() : $_POST['redirect_to'];
wp_safe_redirect( $location );
echo '<script>alert("End Loop");</script>';
The above code will most surely not work. You can't redirect after you've written something to the output. I didn't check the rest of the code, but if it works as you say, removing the top and bottom row (or just the top row even) should make it all work:
$location = empty($_POST['redirect_to']) ? get_redirect_link() : $_POST['redirect_to'];
wp_safe_redirect( $location );
(If you enable debug mode in wp-config.php you should see an error message instead of just a white screen. So if this doesn't work, you can copy the error over here and we can know exactly what the problem is.)
I'm trying to add a bunch of posts programmatically, and then add their attendant images. I have an array with all the image filenames in it, and I've been able to add them to the database, but I can't seem to get the proper thumbnails created.
The posts are coming from a csv located in wp-content/uploads/dirname/. They have numbers in their filenames which correspond to an ID in the CSV, which is how I know what images need to be added to what post id.
I've gotten the wp_insert_attachment() part to work with the images right there in their own little directory, but I couldn't get the thumbnails to generate. I installed the regenerate thumbnails plugin, and it was able to generate them, but I can't seem to get it to happen programmatically.
I thought that might be because wp_generate_attachment needs the photos to be in /uploads/2011/12/ (e.g.), so I started down the path of moving the images and then trying to add them. This makes sense anyway because I kinda want to make copies rather than have 5 or 6 different media sizes added to my wp-content/uploads/dirname/ dir.
Anyway, it doesn't work. Moving the images via PHP's copy doesn't work, and the thumbnails don't generate.
foreach ($frazerfiles[$stock_num] as $stock_img){
require_once(ABSPATH . 'wp-admin/includes/image.php');
echo "... ...Trying to add attachment metadata...";
// copy the file to the upload dir
$uploads = wp_upload_dir();
$file_to_move = ABSPATH."wp-content/uploads/".$stock_img;
if (copy($file_to_move, $uploads['path']."/")) {
echo "Moved $file_to_move to ".$uploads['path']."/";
$my_moved_file = $uploads['path']."/".$stock_img;
// I think this is failing because the images aren't in the upload dir.
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($stock_img)),
'post_content' => '',
'post_status' => 'inherit'
);
if ($attach_id = wp_insert_attachment( $attachment, $my_moved_file, $newpostid )) {
if ($attach_data = wp_generate_attachment_metadata( $attach_id, $my_moved_file)) {
echo "...success for $my_moved_file: ID:$attach_id<br />\n";
} else {
echo "...FAILED for $my_moved_file ID:$attach_id<br />\n";
print_r($attach_data);
}
} else { // inserting attachment failed
echo "Insert attachment failed for $my_moved_file to $newpostid<br />\n";
}
} else {
echo "Failed moving $file_to_move to ".$uploads['path']."/";
}
}// images foreach
After cross checking with WordPress core files, the only thing that seems to be missing out is wp_update_attachment_metadata immediately after wp_generate_attachment_metadata.
Try adding
wp_update_attachment_metadata($attach_id, $attach_data);
after
echo "...success for $my_moved_file: ID:$attach_id<br />\n";
So the if block looks like
if ($attach_data = wp_generate_attachment_metadata( $attach_id, $my_moved_file)) {
echo "...success for $my_moved_file: ID:$attach_id<br />\n";
wp_update_attachment_metadata($attach_id, $attach_data);
} else {
echo "...FAILED for $my_moved_file ID:$attach_id<br />\n";
print_r($attach_data);
}
Suggestion: (not related to the question)
move the line
require_once(ABSPATH . 'wp-admin/includes/image.php');
outside(above) the for loop.
UPDATE 1:
Adding suggestion to fix the copy issue.
You missed out the "check the file type" line.
(PHP copy function will fail if the destination file($my_moved_file) already exists)
Change this code
// copy the file to the upload dir
$uploads = wp_upload_dir();
$file_to_move = ABSPATH."wp-content/uploads/".$stock_img;
if (copy($file_to_move, $uploads['path']."/")) {
echo "Moved $file_to_move to ".$uploads['path']."/";
$my_moved_file = $uploads['path']."/".$stock_img;
// I think this is failing because the images aren't in the upload dir.
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($stock_img)),
'post_content' => '',
'post_status' => 'inherit'
);
TO
// copy the file to the upload dir
$uploads = wp_upload_dir();
$file_to_move = ABSPATH."wp-content/uploads/".$stock_img;
$my_moved_file = $uploads['path']."/".$stock_img;
if (copy($file_to_move, $my_moved_file)) {
echo "Moved $file_to_move to ".$my_moved_file;
// Check the file type
$wp_filetype = wp_check_filetype(basename($my_moved_file), null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($my_moved_file)),
'post_content' => '',
'post_status' => 'inherit'
);