Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/wp-includes/link-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -3662,6 +3662,29 @@ function content_url( $path = '' ) {
return apply_filters( 'content_url', $url, $path );
}

/**
* Retrieves the uploads directory URL.
*
* Returns either the base uploads URL (e.g. https://example.com/wp-content/uploads)
* or, when requested, the URL to the current time-based subdirectory
* (e.g. https://example.com/wp-content/uploads/2025/09) if year/month folders are enabled.
*
* @since 6.9.0
*
* @param bool $with_subdir Optional. Whether to include the time-based subdirectory
* when it is configured. Default true.
* @return string Uploads URL. Empty string on failure.
*/
function upload_url( $with_subdir = true ) {
$uploads = wp_upload_dir();

if ( ! empty( $uploads['error'] ) ) {
return '';
}

return $with_subdir ? $uploads['url'] : $uploads['baseurl'];
}

/**
* Retrieves a URL within the plugins or mu-plugins directory.
*
Expand Down
29 changes: 29 additions & 0 deletions tests/phpunit/tests/upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,33 @@ public function test_upload_dir_empty() {
$this->assertSame( $subdir, $info['subdir'] );
$this->assertFalse( $info['error'] );
}

/**
* Tests the upload_url() function with default settings.
*
* @ticket 33963
*/
public function test_upload_url() {
$expected = wp_upload_dir();
$this->assertFalse( $expected['error'] );

$this->assertSame( $expected['url'], upload_url() );
$this->assertSame( $expected['url'], upload_url( true ) );
$this->assertSame( $expected['baseurl'], upload_url( false ) );
}

/**
* Tests the upload_url() function when year/month folders are disabled.
*
* @ticket 33963
*/
public function test_upload_url_without_yearmonth_folders() {
update_option( 'uploads_use_yearmonth_folders', 0 );

$expected = wp_upload_dir();

$this->assertFalse( $expected['error'] );
$this->assertSame( $expected['url'], upload_url() );
$this->assertSame( $expected['baseurl'], upload_url( false ) );
}
}
Loading