url_to_postid( string $url ): int
- Since
- 1.0.0
- Source
wp-includes/rewrite.php:492
Description
Checks are supposedly from the hosted site blog.
Compatibility
- WordPress
- since 1.0.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$urlstring- Permalink to check.
Return value
int- Post ID, or 0 on failure.
Performance profile
How much work a call to url_to_postid() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Scales with input
- Instructions
- 77–222
- Plugin surface
- 1 hook
- Called by
- 7
Reaches the database via get_post().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 296.
Third-party callbacks on 'url_to_postid' run inside this call, and their cost is not bounded by anything here.
7 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - optionoption read or write
get_option()called directly - querycontent query
get_post()called directly - cacheobject cache
wp_cache_get()one call below url_to_postid() - serializeserialisation
maybe_unserialize()one call below url_to_postid()
Further down the call graph this can also reach transient. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 22 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost url_to_postid() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!is_string($url_host) && !is_string($home_url_host) && !preg_match() && empty($rewrite) | 77 | apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), ->wp_rewrite_rules() |
!is_string($url_host) && !is_string($home_url_host) && !preg_match() && empty($rewrite) | 82 | apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), get_option(), ->wp_rewrite_rules() |
!is_string($url_host) && !is_string($home_url_host) && !preg_match() && !empty($rewrite) | 108–115 | apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), ->wp_rewrite_rules(), ->using_index_permalinks(), trailingslashit(), home_url(), home_url(), get_post_types() |
!is_string($url_host) && !is_string($home_url_host) && !preg_match() && !empty($rewrite) | 113–120 | apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), get_option(), ->wp_rewrite_rules(), ->using_index_permalinks(), trailingslashit(), home_url(), home_url(), get_post_types() |
!is_string($url_host) && !is_string($home_url_host) && !preg_match() && !empty($rewrite) | 125–133 | apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), ->wp_rewrite_rules(), ->using_index_permalinks(), trailingslashit(), home_url(), home_url(), parse_url(), preg_quote(), trailingslashit(), get_post_types() |
!is_string($url_host) && !is_string($home_url_host) && !preg_match() && !empty($rewrite) | 130–138 | apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), get_option(), ->wp_rewrite_rules(), ->using_index_permalinks(), trailingslashit(), home_url(), home_url(), parse_url(), preg_quote(), trailingslashit(), get_post_types() |
!is_string($url_host) && !is_string($home_url_host) && !preg_match() && !empty($rewrite) && ->using_index_permalinks() | 135–138 | apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), get_option(), get_option(), ->wp_rewrite_rules(), ->using_index_permalinks(), trailingslashit(), home_url(), home_url(), parse_url(), preg_quote(), trailingslashit(), get_post_types() |
!is_string($url_host) && !is_string($home_url_host) && !empty($rewrite) && !$rewrite && !$wp_rewrite | 153–171 | apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), ->wp_rewrite_rules(), ->using_index_permalinks(), trailingslashit(), home_url(), home_url(), get_post_types(), preg_match(), ::WP_MatchesMapRegex(), addslashes(), parse_str(), wp_resolve_numeric_slug_conflicts() |
!is_string($url_host) && !is_string($home_url_host) && !empty($rewrite) && !$rewrite && !$wp_rewrite | 158–176 | apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), get_option(), ->wp_rewrite_rules(), ->using_index_permalinks(), trailingslashit(), home_url(), home_url(), get_post_types(), preg_match(), ::WP_MatchesMapRegex(), addslashes(), parse_str(), wp_resolve_numeric_slug_conflicts() |
!is_string($url_host) && !is_string($home_url_host) && !empty($rewrite) && !$rewrite && $wp_rewrite | 159–177 | apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), ->wp_rewrite_rules(), ->using_index_permalinks(), trailingslashit(), home_url(), home_url(), get_post_types(), preg_match(), preg_match(), ::WP_MatchesMapRegex(), addslashes(), parse_str(), wp_resolve_numeric_slug_conflicts() |
!is_string($url_host) && !is_string($home_url_host) && !empty($rewrite) && !$rewrite && $wp_rewrite | 164–182 | apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), get_option(), ->wp_rewrite_rules(), ->using_index_permalinks(), trailingslashit(), home_url(), home_url(), get_post_types(), preg_match(), preg_match(), ::WP_MatchesMapRegex(), addslashes(), parse_str(), wp_resolve_numeric_slug_conflicts() |
!is_string($url_host) && !is_string($home_url_host) && !empty($rewrite) && !$rewrite && !$wp_rewrite | 170–189 | apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), ->wp_rewrite_rules(), ->using_index_permalinks(), trailingslashit(), home_url(), home_url(), parse_url(), preg_quote(), trailingslashit(), get_post_types(), preg_match(), ::WP_MatchesMapRegex(), addslashes(), parse_str(), wp_resolve_numeric_slug_conflicts() |
10 further outcomes, up to 221 instructions
!is_string($url_host) && !is_string($home_url_host) && !empty($rewrite) && !$rewrite && $wp_rewrite | 175–199 | apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), ->wp_rewrite_rules(), ->using_index_permalinks(), trailingslashit(), home_url(), home_url(), get_post_types(), preg_match(), preg_match(), get_page_by_path(), get_post_status_object(), ::WP_MatchesMapRegex(), addslashes(), parse_str(), wp_resolve_numeric_slug_conflicts() |
!is_string($url_host) && !is_string($home_url_host) && !empty($rewrite) && !$rewrite && !$wp_rewrite | 175–194 | apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), get_option(), ->wp_rewrite_rules(), ->using_index_permalinks(), trailingslashit(), home_url(), home_url(), parse_url(), preg_quote(), trailingslashit(), get_post_types(), preg_match(), ::WP_MatchesMapRegex(), addslashes(), parse_str(), wp_resolve_numeric_slug_conflicts() |
!is_string($url_host) && !is_string($home_url_host) && !empty($rewrite) && !$rewrite && $wp_rewrite | 176–195 | apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), ->wp_rewrite_rules(), ->using_index_permalinks(), trailingslashit(), home_url(), home_url(), parse_url(), preg_quote(), trailingslashit(), get_post_types(), preg_match(), preg_match(), ::WP_MatchesMapRegex(), addslashes(), parse_str(), wp_resolve_numeric_slug_conflicts() |
!is_string($url_host) && !is_string($home_url_host) && !empty($rewrite) && !$rewrite && $wp_rewrite | 180–204 | apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), get_option(), ->wp_rewrite_rules(), ->using_index_permalinks(), trailingslashit(), home_url(), home_url(), get_post_types(), preg_match(), preg_match(), get_page_by_path(), get_post_status_object(), ::WP_MatchesMapRegex(), addslashes(), parse_str(), wp_resolve_numeric_slug_conflicts() |
!is_string($url_host) && !is_string($home_url_host) && !empty($rewrite) && ->using_index_permalinks() && !$rewrite && !$wp_rewrite | 180–193 | apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), get_option(), get_option(), ->wp_rewrite_rules(), ->using_index_permalinks(), trailingslashit(), home_url(), home_url(), parse_url(), preg_quote(), trailingslashit(), get_post_types(), preg_match(), ::WP_MatchesMapRegex(), addslashes(), parse_str(), wp_resolve_numeric_slug_conflicts() |
!is_string($url_host) && !is_string($home_url_host) && !empty($rewrite) && !$rewrite && $wp_rewrite | 181–200 | apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), get_option(), ->wp_rewrite_rules(), ->using_index_permalinks(), trailingslashit(), home_url(), home_url(), parse_url(), preg_quote(), trailingslashit(), get_post_types(), preg_match(), preg_match(), ::WP_MatchesMapRegex(), addslashes(), parse_str(), wp_resolve_numeric_slug_conflicts() |
!is_string($url_host) && !is_string($home_url_host) && !empty($rewrite) && ->using_index_permalinks() && !$rewrite && $wp_rewrite | 186–199 | apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), get_option(), get_option(), ->wp_rewrite_rules(), ->using_index_permalinks(), trailingslashit(), home_url(), home_url(), parse_url(), preg_quote(), trailingslashit(), get_post_types(), preg_match(), preg_match(), ::WP_MatchesMapRegex(), addslashes(), parse_str(), wp_resolve_numeric_slug_conflicts() |
!is_string($url_host) && !is_string($home_url_host) && !empty($rewrite) && !$rewrite && $wp_rewrite | 192–217 | apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), ->wp_rewrite_rules(), ->using_index_permalinks(), trailingslashit(), home_url(), home_url(), parse_url(), preg_quote(), trailingslashit(), get_post_types(), preg_match(), preg_match(), get_page_by_path(), get_post_status_object(), ::WP_MatchesMapRegex(), addslashes(), parse_str(), wp_resolve_numeric_slug_conflicts() |
!is_string($url_host) && !is_string($home_url_host) && !empty($rewrite) && !$rewrite && $wp_rewrite | 197–222 | apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), get_option(), ->wp_rewrite_rules(), ->using_index_permalinks(), trailingslashit(), home_url(), home_url(), parse_url(), preg_quote(), trailingslashit(), get_post_types(), preg_match(), preg_match(), get_page_by_path(), get_post_status_object(), ::WP_MatchesMapRegex(), addslashes(), parse_str(), wp_resolve_numeric_slug_conflicts() |
!is_string($url_host) && !is_string($home_url_host) && !empty($rewrite) && ->using_index_permalinks() && !$rewrite && $wp_rewrite | 202–221 | apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), get_option(), get_option(), ->wp_rewrite_rules(), ->using_index_permalinks(), trailingslashit(), home_url(), home_url(), parse_url(), preg_quote(), trailingslashit(), get_post_types(), preg_match(), preg_match(), get_page_by_path(), get_post_status_object(), ::WP_MatchesMapRegex(), addslashes(), parse_str(), wp_resolve_numeric_slug_conflicts() |
This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 296 | 77–222 | 39 | |
| 8.5 | 296 | 77–222 | 39 | |
| 8.4 | 296 | 77–222 | 39 | 49 fewer instructions than PHP 8.3 |
| 8.3 | 345 | 86–250 | 39 | |
| 8.2 | 345 | 86–250 | 39 | |
| 8.1 | 345 | 86–250 | 39 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 346 | 86–251 | 39 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 1
One hook fires while url_to_postid() runs, in this order:
- apply_filters( url_to_postid )filterline 502 (+10 into the body)
Filters the URL to derive the post ID from.
Uses · 15
- apply_filters()Calls the callback functions that have been added to a filter hook.
- home_url()Retrieves the URL for the current site where the front end is accessible.
- absint()Converts a value to non-negative integer.
- set_url_scheme()Sets the scheme for a URL.
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- get_option()Retrieves an option value based on an option name.
- get_post()Retrieves post data given a post ID or post object.
- trailingslashit()Appends a trailing slash.
- get_post_types()Gets a list of all registered post type objects.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- get_page_by_path()Retrieves a page given its path.
- get_post_status_object()Retrieves a post status object by name.
Show all 15
- wp_resolve_numeric_slug_conflicts()Resolves numeric slugs that collide with date permalinks.
- WP_MatchesMapRegex::apply()Substitute substring matches in subject.
- WP_Query::__construct()Constructor.
Used by · 7
- WP_oEmbed_Controller::get_item()Callback for the embed API endpoint.
- get_oembed_response_data_for_url()Retrieves the oEmbed response data for a given URL.
- is_local_attachment()Determines whether an attachment URI is local and really an attachment.
- pingback()Pings back the links found in a post.
- wp_xmlrpc_server::pingback_extensions_getPingbacks()Retrieves an array of URLs that pingbacked the given URL.
- wp_xmlrpc_server::pingback_ping()Retrieves a pingback and registers it.
- wp_xmlrpc_server::wp_newComment()Creates a new comment.
Source code
function url_to_postid( $url ) { global $wp_rewrite; /** * Filters the URL to derive the post ID from. * * @since 2.2.0 * * @param string $url The URL to derive the post ID from. */ $url = apply_filters( 'url_to_postid', $url ); $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 ) { return 0; } // First, check to see if there is a 'p=N' or 'page_id=N' to match against. if ( preg_match( '#[?&](p|page_id|attachment_id)=(\d+)#', $url, $values ) ) { $id = absint( $values[2] ); if ( $id ) { return $id; } } // Get rid of the #anchor. $url_split = explode( '#', $url ); $url = $url_split[0]; // Get rid of URL ?query=string. $url_split = explode( '?', $url ); $url = $url_split[0]; // Set the correct URL scheme. $scheme = parse_url( home_url(), PHP_URL_SCHEME ); $url = set_url_scheme( $url, $scheme ); // Add 'www.' if it is absent and should be there. if ( str_contains( home_url(), '://www.' ) && ! str_contains( $url, '://www.' ) ) { $url = str_replace( '://', '://www.', $url ); } // Strip 'www.' if it is present and shouldn't be. if ( ! str_contains( home_url(), '://www.' ) ) { $url = str_replace( '://www.', '://', $url ); } if ( trim( $url, '/' ) === home_url() && 'page' === get_option( 'show_on_front' ) ) { $page_on_front = get_option( 'page_on_front' ); if ( $page_on_front && get_post( $page_on_front ) instanceof WP_Post ) { return (int) $page_on_front; } } // Check to see if we are using rewrite rules. $rewrite = $wp_rewrite->wp_rewrite_rules(); // Not using rewrite rules, and 'p=N' and 'page_id=N' methods failed, so we're out of options. if ( empty( $rewrite ) ) { return 0; } // Strip 'index.php/' if we're not using path info permalinks.Changelog
Introduced in 1.0.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 tag, from
src/wp-includes/rewrite.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.