This is a bit of a confusing task, but I think I nearly have it figured out and need some help to get it over the finish line.
I’m using GoAffPro (API documentation: https://api.goaffpro.com/docs/admin/) to manage our affiliate network.
Members sign up on goaffpro and I’d like them to get a duplicated landing page on our main WordPress site with their referral link placed inside.
This is the code that I have so far in functions.php and it isn’t working.
// Add custom endpoint
add_action('init', 'custom_add_endpoint'); function custom_add_endpoint() {
add_rewrite_rule('^goaffpro-webhook/?', 'index.php?goaffpro_webhook=1', 'top'); }
// Add query var for the endpoint
add_filter('query_vars', 'custom_add_query_vars'); function custom_add_query_vars($vars) {
$vars[] = 'goaffpro_webhook';
return $vars; }
// Flush rewrite rules on activation function custom_flush_rewrite_rules() {
custom_add_endpoint();
flush_rewrite_rules(); }
register_activation_hook(__FILE__, 'custom_flush_rewrite_rules'); register_deactivation_hook(__FILE__, 'flush_rewrite_rules');
// Handle the endpoint
add_action('template_redirect', 'custom_handle_webhook'); function custom_handle_webhook() {
if (get_query_var('goaffpro_webhook')) { $payload = file_get_contents('php://input'); $data = json_decode($payload, true);
if ($data && isset($data['affiliate_id'])) {
$affiliate_id = $data['affiliate_id']; create_landing_page_for_goaffpro_member($affiliate_id);
}
wp_die(); }
}
function get_goaffpro_affiliate_details($affiliate_id) { $access_token =
'APIACCESSTOKEN'; // Consider storing this securely
$response = wp_remote_get("https://api.goaffpro.com/v1/admin/affiliates/{$affiliate_id}", array(
'headers' => array(
'Authorization' => 'Bearer ' . $access_token
) ));
if (is_wp_error($response)) {
error_log('API request error: ' . print_r($response->get_error_message(), true)); return false;
}
$affiliate_details = json_decode(wp_remote_retrieve_body($response), true);
if (isset($affiliate_details['affiliate'])) { return $affiliate_details['affiliate'];
}
return false; }
function create_landing_page_for_goaffpro_member($affiliate_id) { // Retrieve affiliate details from GoAffPro API
$affiliate_details = get_goaffpro_affiliate_details($affiliate_id);
if ($affiliate_details) {
$user_email = $affiliate_details['email'];
$user_login = $affiliate_details['name'];
$referral_code = $affiliate_details['ref_code'];
$network_link = "https://mqaz0jeik52e.goaffpro.com/create-account?ref=" . $referral_code;
// Create a new landing page $post_id = wp_insert_post(array(
'post_title' => 'Landing Page for ' . $user_login,
'post_content' => '[elementor-template id="affiliate-auto"]', // Adjust this if needed 'post_status' => 'publish',
'post_type' => 'page', // Change to your specific post type if different
));
// Update post meta with the network link update_post_meta($post_id, 'network_link', $network_link);
// Send email to the user
send_landing_page_email($user_email, $post_id); }
}
function send_landing_page_email($user_email, $post_id) { $landing_page_url = get_permalink($post_id);
$subject = 'Your New Landing Page';
$message = 'Hi, your new landing page is ready: ' . $landing_page_url; wp_mail($user_email, $subject, $message);
}
// Shortcode to display network link function network_link_shortcode($atts) {
$network_link = get_post_meta(get_the_ID(), 'network_link', true);
return '<a href="' . esc_url($network_link) . '" class="elementor-button-link elementor-button elementor-size-md">Sign Up Here</a>';
}
add_shortcode('network_link_button', 'network_link_shortcode');
I expected this to work but it didn’t.
Jack Bontatibus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.