I want to set also text comment as required… but in the backend only email and author are possible.
Without using a javascript validation, how it is possible?
Thank you
1 Answer
You can hook into 'pre_comment_on_post'
and inspect $_POST['comment']
:
<?php # -*- coding: utf-8 -*-
/**
* Plugin Name: T5 Require comment text
* Description: Dies if there is no comment text.
* Plugin URI: http://wordpress.stackexchange.com/q/55679/73
* Version: 2012.07
* Author: Thomas Scholz
* Author URI: http://toscho.de
* License: MIT
* License URI: http://www.opensource.org/licenses/mit-license.php
*/
if ( ! function_exists( 't5_require_comment_text' ) )
{
add_action( 'pre_comment_on_post', 't5_require_comment_text' );
function t5_require_comment_text()
{
if ( ! isset ( $_POST['comment'] ) or '' === trim( $_POST['comment'] ) )
{
print 'Please hit your back button and write something useful.';
exit;
}
}
}
See wp-comments-post.php
for more actions and a way to create a useful back link. I didn’t include one because some browsers restore all filled form fields when the user clicks the back button. A link would prevent this rather useful behavior.