2017-07-24 50 views

回答

1

以下代码a)显示'POST'表单提交按钮后的输入字段,该按钮提供显示首字母而不是全名的选项,b)在数据库中添加commentmeta字段以显示信息,以及c)检索元并显示全名或首字母缩写。

/* Add an input field for posting comment form with the switch to show initials instead of full name */ 
function add_comment_stealth_input_formfield(){ 
    echo '<input type="checkbox" name="stealth_comment" id="stealth_comment" value="0">'; 
    echo __("I want only my initials to be displayed instead of my name","comments"); 
} 
add_action('comment_form', 'add_comment_stealth_input_formfield'); 

/* Hide comment author name */ 
if(!function_exists("disable_comment_author_links")){ 
    function abbreviate_authorname_in_comments($author_link){ 

     $sw_initials = get_comment_meta(get_comment_ID(), 'sw_comment_initials',true); 

     if ($sw_initials == 1){ 
      // remove any html tags 
      $fullname = strip_tags($author_link); 

      // explode with ' ' 
      $separate = explode(" ", $fullname); 

      // make initials 
      $shrt = ""; 
      foreach ($separate as $w){ 
       $shrt .= strtoupper($w[0]."."); 
      } 

      // if more than 3 initials then something is wrong. Hide the last ones to be safe 
      if (strlen($shrt) > 6) 
       $shrt = substr($shrt, 0, 6); 

      return $shrt; 
     } else { 
      return $author_link; 
     } 
    } 
    add_filter('get_comment_author_link', 'abbreviate_authorname_in_comments'); 
} 

/* add a comment meta per comment */ 
function add_comment_field_initials_sw($comment_id) { 
    $isChecked = is_null($_REQUEST['stealth_comment']) ? 0:1; 
    add_comment_meta($comment_id, 'sw_comment_initials', $isChecked); 
} 
add_action('comment_post', 'add_comment_field_initials_sw');