• Hi all,

    I would like to prevent people posting comments with the nickname of “admin”. My comments are no longer moderated as it was driving me mad, so I need some kind of validation stuff like the above.

    How do i go about doing that?

Viewing 4 replies - 1 through 4 (of 4 total)
  • There is a hook named pre_comment_author_name that is applied to the comment author name before it is saved to the database. You could create a filter function and change the name there.

    Thread Starter jimmyt1988

    (@jimmyt1988)

    Can I have an example.

    My current function code is:

    <?php
        function mytheme_comment($comment, $args, $depth) {
           $GLOBALS['comment'] = $comment; ?>
           <li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
             <div id="comment-<?php comment_ID(); ?>">
              <div class="comment-author vcard">
                 <?php //echo get_avatar($comment,$size='20',$default='<path_to_url>' ); ?>
    
                 <?php printf(__('<cite class="fn">%s</cite> <span class="says">says:</span>'), get_comment_author_link()) ?>
              </div>
              <?php if ($comment->comment_approved == '0') : ?>
                 <em><?php _e('Your comment is awaiting moderation.') ?></em>
                 <br />
              <?php endif; ?>
    
              <div class="comment-meta commentmetadata"><?php printf(__('%1$s - %2$s'), get_comment_date(),  get_comment_time()) ?><?php edit_comment_link(__('(Edit)'),'  ','') ?></div>
    
              <?php comment_text() ?>
              <div class="reply">
                 <?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
              </div>
              <div class = "commentSeperateLine"></div>
    
             </div>
        <?php
        }

    it is loaded with a callback:

    <?php wp_list_comments('type=comment&callback=mytheme_comment'); ?>

    The code below is UNTESTED, but it should be close. It will change the comment author name from ‘admin’ to ‘Guest’. Put it in your functions.php, or anywhere that is called before saving a comment.

    function my_comment_author_name_filter($name='Guest'){
      if ( $name == 'admin') $name = 'Guest';
      return $name;
     }  
    
    add_filter('pre_comment_author_name','my_comment_author_name_filter');

    Probably a little better to check if the current user really is an admin:

    function my_comment_author_name_filter($name='Guest'){
      if ( $name == 'admin' && !current_user_can('administrator') ) $name = 'Guest';
      return $name;
     }  
    
    add_filter('pre_comment_author_name','my_comment_author_name_filter');
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘prevent comment nickname being “admin”’ is closed to new replies.