programing

문자열에서 워드프레스 쇼트코드 실행

codeshow 2023. 9. 24. 13:12
반응형

문자열에서 워드프레스 쇼트코드 실행

나는 "이것은 [any shortcode1]을 가진 문자열이고 이것은 [any shortcode2]"와 같은 짧은 코드를 포함할 수 있는 문자열을 가지고 있습니다.나는 그들이 있는 곳에 숏코드가 실행되는 문자열을 보여주고 싶습니다.이 줄을 메아리칠 때 쇼트코드가 되지 않고 괄호 안에 남겨서 출력합니다.다음과 같은 코드를 사용하여 이를 해결하려고 했습니다.

$string_with_shortcodes = "this is a string with [anyshortcode1] and this is [anyshortcode2]";
$pattern = get_shortcode_regex();
preg_match_all("/$pattern/",$string_with_shortcodes, $matches);
foreach ($matches[0] as $short) {
    $string_with_shortcodes = str_replace($short, 'do_shortcode("' . $short . '")', $string_with_shortcodes);
}
eval("\$string_with_shortcodes = \"$string_with_shortcodes\";");
echo $string_with_shortcodes;

이렇게 하면 문자열 교체는 성공적으로 수행되지만 eval 함수에 오류가 발생합니다.

구문 오류: 구문 오류, 예기치 않은 T_STRING

제가 eval 기능을 잘못 사용하고 있습니까?아니면 문자열에서 쇼트 코드를 실행하는 더 간단한 방법이 있습니까?

실행하기 위해서는shortcode문자열 내에서 사용해야 합니다.do_shortcode()기능.

사용 방법은 다음과 같습니다.

$string_with_shortcodes = "this is a string with [anyshortcode1] and this is [anyshortcode2]";
echo do_shortcode($string_with_shortcodes);

이 방법이 작동하지 않으면 설치에 문제가 있는 것입니다.

참고로, 그것은eval()당신의 코드에서 최고의 명령이 아닙니다.. ! :)

마지막으로 쇼트 코드가 여전히 존재하는지 확인합니다.여러 테마에서 쇼트코드를 사용하여 콘텐츠를 스타일링한 다음 사용자가 다른 테마를 설치하면 이전 테마와 함께 제거되므로 이전 쇼트코드는 더 이상 작동하지 않습니다.

$string_with_shortcodes = str_replace($short, do_shortcode($short), $string_with_shortcodes);

나는 당신이 Eval로 무엇을 하려고 하는지 잘 모르겠습니다.

언급URL : https://stackoverflow.com/questions/20659509/executing-wordpress-shortcodes-in-a-string

반응형