_wp_get_attachment_relative_path

Get the attachment path relative to the upload directory.

Description

(string) _wp_get_attachment_relative_path( (string) $file ); 

Returns (string)

Attachment path relative to the upload directory.

Parameters (1)

0. $file (string)
Attachment file name.

Usage

  1. if ( !function_exists( '_wp_get_attachment_relative_path' ) ) { 
  2. require_once ABSPATH . WPINC . '/media.php'; 
  3.  
  4. // Attachment file name. 
  5. $file = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = _wp_get_attachment_relative_path($file); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/media.php  
  1. function _wp_get_attachment_relative_path( $file ) { 
  2. $dirname = dirname( $file ); 
  3.  
  4. if ( '.' === $dirname ) { 
  5. return ''; 
  6.  
  7. if ( false !== strpos( $dirname, 'wp-content/uploads' ) ) { 
  8. // Get the directory name relative to the upload directory (back compat for pre-2.7 uploads) 
  9. $dirname = substr( $dirname, strpos( $dirname, 'wp-content/uploads' ) + 18 ); 
  10. $dirname = ltrim( $dirname, '/' ); 
  11.  
  12. return $dirname;