1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

コメント関連機能を無効にする

Posted at

WordPressをブログとしてよりも,組織などのウェブサイトとして使う際は,必要でないコメント機能を初めから無効にしておいた方が,スパム・コメントに悩まされずに済みます.

管理画面のコメント機能を無効にする

stinc/src/basic/antispam.php
function disable_comment() {
	remove_post_type_support( 'post', 'comments' );
	remove_post_type_support( 'page', 'comments' );

	add_filter( 'comments_open', '__return_false' );
	add_filter( 'comments_array', '__return_empty_array' );
	add_filter( 'comment_reply_link', '__return_false' );
	add_filter( 'comments_rewrite_rules', '__return_empty_array' );

	$counts = wp_count_comments();
	$sum = 0;
	foreach ( $counts as $key => $val ) {
		$sum += $val;
	}
	if ( $sum === 0 ) {
		add_action( 'admin_menu', function () {
			remove_menu_page( 'edit-comments.php' );
		} );
		add_action( 'admin_bar_menu', function ( $wp_admin_bar ) {
			$wp_admin_bar->remove_menu( 'comments' );
		}, 300 );
	}
}

コメント・フィード機能を無効にする

stinc/src/basic/antispam.php
function disable_comment_feed() {
	remove_theme_support( 'automatic-feed-links' );

	add_filter( 'feed_links_show_comments_feed', '__return_false' );
	add_filter( 'post_comments_feed_link_html', '__return_empty_string' );
	add_filter( 'post_comments_feed_link', '__return_empty_string' );
	add_filter( 'feed_link', function ( $output ) {
		return ( false === strpos( $output, 'comments' ) ) ? $output : '';
	} );
	remove_action( 'do_feed_rss2', 'do_feed_rss2' );
	remove_action( 'do_feed_atom', 'do_feed_atom' );
	add_action( 'do_feed_rss2', function ( $for_comments ) {
		if ( !$for_comments ) load_template( ABSPATH . WPINC . '/feed-rss2.php' );
	} );
	add_action( 'do_feed_atom', function ( $for_comments ) {
		if ( !$for_comments ) load_template( ABSPATH . WPINC . '/feed-atom.php' );
	} );
}

トラックバック機能を無効にする

stinc/src/basic/antispam.php
function disable_trackback() {
	remove_post_type_support( 'post', 'trackbacks' );
	remove_post_type_support( 'page', 'trackbacks' );

	add_filter( 'pings_open', '__return_false' );
	add_filter( 'site_url', function ( $url, $path, $scheme, $blog_id ) {
		return ( false === strpos( $path, 'xmlrpc.php' ) ) ? $url : '';
	}, 10, 4 );
	add_action( 'template_redirect', function () {
		if ( is_trackback() ) {
			global $wp_query;
			$wp_query->set_404();
			status_header( 404 );
			nocache_headers();
		}
	} );
}
1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?