For a recent project, I needed to inspect the data incoming comments before any of the other filtering on the data took place. Sounds like a big snore-fest, right? In WordPress, preprocess comment filtering is something that’s actually really easy to do, but the thing is that the documentation was lacking.
Anyway, I did actually find a hook that allowed me to do what I needed to do, though there was no official Codex page explaining exactly how to use the filter.
As such, here’s a break down of exactly what I was trying to do, and then a link to the of the Codex article I drafted after I ended up getting familiar with the hook.
WordPress: Preprocess Comment Filtering
In a project that I’ve been working on, I’ve been doing something similar to what I’ve done with Comment Images in that a user has the ability to attach a file to the comment prior to submitting it.
I’ll be talking more about this particular project later, but suffice it to say that there is a file size limitation.
So during the course of the project submission, I needed to be able to perform the following:
- Allow the user to fill out the comment form
- Give the user the ability to attach a file
- During submission, check the file size to make sure it did not exceed a certain size
- If the file size exceeded the limit, redirect back to the comment form with an error
- Otherwise, continue posting the comment
Straight forward enough, right?
Anyway, I hit a hiccup when I needed to actually check the file size and then redirect back to the original post with the comment data intact so that the user could opt to attach another, smaller file.
Unfortunately, none of the other filters with which I was familiar proved useful so I ended up digging through
comments.php until I discovered a hook for
It looked as if it was exactly what I needed in that it fired prior to any other comment processing hook; however, there was no documentation on it.
To that end, I did what any one would do – I setup a handler and then dumped out the incoming parameters to the screen. That’s the correct thing to do, right? .
After that, I had all of the information that I needed to actually preprocess my comment and continue on with working on the plugin.
Documentation For Preprocessing a Comment
Of course, my personal woes aren’t exactly the best example of how to work with the filter, and rather than describe how to do it here, I thought I’d go ahead and document the function in the WordPress Codex.
Though you can read the documentation in its entirety for more information, suffice it to say that the function takes in an array of data for the comment that allows you to inspect and manipulate prior to returning to the caller.
Naturally, within the context of the function, you also have the ability to take a look at the normal PHP arrays such as
$_FILES which can be really useful if you’re looking for something outside of just the comment form.
At any rate, there is an easy way to preprocess comments in WordPress. Just be sure to check out the documentation.