Ok I got it! Here is the code I used:
add_action('publish_post', 'custom_auto_featured_image_publish_post',10,2);
function custom_auto_featured_image_publish_post($post_id, $post) {
// get category of post ID
$cat_detail=get_the_category($post_id);
$cat_name = array();
foreach($cat_detail as $cd){
$cat_name[] = $cd->cat_name;
}
$custom_fields = get_post_custom($post_id);
if(isset($custom_fields['original_guid']) && !empty($custom_fields['original_guid'])){
$htmlURL = $custom_fields['original_guid'][0];
// try to load the webpage
$dom = new domDocument;
$imageURL = '';
@$dom->loadHTML(file_get_contents($htmlURL));
if ( in_array( 'Mongolia.GoGo.mn', $cat_name ) ) {
$finder = new DomXPath($dom);
$classname="newscover";
$content = $finder->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $classname ')]");
if($content->length){
if($content->item(0)->childNodes->length > 1){
$imageURL = $content->item(0)->childNodes->item(1)->getAttribute('src');
}
}else{
$content = $dom->getElementById('ncbubuhome');
if($content->childNodes->length){
foreach ($content->childNodes as $node) {
if($node->nodeName != '#text'){
foreach ($node->childNodes as $childNode) {
if($childNode->nodeName == 'img'){
$imageURL = $childNode->getAttribute('src');
break;
}
}
}
}
}
}
}elseif ( in_array( 'InfoMongolia', $cat_name ) ) {
$finder = new DomXPath($dom);
$classname="full_text";
$content = $finder->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $classname ')]");
if($content->length){
if($content->item(0)->childNodes->length){
foreach ($content->item(0)->childNodes as $node) {
if($node->nodeName == 'img'){
$imageURL = $node->getAttribute('src');
break;
}
}
}
}
if($imageURL != ''){
if (filter_var($imageURL, FILTER_VALIDATE_URL) === false) {
$domain = 'http://www.infomongolia.com/';
$imageURL = str_replace('../', '', $imageURL);
$imageURL = $domain . $imageURL;
}
}
} elseif ( in_array( 'UBPost', $cat_name ) ) {
$finder = new DomXPath($dom);
$classname="full_text";
$content = $finder->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $classname ')]");
if($content->length){
if($content->item(0)->childNodes->length){
foreach ($content->item(0)->childNodes as $node) {
if($node->nodeName == 'img'){
$imageURL = $node->getAttribute('src');
break;
}
}
}
}
if($imageURL != ''){
if (filter_var($imageURL, FILTER_VALIDATE_URL) === false) {
$domain = 'http://www.infomongolia.com/';
$imageURL = str_replace('../', '', $imageURL);
$imageURL = $domain . $imageURL;
}
}
}
if($imageURL != ''){
$imageURL = str_replace(' ', '%20', $imageURL);
// download image from url
$tmp = download_url($imageURL);
$ext = pathinfo(basename($imageURL), PATHINFO_EXTENSION);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$name = strtotime("now") . '_feature_image.' . $ext;
$type = finfo_file($finfo, $tmp);
$file = array(
'name' => $name,
'size' => filesize($tmp),
'type' => $type,
'tmp_name' => $tmp,
'error' => UPLOAD_ERR_OK
);
$overrides = array(
'test_form' => false,
'test_size' => true,
'test_upload' => true,
);
// upload image to server
$file_uploaded = wp_handle_sideload( $file, $overrides );
// $filename should be the path to a file in the upload directory.
$filename = $file_uploaded['file'];
$filetype = $file_uploaded['type'];
// Prepare an array of post data for the attachment.
$attachment = array(
'guid' => $file_uploaded['url'],
'post_mime_type' => $filetype,
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit'
);
// Insert the attachment.
$attach_id = wp_insert_attachment( $attachment, $filename );
// Generate the metadata for the attachment, and update the database record.
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
// set the featured image
update_post_meta($post_id, '_thumbnail_id', $attach_id);
}
}
}