function rksnwp_add_attachment($postID, $thefile, $thefileurl)
{
    // Generate attachments and thumbnails
    $filename = $thefile;
    $attach_id = null;
    $existing_attachment = rksnwp_attachment_exists($filename);
    $attach_id = $existing_attachment['id'];
    return $attach_id;
}
function rksnwp_post_add_featured_image($post, $tags)
{
    $content = $post['post_content'];
    // IF FEATURED IMAGE IDENTIFIED
    $featured_url = '';
    $featured_pattern = "/\\[featured\\].*?src=\"(?P<url>.*?)\".*?\\[\\/featured]/ims";
    preg_match_all($featured_pattern, $content, $output_array);
    if (!empty($output_array['url'])) {
        $featured_url = $output_array['url'][0];
    }
    $content = preg_replace($featured_pattern, '', $content);
    $content = mb_str_replace('<p></p>', '', $content);
    // FEATURED IMAGES GET REMOVED AUTOMATICALLY - THEMES MAY BRING IT BACK
    // $content = mb_str_replace($featured_url,'',$content);
    $post['post_content'] = trim($content);
    $attachment = rksnwp_attachment_exists($featured_url);
    if (!empty($attachment)) {
        set_post_thumbnail($post['ID'], $attachment->id);
    }
    // Update the post
    wp_update_post($post);
}