WordPress Sitemap Hydration from JSON

Paste sitemap.json generated from WP Kanban Plugin into “Tools > Blueprint Hydrator” to programmatically add the full nested sitemap to WordPress.

functions.php

/**
 * WP Kanban Tracker - Stage 2 Blueprint Hydrator Prototype
 * Add this block to the bottom of your target theme's functions.php file.
 */

// 1. Create the Admin Menu Item under "Tools"
add_action('admin_menu', 'kanban_register_hydrator_prototype_page');
function kanban_register_hydrator_prototype_page() {
    add_management_page(
        'Blueprint Hydrator',       // Page Title
        'Blueprint Hydrator',       // Menu Title
        'manage_options',           // Capability required
        'kanban-blueprint-hydrator', // Menu Slug
        'kanban_render_hydrator_prototype_view' // Callback function
    );
}

// 2. Render the Backend Interface View
function kanban_render_hydrator_prototype_view() {
    // Check if the user has submitted the form
    $message = '';
    $error = '';

    if (isset($_POST['kanban_blueprint_nonce']) && wp_verify_nonce($_POST['kanban_blueprint_nonce'], 'execute_hydration')) {
        if (!current_user_can('manage_options')) {
            wp_die('Unauthorized user.');
        }

        $raw_json = isset($_POST['blueprint_json_payload']) ? trim(stripslashes($_POST['blueprint_json_payload'])) : '';

        if (empty($raw_json)) {
            $error = 'Payload is empty. Please paste a valid JSON snapshot string.';
        } else {
            $decoded_data = json_decode($raw_json, true);

            if (json_last_error() !== JSON_ERROR_NONE) {
                $error = 'Invalid JSON: ' . json_last_error_msg();
            } else {
                // JSON is valid! Execute the recursive layout engine loop
                $created_count = kanban_execute_prototype_hydration_loop($decoded_data);
                $message = "Success! Successfully hydrated database with {$created_count} pages and architectural child structures.";
            }
        }
    }

    // Clean, scannable minimalist UI output mimicking your system tokens
    ?>
    <div class="wrap" style="max-width: 800px; margin-top: 30px; font-family: -apple-system,BlinkMacSystemFont,sans-serif;">
        <h1 style="font-weight: 700; letter-spacing: -0.02em; margin-bottom: 4px;">Sitemap Blueprint Hydrator</h1>
        <p style="color: #64748b; margin-top: 0; margin-bottom: 24px;">Paste your structural JSON snapshot below to instantly generate page and child-page architectures.</p>

        <?php if (!empty($message)) : ?>
            <div class="notice notice-success is-dismissible" style="border-left-color: #10b981;"><p><strong><?php echo esc_html($message); ?></strong></p></div>
        <?php endif; ?>

        <?php if (!empty($error)) : ?>
            <div class="notice notice-error is-dismissible" style="border-left-color: #ef4444;"><p><strong><?php echo esc_html($error); ?></strong></p></div>
        <?php endif; ?>

        <div style="background: #1e293b; border: 1px solid #334155; border-radius: 8px; padding: 24px; color: #f8fafc;">
            <form method="post" action="">
                <?php wp_nonce_field('execute_hydration', 'kanban_blueprint_nonce'); ?>

                <label for="blueprint_json_payload" style="display: block; font-weight: 600; margin-bottom: 10px; font-size: 0.9rem; color: #94a3b8;">
                    Blueprint JSON Snapshot String
                </label>

                <textarea
                    id="blueprint_json_payload"
                    name="blueprint_json_payload"
                    rows="12"
                    placeholder='Paste layout JSON snapshot here...'
                    style="width: 100%; background: #0f172a; border: 1px solid #475569; border-radius: 6px; color: #38bdf8; font-family: monospace; font-size: 0.85rem; padding: 14px; box-sizing: border-box; resize: vertical; outline: none; margin-bottom: 20px;"
                ></textarea>

                <button
                    type="submit"
                    class="button button-primary"
                    style="background: #38bdf8; border-color: #38bdf8; color: #0f172a; font-weight: 600; font-size: 0.875rem; height: auto; padding: 8px 20px; border-radius: 6px; text-shadow: none; box-shadow: none;"
                >
                    Execute Site Hydration
                </button>
            </form>
        </div>
    </div>
    <?php
}

// 3. Recursive Page Processing Engine Loop
function kanban_execute_prototype_hydration_loop($nodes, $parent_id = 0) {
    $count = 0;

    // Handle single-object inputs gracefully by converting them to a predictable array layout
    if (isset($nodes['title'])) {
        $nodes = array($nodes);
    }

    foreach ($nodes as $node) {
        if (!isset($node['title']) || empty($node['title'])) {
            continue;
        }

        // Construct baseline post args target routing array
        $post_args = array(
            'post_title'    => sanitize_text_field($node['title']),
            'post_name'     => isset($node['slug']) ? sanitize_title($node['slug']) : '',
            'post_status'   => 'publish',
            'post_type'     => 'page', // Routing as proper WordPress Pages
            'post_parent'   => $parent_id,
            'post_content'  => '<!-- wp:paragraph --><p>Placeholder content for ' . esc_html($node['title']) . '.</p><!-- /wp:paragraph -->'
        );

        // Check if a page with this specific title and parent structure already exists to prevent duplicate test runs
        $existing_page = get_page_by_title($node['title'], OBJECT, 'page');
        if ($existing_page && $existing_page->post_parent == $parent_id) {
            $current_inserted_id = $existing_page->ID;
        } else {
            // Write to the WordPress Database
            $current_inserted_id = wp_insert_post($post_args);
        }

        if (!is_wp_error($current_inserted_id) && $current_inserted_id > 0) {
            $count++;

            // If children exist nested in this snapshot branch, process them immediately under the newly minted parent ID
            if (isset($node['children']) && is_array($node['children']) && !empty($node['children'])) {
                $count += kanban_execute_prototype_hydration_loop($node['children'], $current_inserted_id);
            }
        }
    }

    return $count;
}

