Tuesday, March 31, 2015

Programmatically setting Featured Image for Post using an image on a remote server

In one of my applications we have built a small module that allows other applications to programmatically add posts in our CMS site that is powered by Wordpress. One of the things I got a bit stuck in was setting the featured image of the post. The image resided in a S3 bucket so I basically had to download it to the WordPress server, upload it to the media directory and then set the featured image option of the post to this newly uploaded image.

The media_sideload_image function is what i ended up using after a lot of googling for options.



/**
$url : The full url of the image starting with the protocol.
$post_id : The post you want to add the featured image to.
*/
function add_image_to_post($url, $post_id){
 $desc = "Auto added";
 $image_url = '';
 // The method returns either a string or error

 $image = media_sideload_image($url, $post_id, $desc);
 if(!is_string($image)){
   // Something went wrong
  return;
 }
 // Getting the url of the image. The method returns html
 // code with the img tag.
 $doc = new DOMDocument();
 $doc->loadHTML($image);
 $imageTags = $doc->getElementsByTagName('img');
 foreach($imageTags as $tag) {
  $image_url = $tag->getAttribute('src');
 }
 // get the attachment id from the url so that we can add that to the post.
 $attach_id = fjarrett_get_attachment_id_by_url($image_url);
 // Adding the image as featured image
 update_post_meta($post_id, '_thumbnail_id', $attach_id);
}

The fjarrett_get_attachment_id_by_url is something I picked up from Frankie Jarrett's blog. It works really well.

Saturday, March 21, 2015

ASUS TF700T (Transformer) - Downgrading Stock ROM from 10.6.1.14.10 to 10.6.1.14.8

I use rooted Transformer tablets in a project and discovered that there is no way to root the tablets when they ship with the 10.6.1.14.10 build. The motochopper application works very well when you want to root these tablets with earlier build versions.  This document contains the steps i took to downgrade the build on my tablet.

Note:
This procedure will wipe out all data on the device so please make sure you take a backup of the important data.

Step 0: Check the device build number


This process has been written to support the following:

Android OS Version: 4.2.1
Build Number: 10.6.1.14.10



Check the Settings-> About section of this device to make sure the values are correct. Pay special attention to the ending value of the build number and the SKU or the country code (it is US in the screenshot above).

Step 1 : Download the v10.6.1.14.8 ROM from the ASUS website

ASUS makes the builds available on its website : http://support.asus.com in the downloads section. You will have to select your device and OS to see the available builds. Download the build that matches the SKU code of your device.

Unzip the zip file and you will end up with the zip file that you can actually upload on the device. In my case the zip file I ended up with was called : " US_epad-user-10.6.1.14.8.zip".


Step 2: uploading the rom image on the device.

I guess you can put the build onto the sdcard using any method you are familiar with. I use ADB to push the build on the device and then restart it.

punitraizada$ adb push US_epad-user-10.6.1.14.8.zip /sdcard/
1212 KB/s (512448524 bytes in 412.751s)
punitraizada$ adb reboot


Step 3 :   Installing the new ROM


Once the device restarts you will see a upgrade notification on the lower right of the screen where the time is displayed.




Tapping on the settings should open up the settings menu with the message “System update detected”. Select that option to continue.


Selecting the “System update detected” option should open up a dialog that shows the ROM information.



Selecting “OK” should start the process of  installing the new ROM.  You will have to click on OK to a couple of dialogs that warn about loosing data and having your device plugged into the charger.




The downgrading process takes about 5-6 minutes. The tablet will restart automatically and end up on the welcome - setup screen.




Congratulations... your have a brand new (almost) device now.