add_rewrite_rule example

/** * Adds REST rewrite rules. * * @since 4.4.0 * * @see add_rewrite_rule() * @global WP_Rewrite $wp_rewrite WordPress rewrite component. */
function rest_api_register_rewrites() {
    global $wp_rewrite;

    add_rewrite_rule( '^' . rest_get_url_prefix() . '/?$', 'index.php?rest_route=/', 'top' );
    add_rewrite_rule( '^' . rest_get_url_prefix() . '/(.*)?', 'index.php?rest_route=/$matches[1]', 'top' );
    add_rewrite_rule( '^' . $wp_rewrite->index . '/' . rest_get_url_prefix() . '/?$', 'index.php?rest_route=/', 'top' );
    add_rewrite_rule( '^' . $wp_rewrite->index . '/' . rest_get_url_prefix() . '/(.*)?', 'index.php?rest_route=/$matches[1]', 'top' );
}

/** * Registers the default REST API filters. * * Attached to the {@see 'rest_api_init'} action * to make testing and disabling these filters easier. * * @since 4.4.0 */
add_rewrite_tag( "%$this->name%", '([^/]+)', $this->query_var ? "{$this->query_var}=" : "post_type=$this->name&name=" );
            }

            if ( $this->has_archive ) {
                $archive_slug = true === $this->has_archive ? $this->rewrite['slug'] : $this->has_archive;
                if ( $this->rewrite['with_front'] ) {
                    $archive_slug = substr( $wp_rewrite->front, 1 ) . $archive_slug;
                } else {
                    $archive_slug = $wp_rewrite->root . $archive_slug;
                }

                add_rewrite_rule( "{$archive_slug}/?$", "index.php?post_type=$this->name", 'top' );
                if ( $this->rewrite['feeds'] && $wp_rewrite->feeds ) {
                    $feeds = '(' . trim( implode( '|', $wp_rewrite->feeds ) ) . ')';
                    add_rewrite_rule( "{$archive_slug}/feed/$feeds/?$", "index.php?post_type=$this->name" . '&feed=$matches[1]', 'top' );
                    add_rewrite_rule( "{$archive_slug}/$feeds/?$", "index.php?post_type=$this->name" . '&feed=$matches[1]', 'top' );
                }
                if ( $this->rewrite['pages'] ) {
                    add_rewrite_rule( "{$archive_slug}/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$", "index.php?post_type=$this->name" . '&paged=$matches[1]', 'top' );
                }
            }

            $permastruct_args         = $this->rewrite;
            
/** * Registers sitemap rewrite tags and routing rules. * * @since 5.5.0 */
    public function register_rewrites() {
        // Add rewrite tags.         add_rewrite_tag( '%sitemap%', '([^?]+)' );
        add_rewrite_tag( '%sitemap-subtype%', '([^?]+)' );

        // Register index route.         add_rewrite_rule( '^wp-sitemap\.xml$', 'index.php?sitemap=index', 'top' );

        // Register rewrites for the XSL stylesheet.         add_rewrite_tag( '%sitemap-stylesheet%', '([^?]+)' );
        add_rewrite_rule( '^wp-sitemap\.xsl$', 'index.php?sitemap-stylesheet=sitemap', 'top' );
        add_rewrite_rule( '^wp-sitemap-index\.xsl$', 'index.php?sitemap-stylesheet=index', 'top' );

        // Register routes for providers.         add_rewrite_rule(
            '^wp-sitemap-([a-z]+?)-([a-z\d_-]+?)-(\d+?)\.xml$',
            'index.php?sitemap=$matches[1]&sitemap-subtype=$matches[2]&paged=$matches[3]',
            'top'
        );
Home | Imprint | This part of the site doesn't use cookies.