Advertisement
Guest User

Untitled

a guest
Apr 8th, 2013
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.15 KB | None | 0 0
  1. $photo = 'http://zewnetrzny_adres_z_obrazkie.pl/obrazke.jpg';                          
  2.  
  3. $wp_filetype = wp_check_filetype(basename($photo), null );
  4. if ( ! function_exists( 'wp_handle_upload' ) ) require_once( ABSPATH . 'wp-admin/includes/file.php' );
  5. $overrides = array( 'test_form' => false);
  6. $file = wp_handle_upload($photo, $overrides);
  7.  
  8. if ( $file ) {
  9.     echo "File is valid, and was successfully uploaded.\n";
  10.     var_dump( $file);
  11. } else {
  12.     echo "Possible file upload attack!\n";
  13. }                            
  14.                            
  15. $wp_upload_dir = wp_upload_dir();
  16. $attachment = array(
  17.     'guid' => $wp_upload_dir['path'] . '/' . basename($photo),
  18.         'post_mime_type' => $wp_filetype['type'],
  19.         'post_title' => preg_replace('/\.[^.]+$/', '', $name),
  20.         'post_content' => '',
  21.         'post_status' => 'inherit'
  22.  );
  23.  
  24. $attach_id = wp_insert_attachment( $attachment, $photo );
  25. require_once(ABSPATH . 'wp-admin/includes/image.php');
  26. $attach_data = wp_generate_attachment_metadata( $attach_id, $photo );
  27. wp_update_attachment_metadata( $attach_id, $attach_data );
  28. add_post_meta($post_id, '_thumbnail_id', $attach_id, true);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement