WP Inline Comment Errors plug-in
Error information functions
Use these functions to retrieve error message information so you can customize the way your comment form displays errors.
wpice_get_comment_form_errors_as_list
Description
Use this function to get all of the comment form errors pre-formatted as an HTML list.
Parameter
(array) Associative array contains parameters to customize the HTML for the list. The index of the array is the parameter and the value of the array element is the value of the parameter.
'type'=> 'ul' | 'ol'
'class'=> 'comment-form-errors'
'single-err-selector' => 'comment-form-single-error'
'li-class-prefix' => 'comment-form-error-',
'before-list' => ''
'after-list' => ''
type
, defaults to ‘ul’
‘ul’ – For unordered list
‘ol’ – For ordered list.
class
, defaults to ‘comment-form-errors’
Value for the class attribute of the list tag.
single-err-selector
, defaults to ‘comment-form-error’
CSS selector added to class attribute if there is only one error. Use this for alternative formatting in case of single error.
li-class-prefix
, defaults to ‘comment-form-error-[fieldname]’
Value for the class attribute of li tags. The plug-in dynamically adds the field name to the selector.
before-list
, HTML to be added before the list.
after-list
, HTML to be added after the list.
Return value
(string) Returns an HTML formatted list of errors or empty string if no errors.
Usage
apply_filters('wpice_get_comment_form_errors_as_list',$args);
Example
/*
function to print custom error list
use the WordPress core 'comment_form_top' action
to print error list after form title and before first field
*/
// add html before list
$args = array(
'before-list' => '
Your comment has these errors:
‘
);
// get the formatted error list
$errs = apply_filters(‘wpice_get_comment_form_errors_as_list’,$args);
wpice_print_comment_form_error_list
Description
Prints the error list. Use one of the WordPress comment form action hooks to determine location to print error list. Uses the same error list configuration array as wpice_get_comment_form_errors_as_list.
Parameters
(array) Same as wpice_get_comment_form_errors_as_list.
Return value
None. Prints output of wpice_get_comment_form_errors_as_list at location specified by WordPress comment form action hooks.
Usage
do_action(‘wpice_print_comment_form_error_list’,$args);
Example
/*
function to print custom error list
use the WordPress core 'comment_form_before' action
to print error list before form title
*/
// turn off auto display of errors
$config = array(‘auto_display_errors’ => null);
do_action(‘wpice_set_config_options’,$config);
// set up core WordPress action
add_action(‘comment_form_before’, ‘print_comment_form_errors’);
function print_comment_form_errors(){
// add html before list
$args = array(
‘before-list’ => ‘
Your comment has these errors:
‘
);
do_action(‘wpice_print_comment_form_error_list’,$args);
}
wpice_field_has_error
Description
This function returns true if a given field name has a corresponding error and false if there is no error for the field name. For custom meta fields, the field name must correspond to the field name used in the error array of the meta field validation function. Use this function to create your own error indications.
Parameters
(string) Field name. For core fields the accepted field name values are ‘author’, ’email’, ‘url’, and ‘comment’. For custom meta fields, the field name must correspond to the field name used in the error array of the meta field validation function.
Return value
(boolean) Returns true if there is an error and false if not.
Usage
$has_err = apply_filters('wpice_field_has_error',[field name]);
Example
Determine if the author field has an error.
$author_error = apply_filters('wpice_field_has_error','author');
wpice_get_comment_form_error_array
Description
This function returns all errors in an associative array. The index of the array element is the field name and the value of the element is the error message for that field.
Parameters
none. Must pass ” empty string for second argument of WordPress core apply_filters
function.
Return value
(array). Returns an associative array of field names and corresponding error messages.
Usage
$errs = apply_filters('wpice_get_comment_form_error_array','');
Example
$errs = apply_filters('wpice_get_comment_form_error_array','');
print_r($errs);