2016-02-16 34 views
5

我使用WordPress REST API在外部應用程序中獲取我的WordPress頁面的HTML內容。我打電話這個mysite的/ WP-JSON/WP/V2 /頁/ 10和返回:從WordPress REST API獲取原始HTML輸出

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

有沒有辦法返回代碼在它的最終的HTML輸出和未經[vc_]簡碼,例如:<p>Hello World</p>

簡碼來自Visual Composer page builder plugin

+0

這裏有同樣的問題。我一直在嘗試使用內容過濾器將其轉換爲HTML。我也發佈在支持論壇上,所以我希望能在那裏或在這裏得到答覆。 :) [WP REST API支持論壇帖子](https://wordpress.org/support/topic/convert-shortcodes-to-html-for-json-api) –

回答

0

發現這裏回答: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); 

    return $output; 
}