Using ChatGPT AI for WordPress Customization: An Experiment with Gravity Form’s Phone Number Format

WordPress Tweaks

A friend recently asked me if I could adjust his WordPress site’s Gravity Forms to save domestic US phone numbers in a dash format, which would look like ###-###-####, instead of the default parenthesis format, which looks like (###) ###-####.

A quick Google Search didn’t pull up a ready solution so I figured that I’d have to write the snippet of code myself. I love how customizable the Gravity Forms plugin is so I knew that it shouldn’t be too much trouble. However, even though I’ve written or customized quite a few little snippets for Gravity Forms in the past, it always takes a time to look up exactly which filter to use, what the correct property names and functions to call, the best way to parse the string in PHP, and so on…

A Promising Start

So why not ask ChatGPT to write it for me? I thought I’d give it a try! Right out of the gate I was rather impressed with the job it did getting some decent logic for removing non-digit characters from the original phone number string and making sure that the result was 10 characters before formatting it with dashes:

function format_phone_numbers( $entry, $form ) {
    // Define the phone field IDs that you want to format
    $phone_field_ids = array( 1, 2, 3 ); // Replace with actual field IDs

    foreach ( $phone_field_ids as $field_id ) {
        if ( isset( $entry[ $field_id ] ) ) {
            $phone_number = $entry[ $field_id ];
            
            // Remove any non-digit characters from the phone number
            $phone_number = preg_replace( '/\D/', '', $phone_number );

            // Check if the phone number is in a valid format
            if ( strlen( $phone_number ) === 10 ) {
                // Format the phone number with a dash between area code and the rest
                $formatted_phone = substr( $phone_number, 0, 3 ) . '-' . substr( $phone_number, 3, 3 ) . '-' . substr( $phone_number, 6 );
                $entry[ $field_id ] = $formatted_phone;
            }
        }
    }
    return $entry;
}
add_filter( 'gform_entry_pre_save', 'format_phone_numbers', 10, 2 );

Rapid Improvements

But it was obviously lacking in certain areas, particularly in requiring the manual entry of an array of field IDs to format. Why not just detect all of the phone number fields?! Thankfully, it was super simple to make this revision and a few others—simply by asking! “Hmm, let’s only apply it to domestic US/CANADA numbers … and let’s simplify the code by making it an anonymous function …”. Just like that, ChatGPT handled each request effortlessly:

add_filter( 'gform_entry_pre_save', function( $entry, $form ) {
    foreach ( $form['fields'] as $field ) {
        if ( $field->type === 'phone' && isset( $entry[ $field->id ] ) ) {
            $phone_number = $entry[ $field->id ];

            if ( $field->phoneFormat === 'standard' ) {
                // Remove any non-digit characters from the phone number
                $phone_number = preg_replace( '/\D/', '', $phone_number );

                // Check if the phone number is in a valid format
                if ( strlen( $phone_number ) === 10 ) {
                    // Format the phone number with a dash between area code and the rest
                    $formatted_phone = substr( $phone_number, 0, 3 ) . '-' . substr( $phone_number, 3, 3 ) . '-' . substr( $phone_number, 6 );
                    $entry[ $field->id ] = $formatted_phone;
                }
            }
        }
    }
    return $entry;
}, 10, 2 );

A Major Letdown

But when I plugged it into my site using Code Snippets, it didn’t work at all! A quick Google Search revealed that there is no such Gravity Forms filter as “gform_entry_pre_save”!! What a letdown. I certainly hadn’t expected the bot to make up it’s own WordPress filter!

The correct filter to use turned out to be “gform_save_field_value”. After informing the bot of this fact, he humbly corrected his code. The final version worked beautifully:

add_filter( 'gform_save_field_value', function( $value, $entry, $field, $form ) {
    if ( $field->type === 'phone' && $field->phoneFormat === 'standard' ) {
        $phone_number = $value;

        // Remove any non-digit characters from the phone number
        $phone_number = preg_replace( '/\D/', '', $phone_number );

        // Check if the phone number is in a valid format
        if ( strlen( $phone_number ) === 10 ) {
            // Format the phone number with a dash between area code and the rest
            $formatted_phone = substr( $phone_number, 0, 3 ) . '-' . substr( $phone_number, 3, 3 ) . '-' . substr( $phone_number, 6 );
            $value = $formatted_phone;
        }
    }
    return $value;
}, 10, 4 );

My Thoughts On the Experiment

Using ChatGPT 3.5 did speed up the time that it took me to write this snippet of code. I don’t write PHP code for Gravity Forms every day so I tend to forget the correct names of filters and properties. It took less time to ask ChatGPT to do it for me than to do the Google searches and documentation-browsing necessary for me to write this from scratch.

But it required knowledgeable input to obtain good output. And even then it completely made up a non-existent filter name on the spot! So while ChatGPT 3.5 is certainly a helpful tool when given the right guidance, it shouldn’t be relied upon as a substitute for real knowledge and experience.

Here’s a link to my full chat if you’re interested:
https://chat.openai.com/share/da7ce27f-7309-42ca-8959-2305a02eef6e

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Notify me of followup comments via e-mail. You can also subscribe without commenting.