Skip to content

Instantly share code, notes, and snippets.

@kpirnie
Created August 13, 2025 18:19
Show Gist options
  • Save kpirnie/67a0bde51f700588a221b3335ead2dd4 to your computer and use it in GitHub Desktop.
Save kpirnie/67a0bde51f700588a221b3335ead2dd4 to your computer and use it in GitHub Desktop.
WordPress - Attachment Path from URL
/**
* attachment_url_to_path
*
* Static method for getting the physical server path to an image/attachment/file
* based on it's URL
*
* @since 7.3
* @access public
* @author Kevin Pirnie <[email protected]>
* @package Kevin's Framework
*
* @param string $_url String of the URL to be parsed
*
* @return string The path to the file specified
*
*/
public function attachment_url_to_path( string $_url ) : string {
// parse the URL
$parsed_url = parse_url( $url );
// if it's empty, return false
if( empty( $parsed_url['path'] ) ) return false;
// set the file path
$file = ABSPATH . ltrim( $parsed_url['path'], '/' );
// if the file exists, return it
if ( @is_readable( $file ) ) return $file;
// default return false
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment