5

wp_get_attachment_url() process full file path like

http://example.com/wp-content/uploads/2014/12/aura.mp3

I want the url without http://example.com/ So, I want above example as wp-content/uploads/2014/12/aura.mp3 instead of http://example.com/wp-content/uploads/2014/12/aura.mp3. How to do it?

Sahriar Saikat
  • 613
  • 2
  • 7
  • 17

4 Answers4

3

You can really easily explode it by / and then take the part with index 3. Example

$url = wp_get_attachment_url(id); //id is file's id
$urllocal = explode(site_url(), $url)[1]; //output local path
Sahriar Saikat
  • 613
  • 2
  • 7
  • 17
Roope Hakulinen
  • 7,326
  • 4
  • 43
  • 66
  • it only returns wp-content, not full file path – Sahriar Saikat Dec 31 '14 at 08:32
  • Are you sure you tried the latest version? I had to fix it after I posted it. – Roope Hakulinen Dec 31 '14 at 08:34
  • I used this `$mp3remoteurl = wp_get_attachment_url($_GET["id"]); $mp3localurl = ''.explode("/", $mp3remoteurl)[1].'/'.explode("/", $mp3remoteurl)[2].'/'.explode("/", $mp3remoteurl)[3].'/'.explode("/", $mp3remoteurl)[4].'/'.explode("/", $mp3remoteurl)[5].'';` to get wp-content/uploads/2014/12/aura.mp3 but this is not the proper way. Ain't I right? – Sahriar Saikat Dec 31 '14 at 08:37
  • Yea. wp 4.1 - latest version and it is in localhost – Sahriar Saikat Dec 31 '14 at 08:37
  • @SahriarSaikat: Well, it is one way to do it, but not the prettiest for sure. That is why I changed my code to work with the server name when exploding: explode("example.com/", $url)[1]; – Roope Hakulinen Dec 31 '14 at 08:38
  • @SahriarSaikat: Great, I'm sorry for the struggle caused by my edits. :( – Roope Hakulinen Dec 31 '14 at 08:50
1

Here is the WordPress way using WordPress functions (avoid hacking):

$fullsize_path = get_attached_file( $attachment_id ); // Full path
$filename_only = basename( get_attached_file( $attachment_id ) ); // Just the file name

WordPress has tons of functions, so first try to find the function on the docs: https://developer.wordpress.org/reference/functions/get_attached_file/

gtamborero
  • 2,898
  • 27
  • 28
0

You can use PHP's function explode.

Here is the code:

    <?php
         $image_url = wp_get_attachment_url( 9 ); //ID of your attachment
         $my_image_url = explode('/',$image_url,4);
         echo $my_image_url[3];
    ?>
Rohil_PHPBeginner
  • 6,002
  • 2
  • 21
  • 32
0

You can implode your entire url on / and array_slice from the end, then implode it back in on /.

$url = wp_get_attachment_url($item->ID); //id is file's id
$url_relative = implode(array_slice(explode('/', $url),-3,3),'/');
//Returns: 2019/08/image.jpg

That way if your WordPress is on a subdomain or localhost or the images are on S3 it won't crash.

Miro
  • 8,402
  • 3
  • 34
  • 72
  • 1
    That's a really old question you have answered. If I am to answer my 5 years old question now, I would say `str_replace('http://example.com/', '', wp_get_attachment_url($item->ID));` is a nice choice. – Sahriar Saikat Oct 02 '19 at 17:15