You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Replace the content of add_to_media() function in admin/class-artist-image-generator-admin.php l.600 by this one :
publicfunctionadd_to_media(): mixed
{
require_onceABSPATH . "/wp-admin/includes/image.php";
require_onceABSPATH . "/wp-admin/includes/file.php";
require_onceABSPATH . "/wp-admin/includes/media.php";
$url = sanitize_url($_POST['url']);
$alt = sanitize_text_field($_POST['description']);
// Download url to a temp file$tmp = download_url($url);
if (is_wp_error($tmp)) {
returnfalse;
}
// Get the filename and extension ("photo.png" => "photo", "png")$filename = pathinfo($url, PATHINFO_FILENAME);
$extension = pathinfo($url, PATHINFO_EXTENSION);
// An extension is required or else WordPress will reject the uploadif (!$extension) {
// Look up mime type, example: "/photo.png" -> "image/png"$mime = mime_content_type($tmp);
$mime = is_string($mime) ? sanitize_mime_type($mime) : false;
// Only allow certain mime types because mime types do not always end in a valid extension (see the .doc example below)$mime_extensions = array(
'image/jpg' => 'jpg',
'image/jpeg' => 'jpeg',
'image/gif' => 'gif',
'image/png' => 'png'
);
$extension = $mime_extensions[$mime] ?? false;
if (!$extension) {
// Could not identify extension
@unlink($tmp);
returnfalse;
}
}
// Define the path of the third-party folder where you want to save files$upload_dir = ABSPATH . 'custom_images/'; // REPLACE BY YOUR PATH HERE// Maybe you should add uniqid() to avoid same filenames in the directory $file_name = sanitize_title($alt, $filename) . /*'_' . uniqid() .*/".$extension";
$target_file = $upload_dir . $file_name;
if (!rename($tmp, $target_file)) {
wp_send_json_error('Error renaming file');
}
@unlink($tmp);
// Success, return attachment ID (int). In this code, will return 0 anywaywp_send_json_success(['attachment_id' => 0]);
if (defined('DOING_AJAX') && DOING_AJAX) {
wp_die();
}
}
Note : make your own path by customizing $upload_dir var. This code will not store images into Wordpress Media Library. Feel free to adapt the code to your needs.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Original request by @CarloLupo.
Replace the content of add_to_media() function in admin/class-artist-image-generator-admin.php l.600 by this one :
Note : make your own path by customizing $upload_dir var. This code will not store images into Wordpress Media Library. Feel free to adapt the code to your needs.
Beta Was this translation helpful? Give feedback.
All reactions