I am developing a theme (it is locally hosted) in WordPress. I am attempting to create functionality where commenters cannot impersonate real administrators by setting their names to "admin", "moderator", or anything of that nature. My function takes the value of a custom setting I created with the WordPress settings API (it's called 'blocked_comment_names') in the form of CSV which looks something like:"admin, moderator, etc.", turns it into an array, and checks the commenters name if it contains any value in the array. The problem is, when I test this out on the front end, no matter the commenter name, I will always get a default error message of "Duplicate comment detected, it looks as though you've already said that". I have spent many hours testing alternatives but either the function will not catch any invalid commenter names or it will block every comment and somehow default to the duplicate comment error message. I tested this function out on a page comparing the setting's values to a string and it worked perfectly, and have created other comment filters which have worked fine, the problem seems to only occur when I mix both worlds. Here is my function (in functions.php):
<?php
function require_comment_name($fields) {
$commenter_name = $fields['comment_author'];
$csvstring = get_option( 'blocked_comment_names' );
$blockednames = str_getcsv($csvstring, ',' );
$strlower_comment_name = strtolower($comment_name);
foreach ($blockednames as $blockedname) {
if (strpos($strlower_comment_name, $blockedname) !== false) {
wp_die('<strong>Error</strong>: Please enter a valid name.<br><a href="javascript:history.back()">« Back</a>');
return $fields;
}
}
}
add_filter('preprocess_comment', 'require_comment_name');
?>
Here is another variation of the function that is also not working:
<?php
function require_comment_name($fields) {
$commenter_name = $fields['comment_author'];
$csvstring = get_option( 'blocked_comment_names' );
$blockednames = str_getcsv($csvstring, ',' );
$strlower_comment_name = strtolower($comment_name);
$commentblocked = false;
foreach ($blockednames as $blockedname) {
if (strpos($strlower_comment_name, $blockedname) !== false) {
$commentblocked = true;
}
}
if ($commentblocked == true) {
wp_die('<strong>Error</strong>: Please enter a valid name.<br><a href="javascript:history.back()">« Back</a>');
return $fields;
}
}
add_filter('preprocess_comment', 'require_comment_name');
?>
Any help would be greatly appreciated, please let me know if there is anything I could add to supply more information.
Aucun commentaire:
Enregistrer un commentaire