I would like to make a call to the external API before the page is loaded on my WordPress website. It looks somewhat like this:
$si = session_id();
$res = @file_get_contents("https://my-api.com/si_check?si={$si}", false);
if ($res != 'ok')
exit('not ok');
It doesn't have to be php
but what matters is it should send the session id to the api and if the api responds with anything but "ok"
it should stop the page from loading (or maybe redirect somewhere).
I have tried adding this code to functions.php
, index.php
and wp-config.php
based on what other people were recommending, but only functions.php
runs consistently every time the page is loaded, but it fails when I open a page not in the browser, but in code, for example:
import requests
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0'}
res = requests.get('https://my-website.com', headers=headers)
print(res.text)
It is especially important since obviously this code is meant to keep some users out of the website, and bots are a primary target, but the code above completely bypasses the api call and gets the whole html page. Which file can I add this check to so that It will run 100% of the time?