In a scenario where, say, your plugin or theme has been given a URL. You want to know, is this URL a URL from this very blog? How would you test if it is?


One option could be to use url_to_postid( $url ), which returns a "ID of the post or page that resides at the given URL, or 0 on failure.", https://codex.wordpress.org/Function_Reference/url_to_postid

But this doesn't work with archives, I think. To find out if the url is for an archive, you'd probably need to explode() the path and then do some kind of get_term_by() with the parts.

Perhaps a little less strict way would be to compare the url's host to your domain,

if ( false !== strpos( get_home_url(), parse_url( $some_url, PHP_URL_HOST ) ) ) {
  // do something


if ( parse_url( $some_url, PHP_URL_HOST ) === parse_url( get_home_url(), PHP_URL_HOST ) ) {
  // do something

But doing it this way, you wouldn't know, if the path was correct or not.

| improve this answer | |

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.