WP Inline Comment Errors plug-in
About sessions, cookies and error template
The plug-in uses a session variable to store the comment HTTP POST data and error messages so these values are accessible after the plug-in redirects to the page with the comment form. Sessions are an idiomatic way for PHP to store information across scripts, which is required for the plug-in to provide a seamless experience.
Sessions can be handled with cookies or by passing the session ID as a query string value in the URL through the HTTP GET request. While sessions can work without cookies, there are hosting companies that configure PHP to only support cookie based sessions. So, it is possible that if a user visiting your site has their web browser set up to block all cookies and your server only supports cookie based sessions then the plug-in will not be able to make the HTTP POST data and error messages available on the page with the comment form.
In this case the plug-in will ‘fall back’ to displaying errors in a custom error template or the WordPress error display.
After installing the plug-in you can view the plug-in settings page to determine whether your server’s PHP configuration supports cookie only sessions and decide if you want to add a custom template to display comment errors for people with cookies disabled.
The plug-in will first look for a file based upon the value of the ‘error_template_path’ configuration variable. This would be a path you specify to your own custom error template. If you did not set this value or it cannot find this file then it will attempt to find the default file ‘wp-comment-error-template.php’. If it cannot find the default file then it will display the errors using the WordPress error display screen.
Create an error template
One approach to making an error template is to resave your current 404.php template using the default comment error file name ‘wp-comment-error-template.php’ and save it in your main theme directory. Add the following code to the main body area of the template file.
print "
<h1">Comment Error
“; // print the page title
// call template tag to print the error list
apply_filters(‘wpice_print_comment_form_error_list’,”);
print ‘
Return to the post ‘ .
get_the_title($comment_post_id) .
‘ and try your comment again.
‘;
This code prints the error list and provides a link back to the post or page with the comment form. The plug-in makes the $comment_post_id
variable available to the template. This contains the post_id
of the post or page that the user was attempting to comment on. You can use this post_id
with WordPress template tags to create your custom error message page and ensure that users with cookies disabled will at least see the error messages on a page within your web site with an easy link back to the page.
Refer to the sample error template in the examples folder of the plug-in.
Testing the template
If your server only supports cookie based sessions, then you can set your browser to block all cookies, submit an empty comment to one of your blog posts, and see the output of your template.
If your server supports GET based sessions but you still want to test out the template, in case you are moving the site to a server with a different configuration, then you can add the following code to your functions.php file to reconfigure PHP to support cookie only sessions.
// cookie only session, remove after testing
ini_set('session.use_only_cookies','1');
Turn off cookies in your browser and submit an empty comment to a post to test the template coding. Be sure to remove this code from your functions.php file after you are done testing.