'Get PATH instead of URL [duplicate]

This code:

$files[$file->ID]['file_title'] = '<p><a target="_blank" href="' . esc_url( wp_get_attachment_url( $file->ID ) ) . '">' . $real_title . '</a>' . $title . '</p>';

Gives me:

https://example.com/wp-content/uploads/2022/03/sample.pdf`

But I want:

/wp-content/uploads/2022/03/sample.pdf`

How do I remove https://example.com/?

php


Solution 1:[1]

You can use parse_url to get the path information from a URL:

parse_url(wp_get_attachment_url($file->ID) , PHP_URL_PATH)

https://3v4l.org/5KmsJ

Alternatively str_replace or preg_replace could be used to remove https://example.com/ that those can result in incorrect results.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user3783243