sitemap.json

[
    {
        "title": "About",
        "slug": "about",
        "children": [
            {
                "title": "Board of Directors",
                "slug": "board-of-directors",
                "children": []
            },
            {
                "title": "Careers",
                "slug": "careers",
                "children": []
            },
            {
                "title": "Contact Us",
                "slug": "contact-us",
                "children": []
            },
            {
                "title": "Exec Team &#038; Departments",
                "slug": "exec-team-departments",
                "children": []
            },
            {
                "title": "Media",
                "slug": "media",
                "children": []
            },
            {
                "title": "Shelter Statistics",
                "slug": "shelter-statistics",
                "children": []
            }
        ]
    },
    {
        "title": "Adopt",
        "slug": "adopt",
        "children": [
            {
                "title": "Cats",
                "slug": "cats",
                "children": []
            },
            {
                "title": "Dogs",
                "slug": "dogs",
                "children": []
            },
            {
                "title": "Other Animals",
                "slug": "other-animals",
                "children": []
            }
        ]
    },
    {
        "title": "Events",
        "slug": "events",
        "children": [
            {
                "title": "Animal Camp",
                "slug": "animal-camp",
                "children": []
            },
            {
                "title": "Faux Fur Ball 2027",
                "slug": "faux-fur-ball-2027",
                "children": []
            }
        ]
    },
    {
        "title": "Home",
        "slug": "home",
        "children": []
    },
    {
        "title": "Policy Terms",
        "slug": "policy-terms",
        "children": []
    },
    {
        "title": "Privacy",
        "slug": "privacy",
        "children": []
    },
    {
        "title": "Services",
        "slug": "services",
        "children": [
            {
                "title": "Animal Control",
                "slug": "animal-control",
                "children": []
            },
            {
                "title": "Community Cat Programs / TNR",
                "slug": "community-cat-programs-tnr",
                "children": [
                    {
                        "title": "Garden Cats",
                        "slug": "garden-cats",
                        "children": []
                    }
                ]
            },
            {
                "title": "Found a Pet",
                "slug": "found-a-pet",
                "children": []
            },
            {
                "title": "Help for Pet Owners",
                "slug": "help-for-pet-owners",
                "children": [
                    {
                        "title": "Low Cost Vaccine Clinic",
                        "slug": "low-cost-vaccine-clinic",
                        "children": []
                    },
                    {
                        "title": "Pet Food Bank",
                        "slug": "pet-food-bank",
                        "children": []
                    },
                    {
                        "title": "Sammy&#8217;s Circle",
                        "slug": "sammys-circle",
                        "children": []
                    },
                    {
                        "title": "Surrendering a Pet",
                        "slug": "surrendering-a-pet",
                        "children": []
                    }
                ]
            },
            {
                "title": "Lost Your Pet",
                "slug": "lost-your-pet",
                "children": []
            },
            {
                "title": "Microchip Your Pets",
                "slug": "microchip-your-pets",
                "children": []
            }
        ]
    },
    {
        "title": "Support",
        "slug": "support",
        "children": [
            {
                "title": "Volunteer",
                "slug": "volunteer",
                "children": [
                    {
                        "title": "Foster",
                        "slug": "foster",
                        "children": []
                    }
                ]
            },
            {
                "title": "Ways to Give",
                "slug": "ways-to-give",
                "children": [
                    {
                        "title": "Additional Ways to Give",
                        "slug": "additional-ways-to-give",
                        "children": []
                    },
                    {
                        "title": "Amazon Wish List",
                        "slug": "amazon-wish-list",
                        "children": []
                    },
                    {
                        "title": "Animal Alliance Program",
                        "slug": "animal-alliance-program",
                        "children": []
                    },
                    {
                        "title": "Animal Impact Network",
                        "slug": "animal-impact-network",
                        "children": []
                    },
                    {
                        "title": "Employee Giving Matching Gifts",
                        "slug": "employee-giving-matching-gifts",
                        "children": []
                    },
                    {
                        "title": "Host a Fundraiser",
                        "slug": "host-a-fundraiser",
                        "children": []
                    },
                    {
                        "title": "Legacy &#038; Planned Giving",
                        "slug": "legacy-planned-giving",
                        "children": []
                    },
                    {
                        "title": "Memorials, Tribute &#038; Celebrations",
                        "slug": "memorials-tribute-celebrations",
                        "children": []
                    }
                ]
            }
        ]
    }
]

© 2026 All Rights Reserved.