1

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 :

  • 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 !

Adil Ouchraa
  • 501
  • 3
  • 5
  • 18
  • 1
    Are you sure you wouldn't be better off using something like WordPress's XMLRPC interface for this? Throwing stuff directly into WordPress's database will be quite fragile, I'd have thought. http://stackoverflow.com/questions/12840797/how-can-i-add-a-thumbnail-to-a-wordpress-post-using-xmlrpclib-python-library – Matt Gibson Nov 28 '13 at 13:10
  • I agree, mixing frameworks unless it's absolutely necessary is terrible idea... – Jovan Perovic Nov 28 '13 at 13:20
  • I'm not doing everything in my way, I'm using https://github.com/kayue/KayueWordpressBundle which is a Symfony2 bundle to connect Sf to WP, But I cant find a complete documentation. I've already posted a question about this: http://stackoverflow.com/questions/20003509/where-can-i-find-a-complete-documentation-of-the-symfony2-bundle-kayuewordpressb – Adil Ouchraa Nov 28 '13 at 14:10

1 Answers1

2

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();