Skip to content

Instantly share code, notes, and snippets.

@PeterBooker
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PeterBooker/973cb45917fa3be07f78 to your computer and use it in GitHub Desktop.
Save PeterBooker/973cb45917fa3be07f78 to your computer and use it in GitHub Desktop.
WordPress URL Validation Function
<?php
/**
* Validate URLs
*
* @param string $url
* @param boolean $strict
* @return string|boolean
*/
function kbso_validate_url( $url, $strict = false ) {
// No URL to Validate
if ( ! isset( $url ) || empty( $url ) ) {
return false;
}
// Check for scheme e.g. http://
$scheme = parse_url( $url, PHP_URL_SCHEME );
// Add scheme if missing
if ( empty( $scheme ) ) {
$url = 'http://' . $url;
}
// Check for a valid URL
if ( ! filter_var( $url, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED ) ) {
return false;
}
// If no strict check, return valid URL
if ( true != $strict ) {
return $url;
}
// Prepare HTTP Request Args
$args = array(
'timeout' => 5,
'sslverify' => false,
);
$max_attempts = apply_filters( 'kbso_validate_url_attempts', 3 );
$attempts = 0;
$code = 0;
while ( $attempts <= $max_attempts && 200 != $code ) {
// Make GET HTTP Request
$response = wp_remote_get( $url, $args );
// Fetch the Response Code - Should be 200
$code = wp_remote_retrieve_response_code( $response );
$attempts++;
}
/*
* Check the Response Code
*/
if ( 200 == $code ) {
return $url;
} else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment