Advertisement
chrishajer

Revised code to make sure URL starts with http

Mar 7th, 2013
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. // http://www.gravityhelp.com/forums/topic/custom-input-mask-for-a-valid-url#post-157749
  3. // revised to handle multiple forms and their associated URL fields
  4. add_filter('gform_pre_submission_filter_338', 'force_http_prefix');
  5. function force_http_prefix($form) {
  6.     $formfields = array(
  7.         // add all your form IDs here with their corresponding field ID
  8.         // for example, this will look for field 3 in form 338
  9.         '338' => '3',
  10.         '12' => '3',
  11.         '2' => '10',
  12.         '8' => '7'
  13.         // for all 11 forms ...
  14.         );
  15.  
  16.     // see if the form ID is one of the keys in our array
  17.     // if so, then run the code to check the appropriate field
  18.     if (array_key_exists($form['id'], $formfields)) {
  19.  
  20.         // create a variable for the input we want to modify for this form
  21.         $input_id = 'input_' . $formfields[$form['id']];
  22.         $haystack = strtolower(trim($_POST[$input_id]));
  23.  
  24.         // check for http:// at the beginning of the string
  25.         $needle = 'http://';
  26.  
  27.         // if the http:// is not at the beginning of the string, add it and modify the POST value
  28.         if (substr($haystack, 0, 7) <> $needle) {
  29.             $_POST[$input_id] = $needle . $haystack;
  30.         }
  31.     }
  32.     // return the form either way
  33.     return $form;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement