sourcecode

WordPress REST API에서 원시 HTML 출력 가져오기

codebag 2023. 2. 7. 19:52
반응형

WordPress REST API에서 원시 HTML 출력 가져오기

WordPress REST API를 사용하여 외부 응용 프로그램에서 WordPress 페이지의 HTML 콘텐츠를 가져옵니다.이 사이트를 mysite/wp-json/wp/v2/pages/10이라고 부르면 다음과 같이 반환됩니다.

"content": {
  "rendered": "[vc_column_text]Hello World[/vc_column_text]"
}

[vc_] 쇼트 코드 없이 최종 HTML 출력으로 코드를 반환할 수 있는 방법이 있습니까?<p>Hello World</p>

단축 코드는 Visual Composer 페이지 작성기 플러그인에서 가져옵니다.

여기에서 검색하여 답변: https://github.com/CompassHB/web/issues/67#issuecomment-245857301

다음 예는 위의 링크에서 가져온 것입니다.

/**
 * Modify REST API content for pages to force
 * shortcodes to render since Visual Composer does not
 * do this
 */
add_action( 'rest_api_init', function ()
{
   register_rest_field(
          'page',
          'content',
          array(
                 'get_callback'    => 'compasshb_do_shortcodes',
                 'update_callback' => null,
                 'schema'          => null,
          )
       );
});

function compasshb_do_shortcodes( $object, $field_name, $request )
{
   WPBMap::addAllMappedShortcodes(); // This does all the work

   global $post;
   $post = get_post ($object['id']);
   $output['rendered'] = apply_filters( 'the_content', $post->post_content );

   // EDIT: add custom CSS to $output:
   $output[ 'yb_wpb_post_custom_css' ] = get_post_meta( $object[ 'id' ], '_wpb_post_custom_css', true);

   return $output;
}

편집

코멘트에서 질문이 제기되었습니다.커스텀 CSS 세트를 페이지(게시물 등)에 입수하는 방법REST API 응답에 커스텀 CSS가 추가되도록 샘플 코드를 수정하였습니다.CSS는 다음 URL에 있습니다.content/yb_wpb_post_custom_css.

다른 방법은 이 CSS를 포함하는 REST API 응답에 다른 필드를 추가하는 것입니다.중요한 것은 커스텀 CSS가 페이지/포스트 등에 설정되어 있는 것입니다.에는 메타키 _wpb_post_custom_css가 있습니다.

하지만 파티에 2년 정도 늦게 도착했는데, 다음과 같은 일이 있었습니다.

$output['rendered'] = apply_filters( 'the_content', get_the_content() );

혹시 궁금하실까 봐.

언급URL : https://stackoverflow.com/questions/35432185/get-raw-html-output-from-wordpress-rest-api

반응형