• Resolved nootkan

    (@nootkan)


    I’ve been trying to find a way to block people from leaving urls in the message body of my contact form but I cannot seem to find a solution. Any ideas?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter nootkan

    (@nootkan)

    Finally found this solution:

    function custom_textarea_validation_filter($result, $tag) {
    $type = $tag[‘type’];
    $name = $tag[‘name’];
    //here textarea type name is ‘message’
    if($name == ‘your-message’) {
    $value = $_POST[$name];
    if (preg_match(‘/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i’, $value)){
    $result->invalidate( $tag, “Avoid URLs & Links” );
    }
    }
    return $result;
    }
    add_filter(‘wpcf7_validate_textarea’,’custom_textarea_validation_filter’, 10, 2);
    add_filter(‘wpcf7_validate_textarea*’, ‘custom_textarea_validation_filter’, 10, 2); 
    Thread Starter nootkan

    (@nootkan)

    Thought this was resolved but it is not. I am seeing this error when using this code:

    [10-Aug-2020 18:04:32 UTC] PHP Parse error: syntax error, unexpected ‘?’ in /home/mywebsite/public_html/wp-content/themes/nikkon-child/functions.php on line 34

    This is line 34:

    if (preg_match(‘/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i’, $value)){
    $result->invalidate( $tag, “Avoid URLs & Links” );

    Not sure what to do next as I deleted all the question marks and that didn’t work. Can anyone help me with this?

    Thread Starter nootkan

    (@nootkan)

    I found a solution that blocks both urls and email addresses from the textarea field in my contact 7 form and I copied the function to do the same for the your-name field.

    function custom_textarea_validation_filter($result, $tag) {  
    $type = $tag['type'];
    $name = $tag['name'];
    if($name == 'your-message') {
    $value = $_POST[$name];
    $Match_all = "/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]|[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,8}/";
    if(preg_match($Match_all,$value)){
    
        $result->invalidate( $tag, "Please no urls or email addresses in your message !" );
                    }
    }
    return $result;
    }
    add_filter('wpcf7_validate_textarea','custom_textarea_validation_filter', 10, 2);
    add_filter('wpcf7_validate_textarea*', 'custom_textarea_validation_filter', 10, 2);
    
    function custom_text_validation_filter($result, $tag) {  
    $type = $tag['type'];
    $name = $tag['name'];
    if($name == 'your-name') {
    $value = $_POST[$name];
    $Match_all = "/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]|[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,8}/";
    if(preg_match($Match_all,$value)){
    
        $result->invalidate( $tag, "Please no urls or email addresses in your message !" );
                    }
    }
    return $result;
    }
    add_filter('wpcf7_validate_text','custom_text_validation_filter', 10, 2);
    add_filter('wpcf7_validate_text*', 'custom_text_validation_filter', 10, 2);

    I also tried to copy it one more time for the your-subject text field but got an internal server error so I removed it. Guess I have to stay with the above code for now unless someone has another solution.

    Thread Starter nootkan

    (@nootkan)

    I figured it out. Here is my final code that prevents urls and email addresses inside my three form fields:

    // No Urls or email addresses allowed in textarea
    
    function custom_textarea_validation_filter($result, $tag) {  
    $type = $tag['type'];
    $name = $tag['name'];
    if($name == 'your-message') {
    $value = $_POST[$name];
    $Match_all = "/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]|[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,8}/";
    if(preg_match($Match_all,$value)){
    
        $result->invalidate( $tag, "Please no urls or email addresses in your message !" );
                    }
    }
    return $result;
    }
    add_filter('wpcf7_validate_textarea','custom_textarea_validation_filter', 10, 2);
    add_filter('wpcf7_validate_textarea*', 'custom_textarea_validation_filter', 10, 2);
    
    // No Urls or email addresses allowed in your name or subject field
    
    function custom_text_validation_filter($result, $tag) {  
    $type = $tag['type'];
    $name = $tag['name'];
    if($name == 'your-name' || 'your-subject') {
    $value = $_POST[$name];
    $Match_all = "/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]|[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,8}/";
    if(preg_match($Match_all,$value)){
    
        $result->invalidate( $tag, "Please no urls or email addresses allowed here!" );
                    }
    }
    return $result;
    }
    add_filter('wpcf7_validate_text','custom_text_validation_filter', 10, 2);
    add_filter('wpcf7_validate_text*', 'custom_text_validation_filter', 10, 2);

    If you have more text fields that need to be added just use the || to add them.
    Probably a way to also include the textarea fields inside the if conditional but I am happy with this solution unless someone post a better one.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Block Urls in forms’ is closed to new replies.