WP Inline Comment Errors plug-in
Validation functions
Use these functions to check the value of a field and create your own custom error message when the field does not match your criteria. There is a function for each core comment form field and a generic filter for custom meta fields. Each function should check the $_POST value against some criteria to validate the user submitted data.
If the data is not valid, then create an error message and pass the error back to the filter. The plug-in stores the errors, which can be retrieved using the error information functions.
You can add these functions as filters in your functions.php or a comment-error-functions.php file that you include in the functions.php file.
Note that the plug-in will not save any comment if there is an error. At this point, you cannot use the plug-in to validate ‘optional’ fields. Any field that has a corresponding validation function is considered required.
wpice_validate_commentform_author
Description
Use this filter to create an error message if the author/name field does not match your criteria.
Parameters
none.
Return Value
(string) Your function must return the error message to the filter, so the plug-in can store the errors for later use.
Usage
add_filter('wpice_validate_commentform_author','func_name');
Example
/* validate the author */
add_filter('wpice_validate_commentform_author','is_author_valid');
function is_author_valid($error_msg = ''){
// check author field
if(!isset($_POST['author']) || trim($_POST['author']) == ''){
$error_msg = "Please include your name.";
}
return $error_msg;
}
Notes
If the ‘Comment author must fill out name and e-mail’ option in the Discussion Settings is checked (name and email are required), and you do not provide a validation function, then the plug-in will use its own internal validation and default error messages for these two fields. It uses the same validation as the wp-comments-post.php script. If you provide a validation function, then the plug-in will use your function instead of the internal validation and error message.
If you uncheck the ‘Comment author must fill out name and e-mail’ option in the Discussion Settings (name and email are not required), but still have validation functions for these fields, then the plug-in will continue to generate error messages for these fields and will not save any comments while there are errors. In this particular case the plug-in overrides the WordPress settings.
wpice_validate_commentform_email
Description
Use this filter to create an error message if the email field does not match your criteria.
Parameters
none.
Return Value
(string) Your function must return the error message to the filter, so the plug-in can store the errors for later use.
Usage
add_filter('wpice_validate_commentform_email','func_name');
Example
/* validate the email */
add_filter('wpice_validate_commentform_email','is_email_valid');
function is_email_valid($error_msg = ''){
// check valid email field using wordpress is_email function
if(!isset($_POST['email']) || !is_email($_POST['email'])){
$error_msg = "Please include a valid email address.";
}
return $error_msg;
}
Notes
See notes for the name/author field.
wpice_validate_commentform_url
Description
Use this filter to create an error message if the url/website field does not match your criteria.
Parameters
none.
Return Value
(string) Your function must return the error message to the filter, so the plug-in can store the errors for later use.
Usage
add_filter('wpice_validate_commentform_url','func_name');
Example
/* validate the url */
add_filter('wpice_validate_commentform_url','is_url_valid');
function is_url_valid($error_msg = ''){
// check url field
if(!isset($_POST['url']) ||
!preg_match("#^https?://.+#", $_POST['url'])){
$error_msg = "Please include a valid URL to your website.";
}
return $error_msg;
}
Notes
If you want the URL/website field to be required, then add the corresponding validation function and the plug-in will generate error messages for this field as well. The plug-in will display a required mark for the website/url field if you have a validation function and the req_mark_location
config_option set to ‘after-label’ or ‘after-field’ (ie. not ‘none’).
wpice_validate_commentform_comment
Description
Use this filter to create an error message if the comment field does not match your criteria.
Parameters
none.
Return Value
(string) Your function must return the error message to the filter, so the plug-in can store the errors for later use.
Usage
add_filter('wpice_validate_commentform_comment','func_name');
Example
/* validate the comment */
add_filter('wpice_validate_commentform_comment','is_comment_valid');
function is_comment_valid($error_msg = ''){
// check comment field
if(!isset($_POST['comment']) || trim($_POST['comment']) == ''){
$error_msg = "Please include a comment.";
}
return $error_msg;
}
Notes
The comment field is always required. If you do not provide a validation function for this field, then the plug-in will use its internal validation and default error messages.
wpice_comment_duplicate_trigger_filter
Description
Use this filter to create an error message if the comment field contains a duplicate comment. WordPress handles the validation, you do not need to provide any validation for this situation.
Parameters
none.
Return Value
(string) Your function must return the error message to the filter, so the plug-in can store the errors for later use.
Usage
add_filter('wpice_comment_duplicate_trigger_filter','name');
Example
/*
comment duplicate
generate message in case of duplicate comment
wp-comemnts-post.php will handle validation for this situation
*/
add_filter('wpice_comment_duplicate_trigger_filter','get_dup_msg');
function get_dup_msg($error_msg = ''){
$error_msg = "You've already posted this comment.
Please post a new comment.";
return $error_msg;
}
wpice_validate_commentform_metafield
Description
Use this function to validate and generate an error message for any required custom meta field. You can use this function to integrate error displays from a CAPTCHA plug-in or a ratings plug-in. See plug-in compatibility section for more information.
Parameters
(array). Filter passes an array of error messages to your custom function.
Return Value
(array) Your function must return an associative array of error messages to the filter. Note that this function uses an array unlike the others which use a string. The index of the array is the name of the meta field and the value is the error message for that field. The array holds error messages generated by other instances of this filter, in case you have multiple meta fields to validate.
$error_msgs['fieldname'] = 'your custom error message';
Usage
add_filter('wpice_validate_commentform_metafield','name');
Example
add_filter('wpice_validate_commentform_metafield','meta_validate');
function meta_validate($error_msgs){
$fieldname = 'meta_field_name'; // name of the field
if([your criteria...]){
$error_msgs[$fieldname] = 'your custom error message';
}
return $error_msgs; // must return array of error messages
}
Notes
The plug-in will generate errors for any specified meta field, but will not display any required mark for meta fields. You must modify the meta field HTML directly to show a required field mark.
Hi Hayden, I cannot see the filter for the message “Please correct the following problems:”.
I am very new to WordPress filters so I should say wrong stuff but I guess there is not a filter for that. Am i wrong?
At this time there is not a specific filter for the message header text you mention.
Instead you can set up code to build your own customized error message display with your own header.
Take a look at the code from print-customized-error-list.php in the examples folder of the plug-in. This is a simple code you add to functions.php. You can change the header message in this code or use it as the basis for your own error message.