Atenção!

Este artigo contém código PHP e destina-se a desenvolvedores. Oferecemos este código como uma cortesia, mas não fornecemos suporte para personalizações de código ou desenvolvimento de terceiros.

Para orientação extra, consulte o tutorial do WPBeginner sobre como adicionar código personalizado.

Dispensar

Como Validar um Campo de URL de Mídia Social em Seu Formulário

Introductioon

Do you want to validate a social media URL in your form? For the purpose of this documentation, we’re going to allow the user to enter a Twitter URL and a Facebook URL but we want the ability to validate the social media URLs before the form is submitted, in order to do this we’ll use PHP. In this tutorial, we’re going to walk you through the steps on how to achieve this.

Criando o formulário

First, you’ll need to create your form. Our form will include two Website / URL fields. One will be for a Twitter follow URL and the other for a Facebook follow URL.

Se precisar de ajuda para criar seu formulário, por favor, revise esta documentação.

add the Website / URL field for your users to enter their social media url

Adicionando o snippet

Now that you’ve added the fields, it’s time to add the code snippet to your site that will validate a social media URL in your form.

Se você não tem certeza de como ou onde adicionar snippets ao seu site, consulte este tutorial.

/**
 * Confirm the correct social network link in the form.
 *
 * @link   https://wpforms.com/developers/how-to-validate-a-social-media-url-field-in-your-form/
 */

function wpf_dev_process_check_social_url( $fields, $entry, $form_data ) {
      
    // Optional, you can limit to specific forms. Below, we restrict output to
    // form #902.
    if ( absint( $form_data['id'] ) !== 902 ) {
        return $fields;
    }
    
	
	$fb_url = $fields[25][ 'value' ];
	$findfb   = 'facebook.com';
	$find_fb_url = strpos($fb_url, $findfb);

	if ($find_fb_url === false) {
            // Check the field ID 25 to make sure it contains facebook.com
               wpforms()->process->errors[ $form_data[ 'id' ] ] [ '25' ] = esc_html__( 'Please enter a valid Facebook Profile URL.', 'plugin-domain' );
  
        }
	
	$twitter_url = $fields[26][ 'value' ];
	$findtwitter   = 'twitter.com';
	$find_twitter_url = strpos($twitter_url, $findtwitter);

	if ($find_twitter_url === false) {

            // Check the field ID 26 to make sure it contains twitter.com
               wpforms()->process->errors[ $form_data[ 'id' ] ] [ '26' ] = esc_html__( 'Please enter a valid Twitter Profile URL.', 'plugin-domain' );
  
        }
	
    }
add_action( 'wpforms_process', 'wpf_dev_process_check_social_url', 10, 3 );

In the code snippet above, we’re only using this snippet on the form ID 902 and only on field ID 25 which is the Facebook URL field and field 26 which is the Twitter URL field.

When the users enter a link in those fields that do not match what is listed in our code snippet above, they will see an error after they’ve submitted the form.

You’ll need to update the form and field IDs to match what you have on your site. If you need help in finding these IDs, please take a look at this tutorial.

with this snippet added you can easily validate a social media URL within your form

And that’s it! You’ve successfully been able to validate a social media URL. Would you like to also have the users automatically log in once they’ve completed the registration form? Take a look at our article on How to Automatically Log in Users After Registration.

Action Reference: wpforms_process

Perguntas Frequentes

Q: Can I add LinkedIn to this snippet?

A: Absolutely! To add to the above snippet for LinkedIn, use this snippet and update the form and field IDs for the form you’re using.

/**
 * Confirm the correct social network link in the form.
 *
 * @link   https://wpforms.com/developers/how-to-validate-a-social-media-url-field-in-your-form/
 */

function wpf_dev_process_check_social_url( $fields, $entry, $form_data ) {
      
    // Optional, you can limit to specific forms. Below, we restrict output to
    // form #902.
    if ( absint( $form_data['id'] ) !== 902 ) {
        return $fields;
    }
    
	
	$fb_url = $fields[25][ 'value' ];
	$findfb   = 'facebook.com';
	$find_fb_url = strpos($fb_url, $findfb);

	if ($find_fb_url === false) {
            // Check the field ID 25 to make sure it contains facebook.com
               wpforms()->process->errors[ $form_data[ 'id' ] ] [ '25' ] = esc_html__( 'Please enter a valid Facebook Profile URL.', 'plugin-domain' );
  
        }
	
	$twitter_url = $fields[26][ 'value' ];
	$findtwitter   = 'twitter.com';
	$find_twitter_url = strpos($twitter_url, $findtwitter);

	if ($find_twitter_url === false) {

            // Check the field ID 26 to make sure it contains twitter.com
               wpforms()->process->errors[ $form_data[ 'id' ] ] [ '26' ] = esc_html__( 'Please enter a valid Twitter Profile URL.', 'plugin-domain' );
  
        }

	$linkedin_url = $fields[27][ 'value' ];
	$findlinkedin   = 'linkedin.com';
	$find_linkedin_url = strpos($linkedin_url, $findlinkedin);

	if ($find_linkedin_url === false) {

            // Check the field ID 27 to make sure it contains linkedin.com
               wpforms()->process->errors[ $form_data[ 'id' ] ] [ '27' ] = esc_html__( 'Please enter a valid LinkedIn Profile URL.', 'plugin-domain' );
  
        }
	
    }
add_action( 'wpforms_process', 'wpf_dev_process_check_social_url', 10, 3 );