add_action( 'bricks/form/custom_action', function( $form ) {
    $fields = $form->get_fields();
    
    // 1. Map Text Fields (Ensures we handle prefixes correctly)
    $email      = $fields['form-field-567c3b'] ?? ''; 
    $first_name = $fields['form-field-22edfb'] ?? '';
    $last_name  = $fields['form-field-kpipky'] ?? '';
    $phone      = $fields['form-field-noxrbn'] ?? '';
    $company    = $fields['form-field-kbywak'] ?? '';
    
    if ( empty( $email ) ) return;

    // 2. Create the User
    $password = wp_generate_password( 12, true );
    $user_id  = wp_create_user( $email, $password, $email );
    
    if ( is_wp_error( $user_id ) ) {
        $form->set_result([
            'action'  => 'register_magtex_distributor',
            'type'    => 'danger',
            'message' => 'Error: ' . $user_id->get_error_message(),
        ]);
        return;
    }

    // 3. Update Standard & Custom User Meta
    wp_update_user([
        'ID'         => $user_id,
        'first_name' => $first_name,
        'last_name'  => $last_name,
        'role'       => 'distribuidor-magtex'
    ]);

    update_user_meta( $user_id, 'dist_company_name', $company );
    update_user_meta( $user_id, 'dist_phone', $phone );
    update_user_meta( $user_id, 'is_approved', '0' );

    // 4. Handle File Binding using attachment_url_to_postid
    $uploaded_files = $form->get_uploaded_files();
    
    // Mapping Bricks Field IDs to Meta Box Meta Keys
    $file_mapping = [
        'form-field-csmohn' => 'constancia_de_situacion_fiscal',
        'form-field-gwimhu' => 'ine'
    ];

    foreach ( $uploaded_files as $field_id => $files ) {
        if ( isset( $file_mapping[$field_id] ) ) {
            $meta_key = $file_mapping[$field_id];

            foreach ( $files as $file ) {
                // Bricks has already moved the file to the uploads folder.
                // We construct the URL to find its ID in the database.
                $upload_dir = wp_upload_dir();
                $file_url   = $upload_dir['url'] . '/' . sanitize_file_name($file['name']);
                
                // Lookup the ID based on the URL
                $attachment_id = attachment_url_to_postid( $file_url );

                if ( $attachment_id ) {
                    // Force the binding as a single integer for Meta Box "File" type
                    update_user_meta( $user_id, $meta_key, (int) $attachment_id );
                    error_log("MAGTEX BINDING SUCCESS: ID $attachment_id linked to $meta_key");
                } else {
                    error_log("MAGTEX BINDING ERROR: Could not find ID for URL $file_url");
                }
            }
        }
    }

    $form->set_result([
        'action'  => 'register_magtex_distributor',
        'type'    => 'success',
        'message' => '¡Registro exitoso! Validaremos tu información y documentos pronto.',
    ]);

}, 10, 1 );<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="https://magtex.com.mx/sitemaps_xsl.xsl"?>
<sitemapindex xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc>https://magtex.com.mx/page-sitemap1.xml</loc>
<lastmod>2026-04-01T07:22:15-06:00</lastmod>
</sitemap>
<sitemap>
<loc>https://magtex.com.mx/product-sitemap1.xml</loc>
<lastmod>2026-03-31T10:52:45-06:00</lastmod>
</sitemap>
<sitemap>
<loc>https://magtex.com.mx/product_cat-sitemap1.xml</loc>
</sitemap>
</sitemapindex>