upload or auto setup feature photo by user avatar

I want user avatar image is set as a feature image, If there is no featured image.

I am trying to do like this.

Read More
function auto_featured_image() {
global $post;

if (!has_post_thumbnail($post->ID)) {
    $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
    //$attached_avatar = get_avatar( get_the_author_meta( 'ID' ));
    $attached_avatar = get_avatar(1);

  if ($attached_image) { // set feature image as the 1st image on post.
          foreach ($attached_image as $attachment_id => $attachment) {
               set_post_thumbnail($post->ID, $attachment_id);
          }
  }/*elseif($attached_avatar){ // set feature image as the avatar image.
            foreach ($attached_avatar as $attachment_id => $attachment) {
                set_post_thumbnail($post->ID, $attachment_id);
            }

  }*/
 }
}
 // Use it temporary to generate all featured images
add_action('the_post', 'auto_featured_image');
// Used for new posts
add_action('save_post', 'auto_featured_image');
add_action('draft_to_publish', 'auto_featured_image');
add_action('new_to_publish', 'auto_featured_image');
add_action('pending_to_publish', 'auto_featured_image');
add_action('future_to_publish', 'auto_featured_image');

set feature image as the 1st image on post works fine. but set avatar as feature image is not working.

How can I make it work?
Thanks,

Related posts

1 comment

  1. In general avatars can not be a replacement to attachments unless you have a pluging which forces all avatars to be uploaded via the wordpress upload process to the site (or in other words, be included in the media library). Avatars can be any URL while attachments have an id in your DB. Therefor your attempt to treat the avatar as an attachment can not succeed.

    The only way to make it work is if you will first fetch the avatar image from whatever service it is located on, and create an attachment out of it. Then you can treat it in the way your code implies you intended to do.

Comments are closed.