最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - Completely disable RSS feeds on WP 5.5.2 and show a 404 page instead of die();

programmeradmin0浏览0评论

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 /

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 /

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

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/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/feed/

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

Share Improve this question edited Oct 30, 2020 at 7:02 api2_go asked Oct 30, 2020 at 6:16 api2_goapi2_go 314 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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 );

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.

发布评论

评论列表(0)

  1. 暂无评论