Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Plugin Name: JSON Export
- * Description: Exports blog posts as JSON, including title, date, cleaned content (no URLs, HTML tags, images, newlines, or tabs), and permalink URL. Now with category-specific exports.
- * Version: 0.7
- * Author: Doug Belshaw
- * Author URI: http://dougbelshaw.com
- */
- add_action('admin_menu', 'json_export_menu');
- function json_export_menu() {
- add_menu_page('JSON Export', 'JSON Export', 'manage_options', 'json-export', 'json_export_page');
- }
- function json_export_page() {
- // Fetch categories for the dropdown
- $categories = get_categories();
- echo '<div class="wrap"><h1>JSON Export</h1>';
- echo '<form action="" method="post">';
- // Button for exporting all posts
- echo '<input type="submit" name="export_posts_json" value="Export All Posts" class="button-primary"/><br/><br/>';
- // Dropdown for categories
- echo '<select name="category">';
- foreach ($categories as $category) {
- echo '<option value="' . $category->term_id . '">' . $category->name . '</option>';
- }
- echo '</select>';
- echo '<input type="submit" name="export_category_json" value="Export Category Posts" class="button-primary"/>';
- echo '</form></div>';
- }
- add_action('admin_init', 'json_export_posts_to_json');
- function json_export_posts_to_json() {
- if (current_user_can('manage_options')) {
- if (isset($_POST['export_posts_json'])) {
- $args = array('posts_per_page' => -1, 'post_status' => 'publish');
- $filename = 'blog-json-all-' . date('Y-m-d') . '.json';
- } elseif (isset($_POST['export_category_json']) && !empty($_POST['category'])) {
- $category_id = intval($_POST['category']);
- $category = get_category($category_id);
- $args = array('posts_per_page' => -1, 'post_status' => 'publish', 'cat' => $category_id);
- $category_name = strtolower(str_replace(' ', '-', $category->name));
- $filename = 'blog-json-' . $category_name . '-' . date('Y-m-d') . '.json';
- } else {
- return;
- }
- $posts = get_posts($args);
- $posts_array = array_map(function ($post) {
- // Get content and clean it
- $content = apply_filters('the_content', $post->post_content);
- // Remove URLs
- $content_without_urls = preg_replace('/https?:\/\/[^\s"]+/', '', $content);
- // Strip HTML tags
- $clean_content = wp_strip_all_tags($content_without_urls);
- // Remove newlines, tabs, and extra spaces
- $clean_content = str_replace(array("\n", "\r", "\t"), ' ', $clean_content);
- $clean_content = preg_replace('/\s+/', ' ', $clean_content); // Reduce multiple spaces to a single space
- return array(
- 'id' => trim($post->ID),
- 'title' => get_the_title($post->ID),
- //'date' => get_the_date('Y-m-d', $post->ID),
- 'content' => trim($clean_content), // Trim any leftover leading/trailing spaces
- 'url' => get_permalink($post->ID),
- );
- }, $posts);
- header('Content-Disposition: attachment; filename=' . $filename);
- header('Content-Type: application/json');
- echo json_encode($posts_array);
- exit;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment