WordPress.org

Ready to get started?Download WordPress

Forums

Possible bug, need workaround to set featured image (1 post)

  1. t0phux
    Member
    Posted 1 year ago #

    I have a very interesting problem. If this: set_post_thumbnail( $post_id, 57 ); is in an else statement, $post->post_content only returns 1-3 of the first characters of the post.

    I have uploaded a video of the problem, so you can fully understand it (it's short): https://vimeo.com/48167185

    And my full code is below:

    <?php
    
    	/*
    	Plugin Name: Featured Image grabber and poster
    	Plugin URI: http://badass-gaming.com
    	Description: A plugin to grab the image in your posts and set it as the featured image.
    	Version: 1.0
    	Author: Stephen Roy
    	Author URI: http://badass-gaming.com
    	License: GPL
    	*/
    
    	function autoset_featured( $post_id, $post ) {
    
    	  if ( $post->post_status == 'auto-draft' ) return;
    	  if ( wp_is_post_revision( $post_id ) ) return;
    
    	  if ( ! has_post_thumbnail( $post_id ) )  {
    	    $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);
    	      }
    	    } else {
    
    	      // echo 'here: <br />';
    	      // var_dump( $post->post_content );
    	      // echo '<hr />';
    	      // the_content();
    
    	      $img_srcs = array();
    	      if ( preg_match_all('/<img (.+?)>/', $post->post_content, $matches) ) {
    	        foreach ($matches[1] as $match) {
    	          foreach ( wp_kses_hair($match, array('http')) as $attr) {
    	            if ( 'src' == $attr['name'] )
    	            $img_srcs[] = $attr['value'];
    	          }
    	        }
    	      }
    	      // echo '<pre>'. print_r( $img_srcs, true ) .'</pre>';
    	      // die();
    
    	      /* THIS IS WHERE I'M HAVING PROBLEMS - If the else is active, then it img_srcs is blank, but if it's commented out, then img_srcs has content. */
    	      if( count( $img_srcs ) > 0 ) {
    
    	        foreach( $img_srcs as $img ) {
    	          $id = media_sideload_image_quiet( $img, $post_id );
    	        }
    
    	        //will make the last image the thumbnail
    	        //need more logic (perhaps array_reverse $img_srcs) to make the first img the thumbnail
    	        set_post_thumbnail( $post_id, $id );
    
    	      } else {
    	      	//If you comment this out, it uploads the picture normally.
    	        set_post_thumbnail( $post_id, 57 );
    	      }
    
    	    }
    	  }
    	}
    
    	add_action('save_post', 'autoset_featured', 10, 2 );
    
    	if ( ! function_exists( 'media_sideload_image_quiet' ) ) {
    		// just like media_sideload_image() but returns an ID
    		function media_sideload_image_quiet($file, $post_id, $desc = null) {
    		  if ( ! empty($file) ) {
    		    // Download file to temp location
    		    $tmp = download_url( $file );
    
    		    // Set variables for storage
    		    // fix file filename for query strings
    		    preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $file, $matches);
    		    if ( count( $matches) < 1 ) return false;
    		    $file_array['name'] = basename($matches[0]);
    		    $file_array['tmp_name'] = $tmp;
    
    		    // If error storing temporarily, unlink
    		    if ( is_wp_error( $tmp ) ) {
    		      @unlink($file_array['tmp_name']);
    		      $file_array['tmp_name'] = '';
    		    }
    
    		    // do the validation and storage stuff
    		    $id = media_handle_sideload( $file_array, $post_id, $desc );
    		    // If error storing permanently, unlink
    		    if ( is_wp_error($id) ) {
    		      @unlink($file_array['tmp_name']);
    		    }
    
    		    return $id; //may be actual id (if successful) or wp error object (on failure)
    		  }
    
    		  return false;
    		}
    	}
    
    ?>

    [ Please do not bump, that's not permitted here. ]

Topic Closed

This topic has been closed to new replies.

About this Topic