Title: Add image to posts
Last modified: August 20, 2016

---

# Add image to posts

 *  [fabby](https://wordpress.org/support/users/fabby/)
 * (@fabby)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/add-image-to-posts/)
 * i want to add image for posts from functions.php when the theme is activate
    
   i use this code from wordpress doc:
 *     ```
       $filename=get_template_directory()."/n4p/resources/first page descriptions/support.jpg";
       $wp_filetype = wp_check_filetype(basename($filename), null );
       $wp_upload_dir = wp_upload_dir();
       $attachment = array(
           'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
           'post_mime_type' => $wp_filetype['type'],
           'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
           'post_content' => '',
           'post_status' => 'inherit'
       );
       $postIdObj=get_page_by_title( "Support", "", "post" );
       $postId=$postIdObj->ID;
       /*
       wp_insert_attachment( $attachment, $filename, $postId );
   
       $attach_id = wp_insert_attachment( $attachment, $filename, $postId );
       require_once(ABSPATH . 'wp-admin/includes/image.php');
       $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
       wp_update_attachment_metadata( $attach_id, $attach_data );
       ```
   
 * _[In future please wrap your code using backticks]_
 * the problem is that in the upload directory the image don’t exists.
    and when
   i enter on featured images i see 2 broked images and the url is something like
   this:
 * [http://localhost/bwp/wp-content/uploads/E:web](http://localhost/bwp/wp-content/uploads/E:web)
   designbwp/wp-content/themes/bestwordpressthemesforphotographers/n4p/resources/
   first page descriptions/support.jpg
 * so..can you help me with this?
    thanks!

The topic ‘Add image to posts’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 0 replies
 * 1 participant
 * Last reply from: [fabby](https://wordpress.org/support/users/fabby/)
 * Last activity: [13 years, 4 months ago](https://wordpress.org/support/topic/add-image-to-posts/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
