Support

Account

Home Forums Add-ons Options Page get_field options – outside of loop

Helping

get_field options – outside of loop

  • 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:

    <?php
    $productid = htmlspecialchars($_GET['productid']);
    $link = "http://www.productsite.com/" . $productid . "/?ref=affiliateID";
    header("Location:".$link);
    ?>

    However, I am trying to automate cloning these sites and want the affiliate ID portion to be pulled from a custom field in my ACF Options page, instead of being hard coded in the php file.

    Below is the new code I am trying, but it isn’t working, and in my PHP logs I see “PHP Fatal error: Uncaught Error: Call to undefined function get_field()”

    <?php 
    	$productid = htmlspecialchars($_GET['productid']);
    	$affid = get_field('affiliate_id', 'options');
    	$link = "http://www.productsite.com/" . $productid . "/?ref=" . $affid . "";
    	header("Location:".$link);
    ?>

    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.

    https://codex.wordpress.org/AJAX_in_Plugins

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘get_field options – outside of loop’ is closed to new replies.