Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am in the process of creating a wordpress website, and I just have one issue left - RSS feeds.

Currently, the website has a .htaccess file blocking access to /category/* pages. It was part of a requirement that the site is not obviously Wordpress, and /category is a decent indicator of that.

Each category is fronted by a page, which displays the category content - so /category/news becomes /news.

However, RSS feeds always seem to point to /category/xxx/feed - is there a way to remove this /category/ part? So people could view the RSS feed at http://example.com/news/feed ?

I've tried fiddling around with the .htaccess file, but it's the one area of building a web application I'm not 100% sure on. Every search has returned with results of how to point a feed to Feedburner instead, which is not what I am looking to do at all.

share|improve this question
    
"It was part of a requirement that the site is not obviously Wordpress" Hopefully the people who made this a requirement understand that it has absolutely zero benefit in improving security, if that was the point. –  Milo Jun 25 '13 at 16:21
    
So /news is just a page, not category archive? Hm, I can't think of an easy way to move a category feed in such fashion on WP level... –  Rarst Jun 25 '13 at 21:28
    
@Milo: No, that was not the point. –  jrdn hannah Jun 26 '13 at 8:47
    
@Rarst Me either :( –  jrdn hannah Jun 26 '13 at 8:47
    
You do know that you can change the tag and category slug prefixes on the permalinks settings page, right? –  Bjørn Johansen Jun 28 '13 at 16:08
show 1 more comment

3 Answers

After some thought - this can be achieved with some creative abuse of existing feeds:

  • easy part - redirect category feeds to page comment feeds
  • hard part - make page comment feeds think they are category feeds

Something like this:

Category_Feed_At_Page::on_load();

/**
 * Repurpose page feeds for category of same name feeds.
 */
class Category_Feed_At_Page {

    static function on_load() {

        add_action( 'pre_get_posts', array( __CLASS__, 'pre_get_posts' ) );

        add_action( 'do_feed_rdf', array( __CLASS__, 'do_feed' ), 9 );
        add_action( 'do_feed_rss', array( __CLASS__, 'do_feed' ), 9 );
        add_action( 'do_feed_rss2', array( __CLASS__, 'do_feed' ), 9 );
        add_action( 'do_feed_atom', array( __CLASS__, 'do_feed' ), 9 );
    }

    /**
     * Change page's comment feed into category feed.
     *
     * @param WP_Query $query
     */
    static function pre_get_posts( $query ) {

        if ( $query->is_main_query() && $query->is_page() && $query->is_feed() ) {
            $name = $query->get( 'pagename' );

            require_once( ABSPATH . 'wp-admin/includes/taxonomy.php' );

            if ( category_exists( $name ) ) {
                $category = get_category_by_slug( $name );

                $query->set( 'category_name', $name );
                $query->set( 'cat', $category->term_id );
                $query->set( 'pagename', '' );

                $query->is_page         = false;
                $query->is_comment_feed = false;
                $query->is_category     = true;
                $query->is_singular     = false;


                remove_action( 'do_feed_rdf', array( __CLASS__, 'do_feed' ), 9 );
                remove_action( 'do_feed_rss', array( __CLASS__, 'do_feed' ), 9 );
                remove_action( 'do_feed_rss2', array( __CLASS__, 'do_feed' ), 9 );
                remove_action( 'do_feed_atom', array( __CLASS__, 'do_feed' ), 9 );

                remove_action( 'template_redirect', 'redirect_canonical' );
            }
        }
    }

    /**
     * Redirect real category feed to page feed.
     */
    static function do_feed() {

        if ( ! is_category() )
            return;

        $name = get_query_var( 'category_name' );
        $page = get_page_by_path( $name );

        if ( ! empty( $page ) ) {
            wp_safe_redirect( get_post_comments_feed_link( $page->ID ) );
            die;
        }
    }
}
share|improve this answer
    
Thanks Rarst, I will have a go with this and see how it works :) If it does the trick I will come back and mark this as answered. Cheers! –  jrdn hannah Jul 1 '13 at 9:50
add comment

You do know that you can change the tag and category slug prefixes on the permalinks settings page, right?

So if it bothers you to have the "category" in "/category/news", but accepts having "/cat/news", you can change that there.

share|improve this answer
    
Please do not use answers for discussion. You will be able to leave comments when you gain some reputation on site. (undeleted since made more answer-ish :) –  Rarst Jun 28 '13 at 16:11
add comment
up vote 0 down vote accepted

I found (in my opinion) the best and easiest way to do this, and thought I should leave it as an answer in case anybody stumbles upon this thread.

I downloaded and installed the WP No Category Base plugin and that did exactly what I needed.

Unfortunately, I never got around to trying the other answers (had to move on to a more urgent project), so I can't clarify if any of them work.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.