1

I'm using advanced custom fields and set an image upload field in user profiles. I'm using this image a few places on the website and need to set a custom size for it on some pages.

If I set add_image_size( 'homepage-thumb', 190, 259, true ); in my functions file how can I use this image size with the uploaded image since it's not a featured post or page image?

From my home page this is how I get the image

$author_logo = get_field('author_logo' , 'user_' . $author_id ); // image field, return type = "Image Object"

then <img src="<?php echo $author_logo;?>" />

Thanks

2 Answers 2

1

Your first problem is that custom field is saved as url, and not as id. So you need to retrieve the attachment id from the url. First tentation is to use the 'guid' field.

Resist the temptation and find another way: for robust wordpress code you have to think 'guid' not exists, so repeat with me: "guid does not exist".

Once 'guid' does not exists we have to find another way, this good one, come from here

function get_logo_id_from_url( $attachment_url = '' ) {
    global $wpdb;
    $attachment_id = false;
    if ( '' == $attachment_url ) return;
    $upload_dir_paths = wp_upload_dir();
    if ( false !== strpos( $attachment_url, $upload_dir_paths['baseurl'] ) ) {
     $attachment_url = preg_replace('/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i','',$attachment_url);
     $attachment_url = str_replace($upload_dir_paths['baseurl'].'/','',$attachment_url);
     $attachment_id = $wpdb->get_var( $wpdb->prepare(
       "SELECT wposts.ID FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta 
       WHERE wposts.ID = wpostmeta.post_id AND
       wpostmeta.meta_key = '_wp_attached_file' AND
       wpostmeta.meta_value = '%s' AND
       wposts.post_type = 'attachment'", $attachment_url ) );
    }
    return $attachment_id;
  }

Then we use previous function to wrire another one:

 function author_logo( $author_id = 0, $size = '', $alt = '' ) {
   if ( ! intval($author_id) ) return;
   $author_logo = get_field('author_logo' , 'user_' . $author_id );
   if ( filter_var($author_logo, FILTER_VALIDATE_URL) ) return;
   if ( empty($size) ) {
      $nosize = true;
   } else {
      $logo_id = get_logo_id_from_url( $author_logo );
      if ( ! intval($logo_id) ) {
         $nosize = true;
      } else {
        $sizedlogo = wp_get_attachment_image_src(logo_id, $size);
        $nosize = filter_var($sizedlogo[0], FILTER_VALIDATE_URL) ? false : true;
      }
   }
   if ( $nosize ) {
     return sprintf('<img src="%s" alt="%s" />', $author_logo, $alt );
   } else {
     return sprintf('<img src="%s" alt="%s" />', $sizedlogo[0], $alt );
   }
 }

After you saved these function in a plugin or in theme functions.php you can use them in this way:

<?php

// print default size
echo author_logo( $author_id ); 

// print in a custom size
echo author_logo( $author_id, 'homepage-thumb' );

// print in a custom size with custom alt text
echo author_logo( $author_id, 'homepage-thumb', 'Author Logo' ); 

?>

Code is untested but should works, unless any typo...

0

This might work if it is the full size image url you are storing in your custom field. Try to place this function in functions.php

function get_attachment_id_from_src ($image_src) {
    global $wpdb;
    $id = '';
    if ($image_src){
        $query = "SELECT ID FROM {$wpdb->posts} WHERE guid='".$image_src."'";
        $id = $wpdb->get_var($query);
    }
    return $id;
}

then get the image this way

$author_logo_src = get_field('author_logo' , 'user_' . $author_id ); // image field
$attachment_id = get_attachment_id_from_src ($author_logo_src);
$attachment_image_src = wp_get_attachment_image_src( $attachment_id, $array(190,259) );

and then

<img src="<?php echo $attachment_image_src[0];?>" width="<?php echo $attachment_image_src[1];?>" height="<?php echo $attachment_image_src[2];?>" alt="User Logo" />

More info: wp_get_attachment_image_src

1
  • 1
    Please, never use 'guid'. Every time you type 'guid' in wordpress code, be sure you are doing something wrong.
    – gmazzap
    Commented Aug 4, 2013 at 15:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.