포스트에 Shortcode 사용시 자동으로 만들어지는 태그를 제거하는 방법

포스트나 페이지에 숏코드(Shortcode)로 편집을 하다보면, <p>태그가 자동으로 입력이 된다거나, <br> 태그 등이 자동으로 삽입되어 레이아웃이 깨지거나 틀어지는 경우가 있습니다. 이럴 경우 아래와 같은 방법을 사용하면 원하지 않는 태그가 삽입되는 것을 방지할 수 있습니다.

우선 아래의 코드를 테마 폴더안에 있는 functions.php 파일에 붙여넣습니다.

// Disable WordPress automatic formatting on posts using a shortcode
function my_formatter($content) {
$new_content = '';
$pattern_full = '{([raw].*?[/raw])}is';
$pattern_contents = '{[raw](.*?)[/raw]}is';
$pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE);

foreach ($pieces as $piece) {
if (preg_match($pattern_contents, $piece, $matches)) {
$new_content .= $matches[1];
} else {
$new_content .= wptexturize(wpautop($piece));
}
}

return $new_content;
}

remove_filter('the_content', 'wpautop');
remove_filter('the_content', 'wptexturize');

add_filter('the_content', 'my_formatter', 99);

 

그런 후 포스트를 작성할 때 숏코드를 아래와 같이 [raw][/raw]로 감싸주면 해결됩니다.

[php][raw]숏코드나 원하지 않는 태그가 들어가는 영역[/raw][/php]