Top SEM and SEO Tips    

Archive for February, 2012

Simple WordPress Contact Form 7 Phone Validation

Tuesday, February 14th, 2012

I looked around for a simple Contact Form 7 phone validation however all the solutions I found relied on hacking the Contact Form 7 code. By looking through the CF7 code I found a way to attach a simple function to the validation to allow custom validation of any input type you like

To plug into the Contact Form 7 validation there are some filters you can attach to. The built in validation functions do this so we’re copying the standard CF7 way of validation.

All of the code given here can be put into the functions.php file in your wordpress theme.


add_filter( 'wpcf7_validate_text', 'drc_wpcf7_validate_text' , 10, 2 );
add_filter( 'wpcf7_validate_text*', 'drc_wpcf7_validate_text' , 10, 2 );

This code attaches to two filter calls – one for a text field (wpcf7_validate_text) and one for a required text field (wpcf7_validate_text* – note the asterisk at the end).


function drc_wpcf7_validate_text( $result, $tag ) {
$type = $tag['type'];
$name = $tag['name'];
$value = $_POST[$name] ;

if ( strpos( $name , 'phone' ) !== false ){
$regex = '/^(?:1(?:[. -])?)?(?:\((?=\d{3}\)))?([2-9]\d{2})(?:(?<=\(\d{3})\))? ?(?:(?<=\d{3})[.-])?([2-9]\d{2})[. -]?(\d{4})(?: (?i:ext)\.? ?(\d{1,5}))?$/';
$Valid = preg_match($regex,  $value, $matches );
if ( $Valid > 0 ) {
$formatted = "($matches[1]) $matches[2]-$matches[3]";
if ($matches[4]) $formatted .= " x$matches[4]";
// Replace the value passed in with the cleaned up version.
$_POST[$name] = $formatted ;
}
else {
$result['valid'] = false;
$result['reason'][$name] = 'Phone field invalid';
}
}

return $result;
}

This simple routine gets called to validate all text fields in your form. The strpos looks for a field name containing ‘phone’ i.e. ‘your-phonenumber’ and will validate that. This is very simple to extend to Zip codes or any other text field you would like validated. By setting $result['valid'] to false we tell Contact Form 7 that the validation failed, the value you set for $result['reason'][$name] is the message that is displayed to the user of the site.



What to do if WordPress site just shows a blank page

Tuesday, February 7th, 2012

Sometimes a poor setup for wordpress can cause the site to return a blank screen. If you try accessing through /wp-admin/ and it still returns a blank screen then there are generally three causes:

  1. A broken .htaccess file
  2. A broken theme
  3. A plugin causing issues

Go through the following steps to fix this issue:

  1. Try removing the .htaccess in the root directory and see if the site works
  2. Remove the main theme directory (using FTP access) and see if the site works.
  3. If this fails turn off your plugins (by deleting them using FTP if you can’t get in through wp-admin) and again see if the site works.
  4. If these steps fail you will need to look at your site’s error log and see if anything is output there.Other causes I’ve seen are bad installations with missing files, unsupported versions of PHP/MySQL – your error log should tell you what is going wrong.

Once you have your site working you will need to take the following steps to put things back to normal:

  1. Through the wp-admin system save the Permalinks setup again (will recreate the .htaccess file)
  2. Look through the themes and ensure only those themes you are using exist on the site. I always leave the twenty-ten or twenty-eleven theme there too in case something happens to the main theme. If the theme caused the problem you will need to debug what went wrong – use your web server’s error log to look for php errors. If you can not debug yourself then switch to another theme.
  3. Look through the plugins and ensure only those plugins you are using exist on the site.

I would also harden your site – look for rouge directories in the root and in /wp-content – I once found one directory labeled backup that wasn’t a backup directory and that was obvious when the directory was opened. Remove unnecessary theme directories, remove unnecessary plugins. Ensure the WordPress software and plugins are up to date. Install ‘Bad Behavior’ which will block people trying to hack your forms.