Hello,

I am unable to fetch and display images in my wordpress blogs as its shortlinked by IFTTT in format "http://ift.tt/22lveRw", can someone please help me out with this, the follow code does fetch the image if its in normal format i.e "http://i.imgur.com/Z2aGVEl.png", what changes do i need to make the following code to make it work with ifttt format?

Please help.

function catch_that_image() {
  global $post, $posts;
  $first_img = '';
  ob_start();
  ob_end_clean();
  $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
  $first_img = $matches[1][0];

  if(empty($first_img)) {
    $first_img = "/path/to/default.png";
  }
  return $first_img;
}

Dani AI

Generated

Short summary and practical fixes

This thread shows the common IFTTT pitfall: posts contain an IFTTT shortlink (ift.tt) instead of a direct image URL, so simple extractors that look for <img src="..."> miss the image. The quickest fix — already suggested by and confirmed by — is to turn off IFTTT's URL shortening in your IFTTT account so the original image URL lands in the post. If you can change the IFTTT side, do that first.

Programmatic alternatives (when you cannot change IFTTT)

  • Detect shortlinks in the post body (domains like ift.tt or other shorteners).
  • Resolve the shortlink server-side and check the final URL or Content-Type before using it.
  • If the resolved target is an HTML page, fetch its og:image meta or the first <img>.

Example: a small PHP helper to follow redirects and return the final URL and content type:

<?php
function resolve_redirect($url) {
    if (!function_exists('curl_init')) { return array($url, null); }
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_exec($ch);
    $final = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
    $ctype = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
    curl_close($ch);
    return array($final, $ctype);
}
?>

Sideloading into WordPress

If the resolved URL is an image, sideload it into WP so it becomes a proper attachment (use download_url, media_handle_sideload, etc.). Example helper pattern:

<?php
// requires wp-admin includes before calling
function sideload_image_from_url($image_url, $post_id = 0) {
    require_once ABSPATH . 'wp-admin/includes/file.php';
    require_once ABSPATH . 'wp-admin/includes/media.php';
    require_once ABSPATH . 'wp-admin/includes/image.php';
    $tmp = download_url($image_url);
    if (is_wp_error($tmp)) { return false; }
    $file = array('name' => basename(parse_url($image_url, PHP_URL_PATH)), 'tmp_name' => $tmp);
    $id = media_handle_sideload($file, $post_id);
    if (is_wp_error($id)) { @unlink($tmp); return false; }
    return $id;
}
?>

Quick troubleshooting notes

  • Ensure your server supports outbound HTTP (cURL or allow_url_fopen).
  • Set sensible timeouts and a max redirect guard to avoid loops.
  • If a shortlink resolves to an HTML page, prefer og:image over scraping arbitrary images.
  • Validate Content-Type before treating a resource as an image to avoid saving non-image data.

If changing the IFTTT setting is possible, that is the least fragile approach; otherwise the resolver + sideload flow above is a durable fallback.

Recommended Answers

All 5 Replies

: You are Awesome!! I didn't even knew that option exist :P
Done! Unchecked it, I think this is gonna make my like so much easier :)

If that fixed it, good news. I'm just looking into IFTTT so questions like this help be learn more.

Yes, that fixed my issue :)

For the archives, if the above link dies it wrote "login, go to Preferences > Settings. Then scroll down until you see URL Shortening. You should see a checkbox checked with Auto shorten URLs. Simply uncheck this and update your settings."

Tameat614, if you mark this solved for others to know it's complete.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.