Retrieve attachment file paths with get_attached_file in WordPress

The get_attached_file function in WordPress is designed to retrieve the file path of an attachment’s file. It is primarily used when there is a need to know the physical path of an uploaded file in the server. This function retrieves the file path based on the attachment ID that is stored in the WordPress database.

One of the main uses of the get_attached_file function is in the context of media files that are uploaded to a WordPress site. When a media file such as an image, video, or document is uploaded to a WordPress site, it is stored in a specific location on the server and an attachment post is created in the WordPress database to keep track of that file. The attachment post contains various information about the file including its ID, file name, file type, and file path. The get_attached_file function can be used to retrieve the file path of the uploaded file using the attachment ID.

Another use of the get_attached_file function is when there is a need to manipulate the uploaded file directly. Since this function provides the physical path of the file, it can be used to read, write, or delete the file directly on the server.

Parameters Accepted by the get_attached_file Function

The get_attached_file function in WordPress accepts two parameters. The parameters and their descriptions are as follows:

  • $attachment_id (int): This is a required parameter that represents the ID of the attachment.
  • $unfiltered (bool): This is an optional parameter. Its default value is false. It determines whether to bypass the ‘get_attached_file’ filter or not.

Return Value of the get_attached_file Function

The get_attached_file function returns a string or a boolean value. If the function is successful, it returns a string which is the file path to the location of the attached file. If the function fails, it returns false.

Examples

How to get the path of an attached file in WordPress

$attachment_id = 123; // replace with your attachment ID
$file_path = get_attached_file( $attachment_id );
echo $file_path;

In this example, we’re using the get_attached_file() function to retrieve the path of an attached file in WordPress. We first specify the attachment ID, then we call the function with this ID as the argument. The function then returns the path of the file, which we echo out.

How to check if an attachment file exists in WordPress

$attachment_id = 123; // replace with your attachment ID
$file_path = get_attached_file( $attachment_id );
if ( file_exists( $file_path ) ) {
 echo 'File exists';
} else {
 echo 'File does not exist';
}

In this example, we’re using the get_attached_file() function in combination with the file_exists() function to check if an attachment file exists. We first get the path of the file with get_attached_file(), then we check if a file exists at that path with file_exists(). We then output a message based on whether the file exists or not.

Conclusion

The get_attached_file function in WordPress is a utility function that retrieves the file path of an attached file. This function is typically used when there is a need to manipulate or access the file directly on the server. It takes in a single parameter, the attachment ID, and returns the absolute filepath of the file attached to the given post. While it’s primarily used in the context of media attachments, it can also be utilized for retrieving paths of other post types that support attachments.

Related WordPress Functions