3

I'd like to completely disable RSS feeds on my WP website. Can someone please guide me on how to throw a 404.php page and set the headers to 404, when someone types in https://example.com/feed/

here is the code in functions.php:

function disable_feeds() {    
    global $wp_query;
    $wp_query->is_feed = false;
    $wp_query->set_404();
    status_header( 404 );
    nocache_headers();
    wp_die( __('No feed available,please visit our <a href="'. get_bloginfo('url') .'">homepage</a>!') );
}
 
add_action( 'do_feed',      'disable_feeds', 1 );
add_action( 'do_feed_rdf',  'disable_feeds', 1 );
add_action( 'do_feed_rss',  'disable_feeds', 1 );
add_action( 'do_feed_rss2', 'disable_feeds', 1 );
add_action( 'do_feed_atom', 'disable_feeds', 1 );

add_action( 'do_feed_rss2_comments', 'disable_feeds', 1 );
add_action( 'do_feed_atom_comments', 'disable_feeds', 1 );

add_action( 'feed_links_show_posts_feed',    '__return_false', 1 );
add_action( 'feed_links_show_comments_feed', '__return_false', 1 );

Strangely, I get a 500 Internal Server Error when typing https://example.com/feed/

no corresponding rules are set in .htaccess file Thanks in advance.

2 Answers 2

3

You need to define the HTTP Response code in wp_die function.

wp_die( __('No feed available,please visit our <a href="'. get_bloginfo('url') .'">homepage</a>!'), '', 404 );
2

If I want to bail out of my WP, I do it like this:

    $wp_query->set_404();
    status_header( 404 );
    get_template_part( 404 );
    exit();

I'm actually not sure, why you get a 500 error. I'd try to comment out the lines of your function one-by-one and see, where the error occurs.

6
  • Thanks Chris, I followed your instructions. This has resolved the Error 500. However, the browser keeps thinking it is still an RSS feed by not displaying the HTML page and prompting to open a RSS reader app.. Any ideas how fix that?
    – api2_go
    Commented Nov 2, 2020 at 5:27
  • Hm, I found this on the web, but looks pretty much the same, as you already have. function wpb_disable_feed() { wp_die( __('No feed available,please visit our <a href="'. get_bloginfo('url') .'">homepage</a>!') ); } add_action('do_feed', 'wpb_disable_feed', 1); add_action('do_feed_rdf', 'wpb_disable_feed', 1); add_action('do_feed_rss', 'wpb_disable_feed', 1); add_action('do_feed_rss2', 'wpb_disable_feed', 1); add_action('do_feed_atom', 'wpb_disable_feed', 1); add_action('do_feed_rss2_comments', 'wpb_disable_feed', 1); add_action('do_feed_atom_comments', 'wpb_disable_feed', 1); Commented Nov 2, 2020 at 10:46
  • 1
    Additionally, you can remove the header lines remove_action( 'wp_head', 'feed_links_extra', 3 ); remove_action( 'wp_head', 'feed_links', 2 ); Commented Nov 2, 2020 at 10:48
  • wpcaptain.com/blog/… This seems to be the most complete one... It even shows, how to block them via .htaccess . Commented Nov 2, 2020 at 10:50
  • thanks very much for the link. I saw it before. However, that solution uses a redirect to home url, and it will generate a 301 header, not 404. Do you know if it will have any impact on SEO and search engine crawlers? I've read somewhere it is better not to use a redirect... I guess, I will have to restrict the access via .htaccess as per the solution above
    – api2_go
    Commented Nov 3, 2020 at 5:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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