0
votes

Here's my situation:

I'm redesigning an existing Wordpress site.

The new design separates all images from the actual post content and puts it in a Featured Gallery.

Currently, each post has your typical inline images in the post content.

Is this even remotely possible to extract all inline images from the post content, and create Featured Galleries with those images for each post?

In the past I've done something like the below to grab the first image in the post content, and set it as the standard featured image, but nothing like what I have to do with this dilemma.

function wpforce_featured() {
      global $post;
      $already_has_thumb = has_post_thumbnail($post->ID);
          if (!$already_has_thumb)  {
          $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
                      if ($attached_image) {
                            foreach ($attached_image as $attachment_id => $attachment) {
                            set_post_thumbnail($post->ID, $attachment_id);
                            }
                       }
                    }
  }  //end function
  add_action('the_post', 'wpforce_featured');
  add_action('save_post', 'wpforce_featured');
  add_action('draft_to_publish', 'wpforce_featured');
  add_action('new_to_publish', 'wpforce_featured');
  add_action('pending_to_publish', 'wpforce_featured');
  add_action('future_to_publish', 'wpforce_featured');
2

2 Answers

1
votes

Basically the method I used was to create a one time script that I ran to move all inline images from the post to the Featured Gallery, like this:

// Loop over every post

while ( have_posts() ) : the_post();    

    // Get the images attached to the post.

    $imageids = array();

    $images = get_attached_media( 'image', $post->ID );

    foreach ($images as $image) {
        $imageids[] = $image->ID;
    }

    $comma_separated = implode(",", $imageids);

    // Save them to the Featured Gallery (Got this info by digging into the Featured Gallery plugin's source code)

    if ( $post->post_type == 'revision' ) {return;}
    if ( get_post_meta( $post->ID, 'fg_perm_metadata', FALSE ) ) {
        update_post_meta( $post->ID, 'fg_perm_metadata', $comma_separated );
    } else {
        add_post_meta( $post->ID, 'fg_perm_metadata', $comma_separated );
    }
    if ( !$comma_separated ) delete_post_meta( $post->ID, 'fg_perm_metadata' );

endwhile;

// Reset Query

wp_reset_query();

Then, I created a function that removes all images in the post content, which I placed in functions.php:

function remove_images( $content ) 
{
    $content =
        preg_replace(
            array('{<a(.*?)(wp-att|wp-content\/uploads)[^>]*><img}',
                '{ wp-image-[0-9]*" /></a>}'),
            array('<img','" />'),
            $content
        );
    $content = preg_replace('/<img(.*)>/i','', $content, 1);
    return $content;
}
add_filter( 'the_content', 'remove_images' );

First preg_replace removes the link around the images. Second removes the image.

0
votes

the problem is if i read you correctly is that all the images are located as urls in the post content?

You can parse them:

$content = get_the_content();
$html = new DOMDocument;
$html->loadHTML($content);

//get the images
$images = $html->getElementsByTagName('img');

foreach($images as $image=>$key {
     $imageurls[]=$key->attributes->getNamedItem("src")->value;//play with this cant remember how it returns object.
}

$content= preg_replace('/<img(.*)>/i','',$content,1);

echo $content; 

// you also have a array of images to use.