0

From a simple plugin I have to create a page where to give data in json form.

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' );

but in this way I get this

enter image description here

Instead, I want to get this

enter image description here

0

You can hook before the template is loaded and output page content directly:

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' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.