robertritz
Forum Replies Created
-
Forum: Hacks
In reply to: Programmatically set thumbnail via URL – IF ELSEIF issueHi there, I got some outside help and dramatically changed the code around. Here is my working code. Wanted to share for anyone interested! I uses xpath instead of getting the nth image. I’m not sure which one is better, but xpath is probably more predictable.
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 = 'https://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 = 'https://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); } } }
Forum: Hacks
In reply to: Programmatically set thumbnail via URL – IF ELSEIF issueThanks PhPCentre! I tried the first option as my post’s permalink is actually an external link so this works best. I put it in my functions.php and made a new post, edited the permalink, set the category, saved and then hit publish. It did take a few seconds longer so I’m thinking it tried something.
Is there something I can do to debug this? I’m using a different evaluator to determine if there is a thumbnail already. Also I pulled in the custom field meta as an array and am trying to select the specific custom field. Here is my current code. I’m really scratching my head over this and why it isn’t working…
<?php add_action('publish_post', 'auto_featured_image_publish_post',10,2); function auto_featured_image_publish_post($post_id, $post) { $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); $htmlURL = $custom_fields['original_guid']; // try to load the webpage $doc = new DOMDocument(); @$doc->loadHTMLFile($htmlURL); // get all image tags $images = $doc->getElementsByTagName('img'); if ( get_post_meta($post_id, '_apt_skip_post_thumb', true) ) { if ( in_array( 'Mongolia.GoGo.mn', $cat_name ) ) { $imageURL = $images->item(62)->getAttribute('src'); // download image from url $tmp = download_url($imageURL); $file = array( 'name' => basename($imageURL), 'tmp_name' => $tmp ); // create attachment from the uploaded image $attachment_id = media_handle_sideload( $file, $post_id ); // set the featured image update_post_meta($post_id, '_thumbnail_id', $attachment_id); } } }
Forum: Hacks
In reply to: Programmatically set thumbnail via URL – IF ELSEIF issueEDIT: Well it took a while but eventually errors showed up. I put the corresponding line of code next to the errors. From these errors it looks like this functions isn’t pulling the fields from the post properly? Here are the errors:
Warning: Missing argument 2 for auto_featured_image_publish_post() in /var/www/html/wordpress/wp-content/themes/point/functions.php on line 508 function auto_featured_image_publish_post($post, $post_id) { Notice: Trying to get property of non-object in /var/www/html/wordpress/wp-content/themes/point/functions.php on line 510 $category = $post->the_category; Notice: Undefined variable: post_id in /var/www/html/wordpress/wp-content/themes/point/functions.php on line 514 $post = get_post($post_id); Notice: Trying to get property of non-object in /var/www/html/wordpress/wp-content/themes/point/functions.php on line 515 $htmlURL = $post->original_guid; Warning: DOMDocument::loadHTMLFile(): Empty string supplied as input in /var/www/html/wordpress/wp-content/themes/point/functions.php on line 519 This is empty because the other variables are :) Notice: Undefined variable: post_id in /var/www/html/wordpress/wp-content/themes/point/functions.php on line 523 if ( has_post_thumbnail($post_id) == false ) {