admin管理员组

文章数量:1122846

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("={$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('', 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?

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?

Share Improve this question asked Jul 27, 2024 at 22:04 user9102437user9102437 1135 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

The correct way to do this would be to wrap your custom functionality in a function and hook it to init.

本文标签: phpAdding code before any page loads