How to check if feed URL was requested?

You have not specified exactly when your code runs but you can hook into “request” to check the requested page:

add_filter( 'request', function( $request ){
    if( isset( $request['feed'] ) ){
        //This is a feed request
    }
    return $request;
});

When the requested page is a feed $request, which is an array of query variables, will contain an item called “feed” which is set with the name of the feed like “rss” for example.
https://codex.wordpress.org/Plugin_API/Filter_Reference/request

Leave a Comment