homebrandbeta

JSON Export for WordPress content

Sep 12th, 2024 (edited)
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.32 KB | Source Code | 0 0
  1. <?php
  2. /**
  3.  * Plugin Name: JSON Export
  4.  * 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.
  5.  * Version: 0.7
  6.  * Author: Doug Belshaw
  7.  * Author URI: http://dougbelshaw.com
  8.  */
  9.  
  10. add_action('admin_menu', 'json_export_menu');
  11.  
  12. function json_export_menu() {
  13.     add_menu_page('JSON Export', 'JSON Export', 'manage_options', 'json-export', 'json_export_page');
  14. }
  15.  
  16. function json_export_page() {
  17.     // Fetch categories for the dropdown
  18.     $categories = get_categories();
  19.     echo '<div class="wrap"><h1>JSON Export</h1>';
  20.     echo '<form action="" method="post">';
  21.     // Button for exporting all posts
  22.     echo '<input type="submit" name="export_posts_json" value="Export All Posts" class="button-primary"/><br/><br/>';
  23.     // Dropdown for categories
  24.     echo '<select name="category">';
  25.     foreach ($categories as $category) {
  26.         echo '<option value="' . $category->term_id . '">' . $category->name . '</option>';
  27.     }
  28.     echo '</select>';
  29.     echo '<input type="submit" name="export_category_json" value="Export Category Posts" class="button-primary"/>';
  30.     echo '</form></div>';
  31. }
  32.  
  33. add_action('admin_init', 'json_export_posts_to_json');
  34.  
  35. function json_export_posts_to_json() {
  36.     if (current_user_can('manage_options')) {
  37.         if (isset($_POST['export_posts_json'])) {
  38.             $args = array('posts_per_page' => -1, 'post_status' => 'publish');
  39.             $filename = 'blog-json-all-' . date('Y-m-d') . '.json';
  40.         } elseif (isset($_POST['export_category_json']) && !empty($_POST['category'])) {
  41.             $category_id = intval($_POST['category']);
  42.             $category = get_category($category_id);
  43.             $args = array('posts_per_page' => -1, 'post_status' => 'publish', 'cat' => $category_id);
  44.             $category_name = strtolower(str_replace(' ', '-', $category->name));
  45.             $filename = 'blog-json-' . $category_name . '-' . date('Y-m-d') . '.json';
  46.         } else {
  47.             return;
  48.         }
  49.        
  50.         $posts = get_posts($args);
  51.         $posts_array = array_map(function ($post) {
  52.             // Get content and clean it
  53.             $content = apply_filters('the_content', $post->post_content);
  54.  
  55.             // Remove URLs
  56.             $content_without_urls = preg_replace('/https?:\/\/[^\s"]+/', '', $content);
  57.  
  58.             // Strip HTML tags
  59.             $clean_content = wp_strip_all_tags($content_without_urls);
  60.  
  61.             // Remove newlines, tabs, and extra spaces
  62.             $clean_content = str_replace(array("\n", "\r", "\t"), ' ', $clean_content);
  63.             $clean_content = preg_replace('/\s+/', ' ', $clean_content); // Reduce multiple spaces to a single space
  64.  
  65.             return array(
  66.                 'id'      => trim($post->ID),
  67.                 'title'   => get_the_title($post->ID),
  68.                 //'date'    => get_the_date('Y-m-d', $post->ID),
  69.                 'content' => trim($clean_content), // Trim any leftover leading/trailing spaces
  70.                 'url'     => get_permalink($post->ID),
  71.             );
  72.         }, $posts);
  73.  
  74.         header('Content-Disposition: attachment; filename=' . $filename);
  75.         header('Content-Type: application/json');
  76.         echo json_encode($posts_array);
  77.         exit;
  78.     }
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment