Extract image from content and set it as the featured image

The problem:
I’m posting from the WordPress iPhone app. When adding an image, the image shows up in the description itself, which I don’t like.

What I’m trying to do:
Remove the image from the description and do one of the following (don’t know the benefits of one option over the other):

  1. Get the image source from the removed image and add it to a custom field. -OR-
  2. Set the image as the featured image

I figure either one of those options would help me to isolate the image URL for use in my post templates.

What I’ve tried:
Literally everything under the sun. Right now I’m adding code to functions.php but would eventually like to make this a plugin.

I think the code below is way off, but I want to post it to show some of the attempts I’ve made:

I’ve added an action to run a function mpb_remove_post_image on content_save_pre …

add_action('content_save_pre', 'mpb_remove_post_image');

Problem with that is I don’t have access to the post_id (which I need in order to set the featured image) using content_save_pre so I have to get the image url and look for a post with the guid equal to the image src…

function mpb_remove_post_image( $content ){

    // strip slashes, I guess we need to readd slashes when we're done?
    $content = stripslashes($content);

    // get the first image src
    $image_src = get_first_image($content);

    // if there's an image src we can jam
    if(!is_null($image_src)){

        global $wpdb;

        // query the db for an attachment with this image
        //$thumb_id = $wpdb->get_var($wpdb->prepare("SELECT DISTINCT ID FROM $wpdb->posts WHERE guid='$image_src'"));
        $result = $wpdb->get_results("SELECT ID, post_parent FROM {$wpdb->posts} WHERE guid ='$image_src'");

        $thumb_id = $result[0]->ID;         
        $post_id = $result[0]->post_parent;

        // set the featured image
        mpb_set_featured_image($thumb_id,$post_id);
        // update_post_meta( $post_id, '_thumbnail_id', $thumb_id );

    }

    // remove any images in the content
    $content = preg_replace("/<img[^>]+\>/i", "", $content);

    $content = addslashes($content);

    return $content;
}

the code above references this function I wrote to get the image…

function get_first_image($html){

    require_once('simple_html_dom.php');

    $post_dom = str_get_dom($html);

    // get the first image
    $first_img = $post_dom->find('img', 0);   

    // if any images then return the src attribute
    if($first_img !== null) {
        return $first_img->src;
    }

    return null;
}

And to set the featured image …

// set the featured image
function mpb_set_featured_image( $thumb_id, $post_id ){

    update_post_meta( $post_id, '_thumbnail_id', $thumb_id );

}

I’ve also tried to add an action for save_post, but anything I do inside the function that runs seems to trigger another hook that runs save_post again so it turns into an endless loop.

Does anyone know (even if it’s at a high level) what steps I can take to accomplish this? To recap here’s what I want to do…

On inserting a post 1) get the first image src 2) set it as a custom field or set it as a featured image 3) remove the image from the post.

edit – I should mention the code I posted does work if you add the post through the wp admin. But not if you add it via the mobile app.

2 s
2

Why not utilize two hooks, one before post saved and one after post saved?

content_save_pre : Function attached with this hook will remove image from content and store it in session/transient.

save_post : With this hook you will have id of post. Function attached with this hook will set featured image for the post with that id and delete session/transient data.

Alternatively, If you want to go with your approach, I think you should use post_name, instead of guid, to retrieve post id from database.

Leave a Comment