有朋自远方来,不亦乐乎。
wordpress解决comment_class或body_class输出暴露注册用户名的问题
发布于 4个月前 ( 02-13 ) 阅读: 368 次 抢沙发 A+

将以下代码放进functions.php。

/**
 * Change_comment_or_body_classes
 * @ For change username to nicename or userid
 * @ inlojv.com / Since Rainbow 1.0.8
 */
function inlojv_change_comment_or_body_classes($classes, $comment_id){
	global $wp_query;
	$comment = get_comment( $comment_id );	
	$user = get_userdata( $comment->user_id );
	$comment_author = 'comment-author-' . sanitize_html_class( $user->user_nicename, $comment->user_id );
	$author = $wp_query->get_queried_object();
	$archive_author = 'author-' . sanitize_html_class( $author->user_nicename, $author->ID );
	foreach( $classes as $key => $class ) {
		switch( $class ) {
			case $comment_author:
				// $classes[$key] = 'comment-author-' . sanitize_html_class( $comment->comment_author, $comment->user_id );
				$classes[$key] = 'comment-author-' . sanitize_html_class( $comment->user_id );
			break;			
			case $archive_author:
				// $classes[$key] = 'author-' . sanitize_html_class( get_the_author_meta( 'display_name' ), $author->ID );
				$classes[$key] = 'author-' . sanitize_html_class( $author->ID );
			break;
		}
	}	

    return $classes;
}
add_filter( 'comment_class', 'inlojv_change_comment_or_body_classes', 10, 4 );
add_filter( 'body_class', 'inlojv_change_comment_or_body_classes', 10, 4 );

代码来自苦行僧INLOJV,注释的那两行代码为昵称替换,可以不必理会,也可以不用复制粘帖。

该文章还没有任何评论,你来说两句吧.

发表评论

请稍候,正在加载新评论...