워드프레스 댓글 폼 변경하기

이 글에서는 워드프레스 댓글 폼 변경 방법에 대해 살펴보겠습니다. 워드프레스에서는 후크(액션/필터)를 사용하여 댓글 폼을 변경하거나 comments.php 파일을 직접 수정하여 원하는 대로 바꿀 수 있습니다.

워드프레스 댓글 양식을 간결하게 바꾸고 싶은 경우 "워드프레스 댓글 폼을 간결하게 만들기"를 참고해보시기 바랍니다.

워드프레스 댓글 폼 변경하기

워드프레스 댓글 폼 변경하기

워드프레스에서 댓글 폼(코멘트 폼)을 출력하는 함수는 comment_form()입니다. 보통 comments.php 파일에 보면 다음과 비슷하게 사용됩니다.

<?php comment_form(); ?>

이 함수에 다양한 파라미터를 사용할 수 있습니다.

comment_form()에 사용 가능한 파라미터

fields

입력 필드: 'author', 'email', 'url'.

comment_field

코멘트 입력 필드의 textarea와 라벨

must_log_in

사용자가 로그인해야 하는 경우에 표시되는 텍스트

logged_in_as

사용자가 로그인한 경우에 표시되는 텍스트

comment_notes_before

사용자가 로그인하지 않은 경우 댓글 폼 앞에 표시되는 텍스트 또는 HTML

comment_notes_after

댓글 폼 뒤에 (제출하기 전에) 표시되는 텍스트 또는 HTML

comments.php 파일을 직접 수정할 경우에는 다양한 형태로 사용이 가능합니다.

예)

$comments_args = array(
// 전송 버튼 라벨 변경
'label_submit'=>'Send',
// 댓글 섹션의 라벨 변경
'title_reply'=>'Write a Reply or Comment',
// 코멘트 필드 후에 표시되는 텍스트나 HTML 삭제
'comment_notes_after' => '',
// 댓글 입력 필드(textarea) 서식 변경
'comment_field' => '<p class="comment-form-comment"><label for="comment">' . _x( 'Comment', 'noun' ) . '</label><br /><textarea id="comment" name="comment" aria-required="true"></textarea></p>',
);

comment_form($comments_args);

또는

comment_form(array('comment_notes_after' => ''));

형태로도 사용이 가능합니다.

필터 사용

comment_form_field_comment 필터와 comment_form_default_fields 필터(또는  comment_form_field_{$name})를 사용하는 것도 가능합니다.

예를 들어, "댓글 입력란"에 특정 문구를 넣고 싶은 경우

add_filter( 'comment_form_field_comment', 'my_comment_form_field_comment' );

function my_comment_form_field_comment( $comment_field ) {

$comment_field = '<p class="comment-form-comment"><textarea id="comment" name="comment" cols="45" rows="8" aria-required="true">여기에 의견을 입력해 주세요.</textarea></p>';

return $comment_field;
}
add prefilled text in comments wordpress

위의 함수를 함수 파일에 입력하면 댓글 폼은 다음과 같이 변경됩니다.

댓글 입력 필드에 미리 텍스트로 채우고자 할 때에 유용하게 사용할 수 있습니다.

그리고 기본 필드 중에 "url" 필드를 없애고 싶다면 다음과 같은 css 코드로 가능합니다.

#respond .comment-form-url {
display: none;
}

하지만 필터를 사용하여 없애는 것도 가능합니다.

add_filter('comment_form_default_fields',delete_url_field_fields');
function delete_url_field_fields($fields){
$fields['url'] = '';
return $fields;
}

action 사용

댓글 폼 전/후에 comment_form_before, comment_form_after 액션을 사용할 수 있습니다.

예를 들어 다음 함수를 함수 파일에 추가하면 댓글 폼 바로 이에 지정한 텍스트가 표시됩니다.

add_action( 'comment_form_before', 'my_pre_comment_text' );

function my_pre_comment_text() {
echo '<div class="mytext"><p>부적절한 댓글은 삭제될 수 있습니다.</p></div>';
}

추가

댓글 필드 순서 재정렬하기

다음 함수를 사용하면 댓글 입력란이 맨 위로 이동하게 됩니다.

add_filter( 'comment_form_defaults', 't5_move_textarea' );
add_action( 'comment_form_top', 't5_move_textarea' );

/**
* 기본 필드에서 textarea 코드를 가져다가 맨 위에 출력
*
* @param  array $input Default fields if called as filter
* @return string|void - string|void 반환
*/
function t5_move_textarea( $input = array () )
{
static $textarea = '';

if ( 'comment_form_defaults' === current_filter() )
{
// Copy the field to our internal variable … -- 필드를 내부 변수로 복사
$textarea = $input['comment_field'];
// … and remove it from the defaults array.  -- 기본 array에서 제거
$input['comment_field'] = '';
return $input;
}

print apply_filters( 'comment_form_field_comment', $textarea );
}
//Source: http://wordpress.stackexchange.com/ 

참고

일부 글에 제휴 링크가 포함될 수 있으며 파트너스 활동으로 일정액의 수수료를 받을 수 있습니다.

3개 댓글

    1. Loco Translate와 같은 플러그인을 사용하여 영문을 한글화할 수 있습니다.

      https://www.thewordcracker.com/basic/how-translate-wordpress-po-file-using-loco-translate/

      이 블로그에는 GenertePress 테마가 사용되었는데, GeneratePresss 테마의 경우 다음 글을 참고하여 댓글 섹션을 커스텀하고 문자열을 변경할 수 있습니다.

      https://www.thewordcracker.com/intermediate/generatepress-%ED%85%8C%EB%A7%88-%EB%8C%93%EA%B8%80-%EC%84%B9%EC%85%98-%EB%A0%88%EC%9D%B4%EC%95%84%EC%9B%83-%EB%B3%80%EA%B2%BD/

  1. 워드프레스 댓글에서 이메일이 필수이지만, 이메일과 URL을 제거하려면 다음 함수를 테마 함수 파일에 입력하고 CSS로 없애주면 됩니다.

    function remove_comment_fields($fields) {
        unset($fields['email']);
        unset($fields['url']);
        return $fields;
    }
    add_filter('comment_form_default_fields', 'remove_comment_fields');

wordcracker에 답글 남기기 응답 취소

* 이메일 정보는 공개되지 않습니다.