I am developing a swear filter plugin for my blog. It is supposed to deny submissions containing any words in a text file called swears.txt
. Here is the code I am using for the comment form on single post page:
<?php
if (isset($_POST['submit'])) {
echo 'Submitted'; // for debugging purposes
$swearsFile = 'swears.txt';
$swearsCntnt = file_get_contents($swearsFile);
$swearList = explode("\n", $swearsCntnt);
$swearWords = "/";
for ($i = 0; $i < count($swearList); ++$i) {
if ($i == 0)
$swearWords .= rtrim($swearList[$i]);
else
$swearWords .= "|" . rtrim($swearList[$i]);
}
$swearWords .= "/i";
if (preg_match($swearWords, $_POST['comment'])) {
wp_die( __( '<strong>ERROR</strong>: You have entered forbidden text. Please consider revising.' ), 200 );
}
}
?>
The problem is that nothing happens after I press the Post Comment button except that the submitted comment appears as a new one. I am using an echo to verify that the Post Comment button has been pressed whether or not its following statements return true, yet nothing happens. How do I get WordPress to acknowledge if (isset($_POST['submit']))
?
I am developing a swear filter plugin for my blog. It is supposed to deny submissions containing any words in a text file called swears.txt
. Here is the code I am using for the comment form on single post page:
<?php
if (isset($_POST['submit'])) {
echo 'Submitted'; // for debugging purposes
$swearsFile = 'swears.txt';
$swearsCntnt = file_get_contents($swearsFile);
$swearList = explode("\n", $swearsCntnt);
$swearWords = "/";
for ($i = 0; $i < count($swearList); ++$i) {
if ($i == 0)
$swearWords .= rtrim($swearList[$i]);
else
$swearWords .= "|" . rtrim($swearList[$i]);
}
$swearWords .= "/i";
if (preg_match($swearWords, $_POST['comment'])) {
wp_die( __( '<strong>ERROR</strong>: You have entered forbidden text. Please consider revising.' ), 200 );
}
}
?>
The problem is that nothing happens after I press the Post Comment button except that the submitted comment appears as a new one. I am using an echo to verify that the Post Comment button has been pressed whether or not its following statements return true, yet nothing happens. How do I get WordPress to acknowledge if (isset($_POST['submit']))
?
3 Answers
Reset to default 1This might not directly answer your question (because I haven't even thought about why the $_POST['submit']
would be ignored or if it is even correct), but you should use filters/actions whenever possible.
The correct filter for filtering comment text is pre_comment_content
, and you should use it like this:
function my_filter_comment_for_swear_words( $comment_content ) {
// Verify if $comment_content contains swear words here
// return $comment_content if it doesn't contain forbidden words
// or wp_die if it does
}
add_filter( 'pre_comment_content', 'my_filter_comment_for_swear_words' );
This code would, as usual, go into your functions.php
file or into your custom plugin (depending on what your setup is).
It is not being TRUE because the action url of the comment form points to the wp-comments-post.php file at the root of the site which handle the submission. Once the submitting was successfully processed at that file the user is redirected back to the post in which the submit was done.
As the other answers had said, you are doing it wrong and you should use the available comment handling hooks to trigger the execution of your code (if at all it is needed)
I don´t want to stop you, but what you are doing here is WordPress standard functionality. Have a look at
Settings -> Discussion
where you will find a textarea where you can enter words which trigger a comment to need moderation.
Besides that you are probably going the wrong way. You should not just "wait" for a submit, but create a hook at the relevant places. You could use comment_post
to trigger a function when a comment is posted.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745259483a4619144.html
评论列表(0条)