Absolutely! Let’s outline and build a basic version of an AffiliatePro Manager WordPress plugin. This plugin will allow you to:
- Register affiliates (users)
- Generate unique affiliate links
- Track clicks and referrals
- View affiliate stats in the admin area
This will be a starter version. You can expand it later with features like payouts, email notifications, etc.
1. Plugin File Structure
Create a folder in wp-content/plugins/ called affiliatepro-manager. Inside, create a file named affiliatepro-manager.php.
2. Plugin Header
Paste this at the top of affiliatepro-manager.php:
<?php
/*
Plugin Name: AffiliatePro Manager
Description: Simple affiliate management plugin for WordPress.
Version: 1.0
Author: Your Name
*/
3. Create Affiliate Custom Post Type
This will let you manage affiliates in the admin area.
// Register Affiliate custom post type
function apm_register_affiliate_cpt() {
$args = array(
'public' => false,
'show_ui' => true,
'label' => 'Affiliates',
'supports' => array('title'),
'menu_icon' => 'dashicons-groups',
);
register_post_type('apm_affiliate', $args);
}
add_action('init', 'apm_register_affiliate_cpt');
4. Generate Affiliate Links
Each affiliate will have a unique link like:
https://yoursite.com/?apm_ref=AFFILIATE_ID
Add this to your plugin:
// Show affiliate link in Affiliate admin page
function apm_affiliate_link_meta_box() {
add_meta_box(
'apm_affiliate_link',
'Affiliate Link',
'apm_affiliate_link_callback',
'apm_affiliate',
'side'
);
}
add_action('add_meta_boxes', 'apm_affiliate_link_meta_box');
function apm_affiliate_link_callback($post) {
$affiliate_id = $post->ID;
$link = add_query_arg('apm_ref', $affiliate_id, home_url());
echo '<input type="text" readonly value="' . esc_attr($link) . '" style="width:100%;" />';
}
5. Track Clicks
Create a database table to store clicks.
// Create table on plugin activation
function apm_create_tables() {
global $wpdb;
$table_name = $wpdb->prefix . 'apm_clicks';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id bigint(20) NOT NULL AUTO_INCREMENT,
affiliate_id bigint(20) NOT NULL,
click_time datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
ip_address varchar(100) DEFAULT '' NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
register_activation_hook(__FILE__, 'apm_create_tables');
Now, log clicks when someone visits with ?apm_ref=ID:
// Track affiliate clicks
function apm_track_affiliate_click() {
if (isset($_GET['apm_ref'])) {
$affiliate_id = intval($_GET['apm_ref']);
global $wpdb;
$table_name = $wpdb->prefix . 'apm_clicks';
$ip = $_SERVER['REMOTE_ADDR'];
$wpdb->insert($table_name, array(
'affiliate_id' => $affiliate_id,
'ip_address' => $ip,
));
}
}
add_action('init', 'apm_track_affiliate_click');
6. Admin Stats Page
Add a submenu to view affiliate stats.
// Add admin menu
function apm_admin_menu() {
add_submenu_page(
'edit.php?post_type=apm_affiliate',
'Affiliate Stats',
'Stats',
'manage_options',
'apm_affiliate_stats',
'apm_affiliate_stats_page'
);
}
add_action('admin_menu', 'apm_admin_menu');
function apm_affiliate_stats_page() {
global $wpdb;
$table_name = $wpdb->prefix . 'apm_clicks';
$affiliates = get_posts(array(
'post_type' => 'apm_affiliate',
'numberposts' => -1,
));
echo '<h2>Affiliate Stats</h2>';
echo '<table class="widefat"><thead><tr><th>Affiliate</th><th>Clicks</th></tr></thead><tbody>';
foreach ($affiliates as $affiliate) {
$count = $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*) FROM $table_name WHERE affiliate_id = %d",
$affiliate->ID
));
echo '<tr><td>' . esc_html($affiliate->post_title) . '</td><td>' . intval($count) . '</td></tr>';
}
echo '</tbody></table>';
}
7. (Optional) Shortcode for Affiliate Registration
If you want users to register as affiliates:
// Affiliate registration form shortcode
function apm_affiliate_registration_form() {
if ($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['apm_affiliate_name'])) {
$name = sanitize_text_field($_POST['apm_affiliate_name']);
$post_id = wp_insert_post(array(
'post_title' => $name,
'post_type' => 'apm_affiliate',
'post_status' => 'publish',
));
if ($post_id) {
return '<p>Thank you for registering! Your affiliate link: <br><input type="text" value="' . esc_attr(add_query_arg('apm_ref', $post_id, home_url())) . '" readonly style="width:100%;" /></p>';
}
}
return '<form method="post">
<label>Your Name: <input type="text" name="apm_affiliate_name" required></label>
<input type="submit" value="Register as Affiliate">
</form>';
}
add_shortcode('apm_affiliate_register', 'apm_affiliate_registration_form');
8. Usage
- Activate the plugin in your WordPress admin.
- Go to Affiliates in the admin menu to add affiliates.
- Use the affiliate link shown in each affiliate’s admin page.
- View Stats under the Affiliates menu.
- Use
[apm_affiliate_register] shortcode on a page to allow public registration.
9. Next Steps
This is a starter plugin. You can expand it with:
- Referral tracking (e.g., on WooCommerce orders)
- Payout management
- Email notifications
- More detailed stats
Let me know if you want to add more features or need this as a ZIP file!