• I have set up featured images for my theme.

    After the user has incorporated a featured image into his post, I’m running a ‘post-updated’ hook to see what the path of the featured image is:

    add_action('post_updated','magico_after_saved');
    function magico_after_saved($post_id) {
      $img= wp_get_attachment_image_src(get_post_thumbnail_id( $post_id ));
      magico_resize_image($img);
    }

    Some files return the path to the original image, ie: https://127.0.0.1/divo/wp-content/uploads/2014/05/IPC.1.bmp

    Others return the path to the thumbnail:https://127.0.0.1/divo/wp-content/uploads/2014/05/IPD.5-150×150.jpg

    Currently I’m manipulating the path with strrchr() and substr() to extract the ‘-150×150’ string if it exists, but there must be a way of consistently retrieving the original file name rather than the thumbnail.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter dgcov

    (@dgcov)

    This is my hack:

    function magico_thumb_path($path,$addExt=true) {
      /*
      * if the path points to the thumbnail, then return the path to the file
      * if $addExt is false, then the path is returned without the extension so that
      * a new identifier (such as "-640x300") may be appended prior to the extension.
      */
        $ext=strrchr($path,'.');
        $size=strrchr($path,'-');
        $pos=($size=='-150x150'.$ext)?8:0;
        $name=substr($path,0,-(strlen($ext)+$pos));
        if ($addExt) {
          return $name.$ext;
        }
        return $name;
      }

    Feel free to diss it or suggest alternatives.

    (My slider uses a size 640×300, so I’m resizing to this.)

    Try using:

    // Please note that this returns an array.
    $img = wp_get_attachment_image_src(get_post_thumbnail_id($post_id), 'full');

    or

    $img = wp_get_attachment_url(get_post_thumbnail_id($post_id));
    Thread Starter dgcov

    (@dgcov)

    Great.

    That’s what I needed. Thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Getting the url of a featured image’ is closed to new replies.