$widget_ids) { if (!is_array($widget_ids) || empty($widget_ids)) { continue; } foreach ($widget_ids as $widget_id) { $widget_base_id = preg_replace('/-\d+$/', '', $widget_id); $widget_data = get_option('widget_' . $widget_base_id); if (!empty($widget_data)) { $export_data[$sidebar][$widget_id] = $widget_data; } } } // Convert to JSON $json_data = json_encode($export_data, JSON_PRETTY_PRINT); // Save to a file $file = wp_upload_dir()['basedir'] . '/widgets-export.json'; file_put_contents($file, $json_data); return $file; } // Example usage add_action('admin_init', function () { if (isset($_GET['export_widgets'])) { $file = book_author_template_export_widgets_to_json(); header('Content-Type: application/json'); header('Content-Disposition: attachment; filename="widgets-export.json"'); readfile($file); exit; } });