Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $photo = 'http://zewnetrzny_adres_z_obrazkie.pl/obrazke.jpg';
- $wp_filetype = wp_check_filetype(basename($photo), null );
- if ( ! function_exists( 'wp_handle_upload' ) ) require_once( ABSPATH . 'wp-admin/includes/file.php' );
- $overrides = array( 'test_form' => false);
- $file = wp_handle_upload($photo, $overrides);
- if ( $file ) {
- echo "File is valid, and was successfully uploaded.\n";
- var_dump( $file);
- } else {
- echo "Possible file upload attack!\n";
- }
- $wp_upload_dir = wp_upload_dir();
- $attachment = array(
- 'guid' => $wp_upload_dir['path'] . '/' . basename($photo),
- 'post_mime_type' => $wp_filetype['type'],
- 'post_title' => preg_replace('/\.[^.]+$/', '', $name),
- 'post_content' => '',
- 'post_status' => 'inherit'
- );
- $attach_id = wp_insert_attachment( $attachment, $photo );
- require_once(ABSPATH . 'wp-admin/includes/image.php');
- $attach_data = wp_generate_attachment_metadata( $attach_id, $photo );
- wp_update_attachment_metadata( $attach_id, $attach_data );
- add_post_meta($post_id, '_thumbnail_id', $attach_id, true);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement