diff --git a/src/wp-includes/rewrite.php b/src/wp-includes/rewrite.php index af66d02b94..477a85fbef 100644 --- a/src/wp-includes/rewrite.php +++ b/src/wp-includes/rewrite.php @@ -500,8 +500,21 @@ function url_to_postid( $url ) { */ $url = apply_filters( 'url_to_postid', $url ); - $url_host = str_replace( 'www.', '', parse_url( $url, PHP_URL_HOST ) ); - $home_url_host = str_replace( 'www.', '', parse_url( home_url(), PHP_URL_HOST ) ); + $url_host = parse_url( $url, PHP_URL_HOST ); + + if ( is_string( $url_host ) ) { + $url_host = str_replace( 'www.', '', $url_host ); + } else { + $url_host = ''; + } + + $home_url_host = parse_url( home_url(), PHP_URL_HOST ); + + if ( is_string( $home_url_host ) ) { + $home_url_host = str_replace( 'www.', '', $home_url_host ); + } else { + $home_url_host = ''; + } // Bail early if the URL does not belong to this site. if ( $url_host && $url_host !== $home_url_host ) { diff --git a/tests/phpunit/tests/rewrite.php b/tests/phpunit/tests/rewrite.php index bee61dc66c..2bb7254abf 100644 --- a/tests/phpunit/tests/rewrite.php +++ b/tests/phpunit/tests/rewrite.php @@ -255,8 +255,17 @@ class Tests_Rewrite extends WP_UnitTestCase { $this->assertSame( $grandchild_id_2, url_to_postid( get_permalink( $grandchild_id_2 ) ) ); } - public function test_url_to_postid_home_has_path() { + /** + * @covers ::url_to_postid + */ + public function test_url_to_postid_url_has_only_path() { + $this->assertSame( 0, url_to_postid( '/example/' ) ); + } + /** + * @covers ::url_to_postid + */ + public function test_url_to_postid_home_has_only_path() { update_option( 'home', home_url( '/example/' ) ); $id = self::factory()->post->create(