WordPress provides a helpful function called is_email() which checks if a given email address is valid. Is there a similar function available to check if a URL is valid?

I tried is_url() but that was just wishful thinking on my part.

Ref: http://codex.wordpress.org/Function_Reference/is_email


Use the native php function Filter Validator

if (filter_var($url, FILTER_VALIDATE_URL) === FALSE) {
    die('Not a valid URL');
  • Nice one and thanks @shanebp - never knew that filter existed! – henrywright Oct 24 '14 at 17:01
  • 2
    FILTER_VALIDATE_URL looks good but has some serious problems: d-mueller.de/blog/… – tobltobs Oct 6 '15 at 16:13
  • @tobltobs: Those are not problems, they are features. The author is complaining that valid URLs are being reported as valid. That's like complaining that PDO is broken because is allows $pdo->query("SELECT pw FROM users WHERE id={$_GET['id']}"). – dotancohen Jul 8 '18 at 14:35
  • You may be right @dotancohen, that those are technically valid URLs. So maybe the problem is really in our question. We're not just wanting "technically valid URLs", we're wanting "safe URLs". eg, URLs that help prevent XSS attacks or other mischief. @orionrush's suggestion avoids a number of those problems, whereas FILTER_VALIDATE_URL does not. – thespacecamel Sep 3 '18 at 17:51

I know this is an old post, but for anyone visiting it's also worth considering the WP functions esc_url() and esc_url_raw(), the latter being safe for databases entries etc, as it does not encode entities. esc_url() does encode entities and so is good for display to users.

In the source you can see that esc_url() checks for a whitelist of allowed protocols and structure and so avoids some of the vulnerabilities of FILTER_VALIDATE_URL noted by the link posed by @tobltobs.


I found wp_http_validate_url quite convenient to check if a string in valid URL or not while working on my project.

Take a reference from here: https://developer.wordpress.org/reference/functions/wp_http_validate_url/

For example:

$val = 'http://somevalidurl.com';
if ( wp_http_validate_url( $val ) ) {

    // It's valid URL;

} else {

    // It's NOT valid URL;


It returns the URL itself if it's valid,else false.

  • This function sounds great, but I think it made a few mistakes with regards to user-submitted data. It found the URL localhost to be invalid; but it found http://example.com/"><script>alert("xss")</script> to be valid. – thespacecamel Sep 3 '18 at 17:57
  • sorry, SO isn't letting me modify my comment. I meant it finds http://localhost invalid. – thespacecamel Sep 3 '18 at 18:05
  • @thespacecamel You can use http_request_host_is_external filter to allow localhost: function allow_some_url( $external, $host, $url ) { return ( $host === 'localhost' ) ? true : $external; } add_filter( 'http_request_host_is_external', 'allow_some_url', 10, 3 ); if ( wp_http_validate_url( 'http://localhost/wordpress/' ) ) { echo 'valid'; } else { echo 'invalid'; } – Ashish Shrestha Sep 4 '18 at 11:33

To ensure user-submitted URL is valid and safe to store and later display, I suggest

esc_url_raw($url) === $url

esc_url_raw(), like @orionrush mentioned above, sanitizes the URL by removing anything invalid or malicious from it. So, if there string has nothing invalid or malicious, then it's ok.


if( esc_url_raw($url) === $url ) {
   // URL is valid. Use it...
} else {
   // URL is invalid or malicious. Don't use it...

I wrote a longer blog post on that here: https://cmljnelson.wordpress.com/2018/08/31/url-validation-in-wordpress/


is_email(), actually do not check if an email address is valid, only if it is conforming to spec. The email I gladly supply for many sites that show pointless interest in my email address is a@b.com which is conforming address but it is unlikely to have anyone using it.

Checking out urls is best done by pinging them. You can try to check conformance, but in theory there are very little limitation to what an encoded url can be.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.