WordPress Plugin Specification for affiliate referral tracking
Developer Integration GuideThis guide explains how to implement affiliate tracking on the WordPress site + WooCommerce, using the existing custom plugin that sends order data to WeFund API.
affiliate_code)affiliate_codeWhen a user lands on a URL like:
https://we-fund.com/register?ref=WEF200751
The ref value should be extracted and stored in a cookie.
wefund_affiliate_ref/)When an order is created, copy the cookie value into order metadata.
affiliate_codeWEF200751Extend the existing payload to include the referral code when sending to the WeFund API.
The affiliate code should be included even if null (when no affiliate referral is present).
function wefund_capture_affiliate_ref() {
if (isset($_GET['ref']) && !empty($_GET['ref'])) {
$ref_code = sanitize_text_field($_GET['ref']);
// Store cookie for 30 days
setcookie('wefund_affiliate_ref', $ref_code, time() + (30*24*60*60), '/');
}
}
add_action('init', 'wefund_capture_affiliate_ref');
add_action('woocommerce_checkout_create_order', function($order, $data) {
if (isset($_COOKIE['wefund_affiliate_ref'])) {
$order->update_meta_data('affiliate_code',
sanitize_text_field($_COOKIE['wefund_affiliate_ref']));
}
}, 10, 2);
// Inside your existing order processing function
$affiliate_code = $order->get_meta('affiliate_code');
$payload = [
'order_id' => $order->get_id(),
'email' => $order->get_billing_email(),
'amount' => $order->get_total(),
'affiliate_code' => $affiliate_code, // Must be included
// ... other fields already sent
];
$response = wp_remote_post('https://api.we-fund.com/orders/', [
'method' => 'POST',
'headers' => ['Content-Type' => 'application/json'],
'body' => wp_json_encode($payload),
'timeout' => 30,
]);
When an order is placed, the plugin should send a payload similar to:
{
"order_id": 12345,
"email": "client@email.com",
"amount": 299.00,
"affiliate_code": "WEF200751"
}
affiliate_code field should always be included in the payload, even if it's null (when no affiliate referral is present).
Always use sanitize_text_field() for safety before saving/using codes.
Do not overwrite affiliate_code if already attached to an order.
Ensure the affiliate_code field is always sent, even if null.
Consider adding appropriate cookie flags for security (httponly, secure).