Site icon Hip-Hop Website Design and Development

Search for picture by class and set as featured picture bulk

I am new right here and I’ve zero data of coding so I hope you may assist me.

I migrated an previous web site to new wordpress one and used a plugins to set first picture of posts as featured picture. however featured picture of some posts for some cause i do not know will not be the primary picture of the put up. so i made a decision to repair this by coding.

All my first photographs of the posts has a category title which is "worpresspic". I would like that code seems in posts for photographs that has the category I discussed above and units that as put up’s featured picture regardless of the put up already has featured picture or not. i discovered this regex that appears work excellent at discovering the picture:

(<img(.*class=['"](.*)worpresspic(.*)['"].*)*/>)+

I discovered this code that search for first picture and units it as featured picture as effectively:

// Get URL of first picture in a put up
   operate catch_that_image() {
   international $put up, $posts;
   $first_img = '';
   ob_start();
   ob_end_clean();
   $output = preg_match_all('/<img.+src=['"]([^'"]+)['"].*>/i', $post->post_content,      $matches);
   $first_img = $matches [1] [0];

   // no picture discovered show default picture as an alternative
   if(empty($first_img)){
   $first_img = "/images/default.jpg";
   }
   return $first_img;
   }

and likewise the beneath code which appears has extra choices:

// Be aware that your theme should assist put up thumbnails for this operate to work. 
// In case you are getting an error strive including add_theme_support('post-thumbnails'); to your features. php file  
operate vp_get_thumb_url($textual content, $dimension){
    international $put up;
    $imageurl="";

    // Test to see which picture is about as "Featured Image"
    $featuredimg = get_post_thumbnail_id($post->ID);
    // Get supply for featured picture
    $img_src = wp_get_attachment_image_src($featuredimg, $dimension);
    // Set $imageurl to Featured Picture
    $imageurl=$img_src[0];

    // If there isn't any "Featured Image" set, transfer on and get the primary picture connected to the put up
    if (!$imageurl) {
        // Extract the thumbnail from the primary connected imaged
        $allimages =&get_children('post_type=attachment&post_mime_type=picture&post_parent=' . $post->ID );

        foreach ($allimages as $img){
            $img_src = wp_get_attachment_image_src($img->ID, $dimension);
            break;
        }
        // Set $imageurl to first connected picture
        $imageurl=$img_src[0];
    }

    // If there isn't any picture connected to the put up, search for something that appears like a picture and get that
    if (!$imageurl) {
        preg_match('/<s*img [^>]*srcs*=s*[""']?([^""'>]*)/i' ,  $textual content, $matches);
        $imageurl=$matches[1];
    }

    // If there is not any picture connected or inserted within the put up, search for a YouTube video
    if (!$imageurl){
        // search for conventional youtube.com url from handle bar
        preg_match("/([a-zA-Z0-9-_]+.|)youtube.com/watch(?v=|/v/)([a-zA-Z0-9-_]{11})([^<s]*)/", $textual content, $matches2);
        $youtubeurl = $matches2[0];
        $videokey = $matches2[3];
    if (!$youtubeurl) {
        // search for youtu.be 'embed' url
        preg_match("/([a-zA-Z0-9-_]+.|)youtu.be/([a-zA-Z0-9-_]{11})([^<s]*)/", $textual content, $matches2);
        $youtubeurl = $matches2[0];
        $videokey = $matches2[2];
    }
    if ($youtubeurl)
        // Get the thumbnail YouTube routinely generates
        // '0' is the largest model, use 1 2 or 3 for smaller variations
        $imageurl = "http://i.ytimg.com/vi/{$videokey}/0.jpg";
    }

    // Spit out the picture path
    return $imageurl;
}

However as I stated I’ve no coding data and do not know the way to edit these codes to match my wants. For instance this codes verify if the put up has featured picture or not and if the put up has featured picture nothing occurs, however i do not need this.

Additionally I wish to run it in wordpress and with out have to open every put up and save, all featured photographs of the posts being up to date. the place to place the code? what php code I would like so as to add and the place am i able to put the ultimate code? (as i do know it have to be features.php am i proper?)

Thanks on your assist.