* Endpoint mask that matches nothing.
* Endpoint mask that matches post permalinks.
define( 'EP_PERMALINK', 1 );
* Endpoint mask that matches attachment permalinks.
define( 'EP_ATTACHMENT', 2 );
* Endpoint mask that matches any date archives.
* Endpoint mask that matches yearly archives.
* Endpoint mask that matches monthly archives.
define( 'EP_MONTH', 16 );
* Endpoint mask that matches daily archives.
* Endpoint mask that matches the site root.
* Endpoint mask that matches comment feeds.
define( 'EP_COMMENTS', 128 );
* Endpoint mask that matches searches.
* Note that this only matches a search at a "pretty" URL such as
* `/search/my-search-term`, not `?s=my-search-term`.
define( 'EP_SEARCH', 256 );
* Endpoint mask that matches category archives.
define( 'EP_CATEGORIES', 512 );
* Endpoint mask that matches tag archives.
define( 'EP_TAGS', 1024 );
* Endpoint mask that matches author archives.
define( 'EP_AUTHORS', 2048 );
* Endpoint mask that matches pages.
define( 'EP_PAGES', 4096 );
* Endpoint mask that matches all archive views.
define( 'EP_ALL_ARCHIVES', EP_DATE | EP_YEAR | EP_MONTH | EP_DAY | EP_CATEGORIES | EP_TAGS | EP_AUTHORS );
* Endpoint mask that matches everything.
define( 'EP_ALL', EP_PERMALINK | EP_ATTACHMENT | EP_ROOT | EP_COMMENTS | EP_SEARCH | EP_PAGES | EP_ALL_ARCHIVES );
* Adds a rewrite rule that transforms a URL structure to a set of query vars.
* Any value in the $after parameter that isn't 'bottom' will result in the rule
* being placed at the top of the rewrite rules.
* @since 4.4.0 Array support was added to the `$query` parameter.
* @global WP_Rewrite $wp_rewrite WordPress rewrite component.
* @param string $regex Regular expression to match request against.
* @param string|array $query The corresponding query vars for this rewrite rule.
* @param string $after Optional. Priority of the new rule. Accepts 'top'
* or 'bottom'. Default 'bottom'.
function add_rewrite_rule( $regex, $query, $after = 'bottom' ) {
$wp_rewrite->add_rule( $regex, $query, $after );
* Add a new rewrite tag (like %postname%).
* The `$query` parameter is optional. If it is omitted you must ensure that you call
* this on, or before, the {@see 'init'} hook. This is because `$query` defaults to
* `$tag=`, and for this to work a new query var has to be added.
* @global WP_Rewrite $wp_rewrite WordPress rewrite component.
* @global WP $wp Current WordPress environment instance.
* @param string $tag Name of the new rewrite tag.
* @param string $regex Regular expression to substitute the tag for in rewrite rules.
* @param string $query Optional. String to append to the rewritten query. Must end in '='. Default empty.
function add_rewrite_tag( $tag, $regex, $query = '' ) {
// Validate the tag's name.
if ( strlen( $tag ) < 3 || '%' !== $tag[0] || '%' !== $tag[ strlen( $tag ) - 1 ] ) {
$wp->add_query_var( $qv );
$wp_rewrite->add_rewrite_tag( $tag, $regex, $query );
* Removes an existing rewrite tag (like %postname%).
* @global WP_Rewrite $wp_rewrite WordPress rewrite component.
* @param string $tag Name of the rewrite tag.
function remove_rewrite_tag( $tag ) {
$wp_rewrite->remove_rewrite_tag( $tag );
* Add permalink structure.
* @see WP_Rewrite::add_permastruct()
* @global WP_Rewrite $wp_rewrite WordPress rewrite component.
* @param string $name Name for permalink structure.
* @param string $struct Permalink structure.
* @param array $args Optional. Arguments for building the rules from the permalink structure,
* see WP_Rewrite::add_permastruct() for full details. Default empty array.
function add_permastruct( $name, $struct, $args = array() ) {
// Back-compat for the old parameters: $with_front and $ep_mask.
if ( ! is_array( $args ) ) {
$args = array( 'with_front' => $args );
if ( func_num_args() == 4 ) {
$args['ep_mask'] = func_get_arg( 3 );
$wp_rewrite->add_permastruct( $name, $struct, $args );
* Removes a permalink structure.
* Can only be used to remove permastructs that were added using add_permastruct().
* Built-in permastructs cannot be removed.
* @see WP_Rewrite::remove_permastruct()
* @global WP_Rewrite $wp_rewrite WordPress rewrite component.
* @param string $name Name for permalink structure.
function remove_permastruct( $name ) {
$wp_rewrite->remove_permastruct( $name );
* Add a new feed type like /atom1/.
* @global WP_Rewrite $wp_rewrite WordPress rewrite component.
* @param string $feedname Feed name.
* @param callable $function Callback to run on feed display.
* @return string Feed action name.
function add_feed( $feedname, $function ) {
if ( ! in_array( $feedname, $wp_rewrite->feeds, true ) ) {
$wp_rewrite->feeds[] = $feedname;
$hook = 'do_feed_' . $feedname;
// Remove default function hook.
remove_action( $hook, $hook );
add_action( $hook, $function, 10, 2 );
* Remove rewrite rules and then recreate rewrite rules.
* @global WP_Rewrite $wp_rewrite WordPress rewrite component.
* @param bool $hard Whether to update .htaccess (hard flush) or just update
* rewrite_rules option (soft flush). Default is true (hard).
function flush_rewrite_rules( $hard = true ) {
if ( is_callable( array( $wp_rewrite, 'flush_rules' ) ) ) {
$wp_rewrite->flush_rules( $hard );
* Add an endpoint, like /trackback/.
* Adding an endpoint creates extra rewrite rules for each of the matching
* places specified by the provided bitmask. For example:
* add_rewrite_endpoint( 'json', EP_PERMALINK | EP_PAGES );
* will add a new rewrite rule ending with "json(/(.*))?/?$" for every permastruct
* that describes a permalink (post) or page. This is rewritten to "json=$match"
* where $match is the part of the URL matched by the endpoint regex (e.g. "foo" in
* "[permalink]/json/foo/").
* A new query var with the same name as the endpoint will also be created.
* When specifying $places ensure that you are using the EP_* constants (or a
* combination of them using the bitwise OR operator) as their values are not
* guaranteed to remain static (especially `EP_ALL`).
* Be sure to flush the rewrite rules - see flush_rewrite_rules() - when your plugin gets
* activated and deactivated.
* @since 4.3.0 Added support for skipping query var registration by passing `false` to `$query_var`.
* @global WP_Rewrite $wp_rewrite WordPress rewrite component.
* @param string $name Name of the endpoint.
* @param int $places Endpoint mask describing the places the endpoint should be added.
* @param string|bool $query_var Name of the corresponding query variable. Pass `false` to skip registering a query_var
* for this endpoint. Defaults to the value of `$name`.
function add_rewrite_endpoint( $name, $places, $query_var = true ) {
$wp_rewrite->add_endpoint( $name, $places, $query_var );
* Filters the URL base for taxonomies.
* To remove any manually prepended /index.php/.
* @param string $base The taxonomy base that we're going to filter
function _wp_filter_taxonomy_base( $base ) {
if ( ! empty( $base ) ) {
$base = preg_replace( '|^/index\.php/|', '', $base );
$base = trim( $base, '/' );
* Resolve numeric slugs that collide with date permalinks.
* Permalinks of posts with numeric slugs can sometimes look to WP_Query::parse_query()
* like a date archive, as when your permalink structure is `/%year%/%postname%/` and
* a post with post_name '05' has the URL `/2015/05/`.
* This function detects conflicts of this type and resolves them in favor of the
* Note that, since 4.3.0, wp_unique_post_slug() prevents the creation of post slugs
* that would result in a date archive conflict. The resolution performed in this
* function is primarily for legacy content, as well as cases when the admin has changed
* the site's permalink structure in a way that introduces URL conflicts.
* @param array $query_vars Optional. Query variables for setting up the loop, as determined in
* WP::parse_request(). Default empty array.
* @return array Returns the original array of query vars, with date/post conflicts resolved.
function wp_resolve_numeric_slug_conflicts( $query_vars = array() ) {
if ( ! isset( $query_vars['year'] ) && ! isset( $query_vars['monthnum'] ) && ! isset( $query_vars['day'] ) ) {
// Identify the 'postname' position in the permastruct array.
$permastructs = array_values( array_filter( explode( '/', get_option( 'permalink_structure' ) ) ) );
$postname_index = array_search( '%postname%', $permastructs, true );
if ( false === $postname_index ) {
* A numeric slug could be confused with a year, month, or day, depending on position. To account for
* the possibility of post pagination (eg 2015/2 for the second page of a post called '2015'), our
* `is_*` checks are generous: check for year-slug clashes when `is_year` *or* `is_month`, and check
* for month-slug clashes when `is_month` *or* `is_day`.
if ( 0 === $postname_index && ( isset( $query_vars['year'] ) || isset( $query_vars['monthnum'] ) ) ) {
} elseif ( $postname_index && '%year%' === $permastructs[ $postname_index - 1 ] && ( isset( $query_vars['monthnum'] ) || isset( $query_vars['day'] ) ) ) {
} elseif ( $postname_index && '%monthnum%' === $permastructs[ $postname_index - 1 ] && isset( $query_vars['day'] ) ) {
// This is the potentially clashing slug.
$value = $query_vars[ $compare ];
$post = get_page_by_path( $value, OBJECT, 'post' );
if ( ! ( $post instanceof WP_Post ) ) {
// If the date of the post doesn't match the date specified in the URL, resolve to the date archive.
if ( preg_match( '/^([0-9]{4})\-([0-9]{2})/', $post->post_date, $matches ) && isset( $query_vars['year'] ) && ( 'monthnum' === $compare || 'day' === $compare ) ) {
// $matches[1] is the year the post was published.
if ( (int) $query_vars['year'] !== (int) $matches[1] ) {
// $matches[2] is the month the post was published.
if ( 'day' === $compare && isset( $query_vars['monthnum'] ) && (int) $query_vars['monthnum'] !== (int) $matches[2] ) {
* If the located post contains nextpage pagination, then the URL chunk following postname may be
* intended as the page number. Verify that it's a valid page before resolving to it.
if ( 'year' === $compare && isset( $query_vars['monthnum'] ) ) {
$maybe_page = $query_vars['monthnum'];
} elseif ( 'monthnum' === $compare && isset( $query_vars['day'] ) ) {
$maybe_page = $query_vars['day'];
// Bug found in #11694 - 'page' was returning '/4'.
$maybe_page = (int) trim( $maybe_page, '/' );
$post_page_count = substr_count( $post->post_content, '<!--nextpage-->' ) + 1;
// If the post doesn't have multiple pages, but a 'page' candidate is found, resolve to the date archive.
if ( 1 === $post_page_count && $maybe_page ) {
// If the post has multiple pages and the 'page' number isn't valid, resolve to the date archive.
if ( $post_page_count > 1 && $maybe_page > $post_page_count ) {
// If we've gotten to this point, we have a slug/date clash. First, adjust for nextpage.
if ( '' !== $maybe_page ) {
$query_vars['page'] = (int) $maybe_page;
// Next, unset autodetected date-related query vars.
unset( $query_vars['year'] );
unset( $query_vars['monthnum'] );
unset( $query_vars['day'] );
// Then, set the identified post.
$query_vars['name'] = $post->post_name;
// Finally, return the modified query vars.
* Examine a URL and try to determine the post ID it represents.
* Checks are supposedly from the hosted site blog.
* @global WP_Rewrite $wp_rewrite WordPress rewrite component.
* @global WP $wp Current WordPress environment instance.
* @param string $url Permalink to check.
* @return int Post ID, or 0 on failure.
function url_to_postid( $url ) {
* Filters the URL to derive the post ID from.
* @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 ) );