I am currently doing affiliate link redirects using a custom php file inside of a folder (using mod_rewrite in an htaccess file). The product ID is pulled from the URL, and it is then rewritten in htaccess, passed to the php file which has a hard-coded affiliate ID, and posted into the location bar so the user is automatically forwarded to the site.
This has been working fine for a long time, using the following php code:
As I mentioned, this worked fine when I had the affiliate ID manually coded, but now I want to pull it from an ACF field and can’t seem to figure out how to get it to work.
From a long day of research and testing things, I’ve come to the conclusion that this isn’t working because I’m out of the wordpress loop, and I saw some code to include wp-load.php ..but then someone mentioned that you shouldn’t do that. I tried moving everything into a function, but I couldn’t get that to work either. Currently, I have a small workaround where I am passing parameters in the URL for a rewrite, but I’m not a fan of this option, and really want to keep it similar to my old method.
This is because no WP or Plugin files are defined if this file is loaded outside of WP. There isn’t any way that you can do what you are attempting to do without also loading WP. The best way to do this is to call and action function using admin-ajax.php. You can load this file without using AJAX and create an action that outputs what you want where you need it output.