0

So I recently updated my Better WP Security plugin (and may have changed some of the settings, don't remember exactly) and now I'm having a problem where it's blocking the legitimate requests one of my custom plugins uses. I send a request to my website using the file_get_contents() method in order to determine whether a certain URL exists or not. When Better WP Security is enabled, this method is incorrect. When I disable the plugin however, the method works correctly. Any ideas what setting in Better WP Security plugin is blocking this so I don't have to disable the entire plugin?

1
  • This is a legitimate question. It can be answered with a best WP practices answer. Commented Jan 7, 2013 at 22:24

1 Answer 1

1

You shouldn't use file_get_contents() for remote requests. WordPress does the heavy lifting for you when determining what is compatible on your host.

Instead of file_get_contents() use the following to check for 404s:

$request = wp_remote_get($url);
$status = $request['response']['code'];
if($status === 404){
    //do something
}

You can also use wp_remote_retrieve_body($request) to get the contents of the request.

6
  • On second thought, this is still causing some issues. My previous code was: function url_exists($url) { if(@file_get_contents($url,0,NULL,0,1)){return 1;}else{ return 0;} } The replacement code doesn't work for all URLs entered: function url_exists($url) { $request = wp_remote_get($url); $status = $request['response']['code']; if($status === 404){return 0;}else{ return 1;} }
    – tsb8m
    Commented Jan 8, 2013 at 16:12
  • The new code is returning true for 404'd items.
    – tsb8m
    Commented Jan 8, 2013 at 16:23
  • @tsbadorrek Try if($status == 404) instead of the stringent === Commented Jan 8, 2013 at 16:28
  • @tsbadorrek Any luck? Commented Jan 9, 2013 at 7:24
  • No, it doesn't seem to be behaving correctly. It seems like it's never finding that ($status == 404), so it's always returning '1'. I can tell because the way I'm using the code is to display an image if it finds it on the server, otherwise to skip it, and instead it's just trying to display all of the links, regardless of whether or not it finds it on the server.
    – tsb8m
    Commented Jan 11, 2013 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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