WordPress Page Spam

I’ve been getting spam to my page’s and pictures for years, but this last weekend I got 400+

Grrr, time to fight back.

The annoying thing is that both Media and Pages don’t have the ability for the Admin (out of the box) to say “no comments”, which is what I do to all my posts once I they get enough spam, and I don’t want to leave to open for active discussion.

So I found this support post with this code to stop Media comments:

add_filter( 'comments_open', 'no_media_comments', 10, 2 );

function no_media_comments( $open, $post_id ) {

	$post = get_post( $post_id );
// wordpress refers to images as attachments
	if ( 'attachment' == $post->post_type )
		$open = false;

	return $open;
}

I then found this page in the Codex with the ‘post->post_type’ defined and added another clause to block posts to Pages also and ended up with this:

function no_media_comments( $open, $post_id ) {

	$post = get_post( $post_id );
// wordpress refers to images as attachments
	if ( 'attachment' == $post->post_type )
		$open = false;
	if ( 'page' == $post->post_type )
		$open = false;

	return $open;
}

add_filter( 'comments_open', 'no_media_comments', 10, 2 );

Which I placed at the very bottom of my functions.php (Appearance -> Editor -> functions.php)