Plugin json page

时间:2018-08-24 作者:Afol

从一个简单的插件中,我必须创建一个页面,在该页面中以json格式提供数据。

function create_page_json() {

    $return = array(
        \'value1\'  => \'1\',
        \'value2\'  => \'ok\'
    );
    $jdata = json_encode($return);

    $PageGuid = site_url() . "/page-json";
    $my_post  = array( \'post_title\'     => \'Page Json\',
                 \'post_type\'      =>  \'page\',
                 \'post_name\'      => \'json-page\',
                 \'post_content\'   => $jdata, 
                 \'post_status\'    => \'publish\',
                 \'comment_status\' => \'closed\',
                 \'ping_status\'    => \'closed\',
                 \'post_author\'    => 1,
                 \'menu_order\'     => 0,
                 \'guid\'           => $PageGuid );

    $PageID = wp_insert_post( $my_post, FALSE ); 
}

if( get_page_by_title( \'Page Json\' ) == NULL ) 
    add_action( \'init\', \'create_page_json\' );
但这样我就明白了

enter image description here

相反,我想得到这个

enter image description here

1 个回复
SO网友:Milo

您可以在加载模板之前进行挂钩,并直接输出页面内容:

function wpd_output_json_page(){
    if( is_page( \'Page Json\' ) ){
        global $post;
        echo $post->post_content;
        die;
    }
}
add_action( \'template_redirect\', \'wpd_output_json_page\' );

结束