Skip to content

keesiemeijer/wp-parser-json

Repository files navigation

WP Parser JSON

A WordPress plugin to create JSON files from posts.

The settings page for this plugin is at Tools -> WP Parser JSON. Here you can create JSON files for any post type. Or use the wp parser-json generate WP-CLI command to create JSON files.

Example

wp parser-json generate --post_type=post,page --posts_per_page=100

The JSON files are saved in this plugin's directory in the folder json-files. If you use the --posts_per_page option multiple (numbered) files are created for a post type instead of a single file (per post type).

Settings page

Screen shot of settings page

Backward Compatibility

Originally this plugin only created JSON files for the post types of the the WP Parser plugin. For backward compatibility if you do not provide a post type and the WP Parser post types exist it will create the JSON files for the wp-parser-function,wp-parser-hook andwp-parser-class post types. It will also create a version.json file with the WP version that was parsed.

JSON

This is the JSON file (movies.json) structure for a movies post type.

{
  "post_type": "movies",
  "url": "https:\/\/my-website.com\/movie",
  "found_posts": 100,
  "max_pages": 1,
  "posts_per_page": -1,
  "content":[
      {"title": "Die Hard", "slug": "die-hard"},
      {"title": "Mad Max Fury Road", "slug": "mad-max-fury-road"},

      {"title": "etc...", "slug":"etc..."}
    ]
}

As you can see, by default only the post title and slug are included. To add more post fields use the wp_parser_json_content_item filter.

Example of adding the post ID field.

add_filter( 'wp_parser_json_content_item', 'json_parser_add_post_id', 10, 2 );

function json_parser_add_post_id( $item, $post ) {
	$item['post_id'] = $post->ID;

	return $item;
}

This will result with the post ID added in the JSON files.

{
  "post_type": "movies",
  "url": "https:\/\/my-website.com\/movie",
  "found_posts": 2,
  "max_pages": 1,
  "posts_per_page": -1,
  "content":[
      {"title": "Die Hard", "slug": "die-hard", "post_id": 1288},
      {"title": "Mad Max Fury Road", "slug": "mad-max-fury-road", "post_id": 2768}
    ]
}

The url and posts_per_page values can also be filtered. See Filters below.

Pagination

Use the --posts_per_page option if you need to add a lot of post fields with the wp_parser_json_content_item filter above. This stops the JSON files from getting to big. if --posts_per_page is used everything you add with the filter will be put in the paginated files.

Example of a movies.json file created with the --posts_per_page=2 option.

{
  "post_type": "movies",
  "url": "https:\/\/my-website.com\/movie",
  "found_posts": 3,
  "max_pages": 2,
  "posts_per_page": 2,
  "content":[
      {"title": "Die Hard", "slug": "die-hard", "page": 1},
      {"title": "Mad Max Fury Road", "slug": "mad-max-fury-road", "page": 1}
      {"title": "The Terminator", "slug": "the-terminator", "page": 2}
    ]
}

In this example you see that you can access the The Terminator post fields in the movies-2.json file.

Filters

About

A WordPress plugin to create JSON files from posts

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages