url_to_postid() – Examine a URL and try to determine the post ID it represents.

You appear to be a bot. Output may be restricted

Description

Examines a URL and try to determine the post ID it represents.

Checks are supposedly from the hosted site blog.

Usage

$int = url_to_postid( $url );

Parameters

$url
( string ) required – Permalink to check.

Returns

int Post ID, or 0 on failure.

Source

File name: wordpress/wp-includes/rewrite.php
Lines:

1 to 100 of 154
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      = str_replace( 'www.', '', parse_url( $url, PHP_URL_HOST ) );
  $home_url_host = str_replace( 'www.', '', parse_url( home_url(), PHP_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 ( false !== strpos( home_url(), '://www.' ) && false === strpos( $url, '://www.' ) ) {
    $url = str_replace( '://', '://www.', $url );
  }

  // Strip 'www.' if it is present and shouldn't be.
  if ( false === strpos( 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.
  if ( ! $wp_rewrite->using_index_permalinks() ) {
    $url = str_replace( $wp_rewrite->index . '/', '', $url );
  }

  if ( false !== strpos( trailingslashit( $url ), home_url( '/' ) ) ) {
    // Chop off http://domain.com/[path].
    $url = str_replace( home_url(), '', $url );
  } else {
    // Chop off /path/to/blog.
    $home_path = parse_url( home_url( '/' ) );
    $home_path = isset( $home_path['path'] ) ? $home_path['path'] : '';
    $url       = preg_replace( sprintf( '#^%s#', preg_quote( $home_path ) ), '', trailingslashit( $url ) );
  }

  // Trim leading and lagging slashes.
  $url = trim( $url, '/' );

  $request              = $url;
  $post_type_query_vars = array();

  foreach ( get_post_types( array(), 'objects' ) as $post_type => $t ) {
    if ( ! empty( $t->query_var ) ) {
      $post_type_query_vars[ $t->query_var ] = $post_type;
    }
  }

  // Look for matches.
  $request_match = $request;
  foreach ( (array) $rewrite as $match => $query ) {

    // If the requesting file is the anchor of the match,
    // prepend it to the path info.
    if ( ! empty( $url ) && ( $url != $request ) && ( strpos( $match, $url ) === 0 ) ) {
 

 View on GitHub View on Trac