Reputation: 523
I'm searching for a function that tells me if an URL returns a 404
HTTP error or "Page not found" inside WordPress.
Could be something like is_404($url)
, but this function works without parameters.
Thanks in advance.
Upvotes: 1
Views: 3189
Reputation: 1566
It's possibile achieve that using curl.
EDIT
Add this to your functions.php:
function wpcf_is_404( $url = null ){
$code = '';
if( is_null( $url ) ){
return false;
}else{
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
curl_exec($handle);
$code = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if( $code == '404' ){
return true;
}else{
return false;
}
curl_close($handle);
}
}
and then make a call to wpcf_is_404();
in your template files or in your functions.php to test if the given url returns true ( 404 ) or false ( all other responses )
Hope it helps!
Upvotes: 5