bbPress::add_rewrite_rules()

Add Forums-specific rewrite rules for uri’s that are not setup for us by way of custom post types or taxonomies. This includes: – Front-end editing – Topic views – User profiles

Description

Source

File: bp-forums/classes/class-bbpress.php

    public static function add_rewrite_rules() {

        /** Setup *************************************************************/

        // Add rules to top or bottom?
        $priority           = 'top';

        // Single Slugs
        $forum_slug         = bbp_get_forum_slug();
        $topic_slug         = bbp_get_topic_slug();
        $reply_slug         = bbp_get_reply_slug();
        $ttag_slug          = bbp_get_topic_tag_tax_slug();

        // Archive Slugs
        $view_slug          = bbp_get_view_slug();
        $search_slug        = bbp_get_search_slug();
        $topic_archive_slug = bbp_get_topic_archive_slug();
        $reply_archive_slug = bbp_get_reply_archive_slug();

        // Tertiary Slugs
        $feed_slug          = 'feed';
        $edit_slug          = 'edit';
        $paged_slug         = bbp_get_paged_slug();

        // Unique rewrite ID's
        $feed_id            = 'feed';
        $edit_id            = bbp_get_edit_rewrite_id();
        $view_id            = bbp_get_view_rewrite_id();
        $paged_id           = bbp_get_paged_rewrite_id();
        $search_id          = bbp_get_search_rewrite_id();
        $user_id            = bbp_get_user_rewrite_id();
        $user_favs_id       = bbp_get_user_favorites_rewrite_id();
        $user_subs_id       = bbp_get_user_subscriptions_rewrite_id();
        $user_tops_id       = bbp_get_user_topics_rewrite_id();
        $user_reps_id       = bbp_get_user_replies_rewrite_id();

        // Rewrite rule matches used repeatedly below
        $root_rule    = '/([^/]+)/?$';
        $feed_rule    = '/([^/]+)/' . $feed_slug  . '/?$';
        $edit_rule    = '/([^/]+)/' . $edit_slug  . '/?$';
        $paged_rule   = '/([^/]+)/' . $paged_slug . '/?([0-9]{1,})/?$';

        // Search rules (without slug check)
        $search_root_rule  = '/?$';
        $search_paged_rule = '/' . $paged_slug . '/?([0-9]{1,})/?$';

        /** Add ***************************************************************/

        // Edit Forum|Topic|Reply|Topic-tag
        add_rewrite_rule( $forum_slug . $edit_rule, 'index.php?' . bbp_get_forum_post_type()  . '=$matches[1]&' . $edit_id . '=1', $priority );
        add_rewrite_rule( $topic_slug . $edit_rule, 'index.php?' . bbp_get_topic_post_type()  . '=$matches[1]&' . $edit_id . '=1', $priority );
        add_rewrite_rule( $reply_slug . $edit_rule, 'index.php?' . bbp_get_reply_post_type()  . '=$matches[1]&' . $edit_id . '=1', $priority );
        add_rewrite_rule( $ttag_slug  . $edit_rule, 'index.php?' . bbp_get_topic_tag_tax_id() . '=$matches[1]&' . $edit_id . '=1', $priority );

        // Topic-View Pagination|Feed|View
        add_rewrite_rule( $view_slug . $paged_rule, 'index.php?' . $view_id . '=$matches[1]&' . $paged_id . '=$matches[2]', $priority );
        add_rewrite_rule( $view_slug . $feed_rule,  'index.php?' . $view_id . '=$matches[1]&' . $feed_id  . '=$matches[2]', $priority );
        add_rewrite_rule( $view_slug . $root_rule,  'index.php?' . $view_id . '=$matches[1]',                               $priority );

        // Search All
        add_rewrite_rule( $search_slug . $search_paged_rule, 'index.php?' . $paged_id .'=$matches[1]', $priority );
        add_rewrite_rule( $search_slug . $search_root_rule,  'index.php?' . $search_id,                $priority );
    }

Changelog

Changelog
Version Description
bbPress (r2688) Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.