How to upload wordpress post thumbnail manually [ wp_database ]

I’m trying to create an external php script outside of wordpress to manage posts in a custom backoffice (using Symfony2).

So far everything works fine. I just want to know how to set a post thumbnail manually by uploading a picture and registering its informations directly into WordPress database, means :

Read More
  • Which upload dir to use
  • Which post type
  • post status
  • The maining of postMime
  • Metas to add
  • …etc

In general I just want to know how to register a post thumbnail manually without using WordPress functions.

Any ideas ?

Thank you !

Related posts

Leave a Reply

1 comment

  1. To do this, you must create manually the postmeta _wp_attachment_metadata which is a serialized array.

    1. First you get your thumbnail:

    $file = $form->getData();
    $thumb = $file[‘logo’];

    1. you get your thumbnail characteristics (width, height…):

    list($width, $height, $type, $attr) = getimagesize($thumb);

    1. building the array from the thumb charactéristics:

      $meta_data_value = array();
      $meta_data_value[‘width’] = $width;
      $meta_data_value[‘height’] = $height;
      $meta_data_value[‘file’] = $slug.’.’.$ext;

      $sizes = array();
      $sizes[‘thumbnail’] = array(‘file’ => $slug.’.’.$ext,’width’ => 125,’height’ => 150,’mime-type’ => $mime);
      $sizes[‘medium’] = array(‘file’ => $slug.’.’.$ext,’width’ => 250,’height’ => 300,’mime-type’ => $mime);
      $meta_data_value[‘sizes’] = $sizes;
      $meta_data_value[‘image_meta’] = array(‘aperture’ => 0,’credit’ => ”,’camera’ => ”,’caption’ => ”,’created_timestamp’ => 0,’copyright’ => ”,’focal_length’ => 0,’iso’ => 0,’shutter_speed’ => 0,’title’ => ”);
      $meta_data_value = serialize($meta_data_value);

      1. Finally adding the postmeta with Doctrine :

        $meta_data = new PostMeta();
        $meta_data->setKey(‘_wp_attachment_metadata’);
        $meta_data->setValue($meta_data_value);
        $meta_data->setPost($logo);// supposing that you create the post $logo
        $em->persist($meta_data);
        $em->flush();