42. Custom Export Formats | Form Forge - Build Forms with AI in Seconds
Download Log in

42. Custom Export Formats

Developer Guide

Build custom export formats beyond the built-in CSV export.

JSON Export

php
function export_submissions_json( $form_id ) {
    global $wpdb;
    $rows = $wpdb->get_results( $wpdb->prepare(
        "SELECT * FROM {$wpdb->prefix}formforge_submissions
         WHERE form_id = %d AND status = 'new'
         ORDER BY created_at ASC",
        $form_id
    ) );

    $export = [];
    foreach ( $rows as $row ) {
        $data = json_decode( $row->data, true );
        $entry = [
            'id'         => (int) $row->id,
            'date'       => $row->created_at,
            'ip'         => $row->ip_address,
        ];
        foreach ( $data as $field_id => $field ) {
            $entry[ $field['label'] ] = $field['value'];
        }
        $export[] = $entry;
    }

    return wp_json_encode( $export, JSON_PRETTY_PRINT );
}

XML Export

php
function export_submissions_xml( $form_id ) {
    global $wpdb;
    $rows = $wpdb->get_results( $wpdb->prepare(
        "SELECT * FROM {$wpdb->prefix}formforge_submissions
         WHERE form_id = %d AND status = 'new'", $form_id
    ) );

    $xml = new SimpleXMLElement( '<submissions/>' );
    foreach ( $rows as $row ) {
        $sub = $xml->addChild( 'submission' );
        $sub->addAttribute( 'id', $row->id );
        $sub->addAttribute( 'date', $row->created_at );
        $data = json_decode( $row->data, true );
        foreach ( $data as $field ) {
            $child = $sub->addChild( 'field', esc_html( $field['value'] ) );
            $child->addAttribute( 'label', $field['label'] );
        }
    }
    return $xml->asXML();
}

Register a Custom Export Endpoint

php
add_action( 'rest_api_init', function() {
    register_rest_route( 'mytheme/v1', '/export/(?P<form_id>d+)', [
        'methods'  => 'GET',
        'callback' => function( WP_REST_Request $request ) {
            $format  = $request->get_param( 'format' ) ?? 'csv';
            $form_id = (int) $request['form_id'];

            switch ( $format ) {
                case 'json':
                    return new WP_REST_Response( json_decode( export_submissions_json( $form_id ) ), 200 );
                case 'xml':
                    return new WP_REST_Response( export_submissions_xml( $form_id ), 200, [
                        'Content-Type' => 'application/xml',
                    ] );
                default:
                    return new WP_REST_Response( export_submissions_csv( $form_id ), 200, [
                        'Content-Type' => 'text/csv',
                    ] );
            }
        },
        'permission_callback' => function() {
            return current_user_can( 'manage_options' );
        },
    ] );
} );

Forge AI Assistant Online

Hi! I'm the Form Forge AI assistant. Ask me anything about the plugin — setup, features, troubleshooting, or development.

Just now
Powered by Forge AI · Browse docs