0

I have created a file using json and exported it. Its creating HTML tags with it. Actually,I don't want to have that html part to be there in the file. Below I am sharing structure of my current file: HTML default tags like head,title etc and below is json alon with it.

<html xmlns="http://www.w3.org/1999/xhtml" class="wp-toolbar"  lang="en-US">
<!--<![en-->
[{
    "ID" : 13,
    "post_author" : "1",
    "post_date" : "2015-10-30 07:09:16",
    "post_date_gmt" : "2015-10-30 07:09:16",
    "post_content" : "Test",
    "post_title" : "Test",
    "post_excerpt" : "",
    "post_status" : "publish",
    "comment_status" : "closed",
    "ping_status" : "closed",
    "post_password" : "",
    "post_name" : "test",
    "to_ping" : "",
    "pinged" : "",
    "post_modified" : "2015-10-30 07:09:16",
    "post_modified_gmt" : "2015-10-30 07:09:16",
    "post_content_filtered" : "",
    "post_parent" : 0,
    "guid" : "url/?post_type=wpcp_pointer&#038;p=9",
    "menu_order" : 0,
    "post_type" : "wpcp_pointer",
    "post_mime_type" : "",
    "comment_count" : "0",
    "filter" : "raw"
}
]

But what I want is :

[{
    "ID" : 13,
    "post_author" : "1",
    "post_date" : "2015-10-30 07:09:16",
    "post_date_gmt" : "2015-10-30 07:09:16",
    "post_content" : "Test",
    "post_title" : "Test",
    "post_excerpt" : "",
    "post_status" : "publish",
    "comment_status" : "closed",
    "ping_status" : "closed",
    "post_password" : "",
    "post_name" : "test",
    "to_ping" : "",
    "pinged" : "",
    "post_modified" : "2015-10-30 07:09:16",
    "post_modified_gmt" : "2015-10-30 07:09:16",
    "post_content_filtered" : "",
    "post_parent" : 0,
    "guid" : "url/?post_type=wpcp_pointer&#038;p=9",
    "menu_order" : 0,
    "post_type" : "wpcp_pointer",
    "post_mime_type" : "",
    "comment_count" : "0",
    "filter" : "raw"
}
]

Only JSON array to be there file export. Please help how could I remove that html part from json file. Below is the coding part using to generate this code:

$json_file = json_encode($need_options); // Encode data into json data
$json_file = stripslashes($json_file);

echo $json_file;

header("Content-Type: text/json; charset=" . get_option( 'blog_charset'));
header("Content-Disposition: attachment; filename=$json_name.json");
exit();

Any help will be really appriciated.

1 Answer 1

0

You've left something out of the description-- namely, it isn't clear how/where you load that code. I am guessing you are pushing things through a template file somehow, or through a backend plugin file maybe, as you have what looks like template code showing up.

You should instead, in my opinion, be using the AJAX API:

function my_grab_ajax_wpse_108874() {
  if (empty($_GET['need_options'])) die;
  $need_options = $_GET['need_options']; // ??????
  $need_options = get_post($need_options);

  $json_name = $need_options->post_name;

  $json_file = json_encode($need_options); // Encode data into json data
  $json_file = stripslashes($json_file);

  header("Content-Type: text/json; charset=" . get_option( 'blog_charset'));
  header("Content-Disposition: attachment; filename=$json_name.json");

  echo $json_file;

  exit();
}
add_action('wp_ajax_my_grab_ajax', 'my_grab_ajax_wpse_108874');
add_action('wp_ajax_nopriv_my_grab_ajax', 'my_grab_ajax_wpse_108874');

Then request the data with http://example.com/wp-admin/admin-ajax.php?action=mygrab_ajax&need_options={some post ID}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.