Search
 
SCRIPT & CODE EXAMPLE
 

PHP

upload file in wp from url

//first, get the image, and store it in your upload-directory:
$uploaddir = wp_upload_dir();
$uploadfile = $uploaddir['path'] . '/' . $filename;

$contents= file_get_contents('http://mydomain.com/folder/image.jpg');
$savefile = fopen($uploadfile, 'w');
fwrite($savefile, $contents);
fclose($savefile);
//after that, we can insert the image into the media library:
$wp_filetype = wp_check_filetype(basename($filename), null );

$attachment = array(
    'post_mime_type' => $wp_filetype['type'],
    'post_title' => $filename,
    'post_content' => '',
    'post_status' => 'inherit'
);

$attach_id = wp_insert_attachment( $attachment, $uploadfile );

$imagenew = get_post( $attach_id );
$fullsizepath = get_attached_file( $imagenew->ID );
$attach_data = wp_generate_attachment_metadata( $attach_id, $fullsizepath );
wp_update_attachment_metadata( $attach_id, $attach_data );
Comment

PREVIOUS NEXT
Code Example
Php :: get env app url laravel 
Php :: using js variable in php 
Php :: current timestamp carbon 
Php :: php check if session is running 
Php :: php get user ip address 
Php :: get price woocommerce product 
Php :: get post order by meta value int 
Php :: get_declared_classes 
Php :: running laravel as host 
Php :: javascript php variable 
Php :: cut string in php 
Php :: laravel mix disable notifications 
Php :: add zeros in front of number php 
Php :: php temp directory 
Php :: SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes 
Php :: how to echo line number in php 
Php :: apache htaccess read from /public 
Php :: select in php mysql 
Php :: how to get the link of the current page in php 
Php :: alert php 
Php :: clear file contents php 
Php :: laravel validation integer 
Php :: php how to count array 
Php :: for loop php increment by 2 
Php :: laravel pluralization text 
Php :: shuffle php function 
Php :: get host from url php 
Php :: how to fetch all defined constant in php 
Php :: php switch 2 variables 
Php :: moodle webservice create user phone2 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =