Switch off comments

Switch off comments

4 minutes, 0 seconds Read

Only switch off the individual comments

An alternative version of Mor7ifer’s answer, this one only the Per-Post switches comment feeds, and not the site-wide comments feed (by checking is_single() Before you do it wp_die()).

It also uses the 410 Error code which means GONE And seems suitable for this use case (at least for me, where these URLs already exist and are indexed, and I want to tell Google and friends unambiguously to stop indexing).

function wpse45941_disable_feed( $comments ) {
    if( $comments and is_single() ) {
        wp_die( 'Feeds for comments on single posts have been disabled.', 410 );
    }
}
add_action('do_feed', 'wpse45941_disable_feed',1 );
add_action('do_feed_rdf', 'wpse45941_disable_feed',1 );
add_action('do_feed_rss', 'wpse45941_disable_feed',1 );
add_action('do_feed_rss2', 'wpse45941_disable_feed',1 );
add_action('do_feed_atom', 'wpse45941_disable_feed',1 );

Switch off all comments feeds

Here is the same code with the 410 HTTP status, but it will also switch off the SiteWide remark feed (and show a suitable message):

function wpse45941_disable_feed( $comments ) {
    if( $comments ) {
        wp_die( 'Feeds for comments have been disabled.', 410 );
    }
}
add_action('do_feed', 'wpse45941_disable_feed',1 );
add_action('do_feed_rdf', 'wpse45941_disable_feed',1 );
add_action('do_feed_rss', 'wpse45941_disable_feed',1 );
add_action('do_feed_rss2', 'wpse45941_disable_feed',1 );
add_action('do_feed_atom', 'wpse45941_disable_feed',1 );

Remove Per-Post Comment-Voedings URLs from the header

The above code ensures that nobody has access to the reactions -Urls, but they can still be inserted in the header of a few posts! Publishing these URLs when they do not solve is a terrible SEO error.

By adding a filter post_comments_feed_link We can ensure that the per-post-commentary food URLs never get output, you can see my version below.

There is a problem with this first filter: it ensures that RSS becomes invalid! Because of A bug in Core WP (Trac Ticket)when the post_comments_feed_link Is empty back, the RSS feed still prevents the tag (), but empty, and this can cause the following error when validating the feed:

There is a solution for this, but it is very annoying and ugly, imo, that’s why I think post_comments_feed_link.

Anyway, here are the three filters that, together and for the time being, remove the per-post-commentary-feed URL without causing a validation problem with your RSS:


/**
 * Filter get_post_comments_feed_link() to disable per-post comment feeds entirely
 * 
 * There are two expected effects: 
 *  Disable the automatic insertion of a  tag in the header of single post URLs
 *      i.e. 
 *  Remove the feed URL for each post from regular site feeds
 *      i.e. example.com/posturl/feed/
 * 
 * NOTE: This doesn't disable the sitewide comments feed being inserted 
 * NOTE: On its own, this triggers a bug in core, and  is output with no URL which makes feeds technically invalid. For now we also need the filters on get_comments_number() and comments_open() to be able to disable the RSS link without errors.
 * 
 * @link https://core.trac.wordpress.org/ticket/63843 Trac report about how on its own this triggers bugs
 * @param string $for_comments
 * @return string
 */
function wpse45941_disable_post_comment_feeds_filter_post_comments_feed_link($for_comments) {
  
  return '';
}
add_filter('post_comments_feed_link', 'wpse45941_disable_post_comment_feeds_filter_post_comments_feed_link');

/**
 * Filter get_comments_number() to always return zero in RSS so we don't insert post comment feeds in normal RSS
 * 
 * Trick feed-rss2.php into not inserting  with empty value. 
 * 
 * @link https://core.trac.wordpress.org/ticket/63843 Bug report on why this is necessary
 * @param int $comments_number
 * @param int $post_id
 * @return void
 */
function wpse45941_disable_post_comment_feeds_filter_get_comments_number(int $comments_number, int $post_id) {

    if (!is_feed()) {
        return $comments_number;
    }

    return 0;
}
add_filter('get_comments_number', 'wpse45941_disable_post_comment_feeds_filter_get_comments_number', 10, 2);

/**
 * Filter comments_open() to always return false in RSS so we don't insert post comment feeds in normal RSS
 *
 * Trick feed-rss2.php into not inserting  with empty value. 
 * 
 * @link https://core.trac.wordpress.org/ticket/63843 Bug report on why this is necessary
 * @param bool $comments_open
 * @param int $post_id
 * @return void
 */
function wpse45941_disable_post_comment_feeds_filter_comments_open( bool $comments_open, int $post_id ) {

    if (!is_feed()) {
        return $comments_open;
    }

    return false;
}
add_filter('comments_open', 'wpse45941_disable_post_comment_feeds_filter_comments_open', 10, 2);

This code should work in an adjusted plug -in (where I placed it) or functions.php. If you use this, test carefully and make sure you validate your feed afterwards https://validator.w3.org/feed/

You must also check in on the Trac -Bug report and see if it has been resolved. If so, you may only need the first filter above, wpse45941_disable_post_comment_feeds_filter_post_comments_feed_link!

#Switch #comments

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *