Check if user is an affiliate (affiliatewp)

Home » Snippets » Check if user is an affiliate (affiliatewp)
0

Created with:

Visibility: 

public

Creator: Alex

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php
/**
 * Check if a user is an affiliate.
 *
 * @param int $user_id The ID of the user to check.
 * @return bool Returns true if the user is an affiliate, false otherwise.
 */
function wpturbo_is_user_affiliate( $user_id ) {
    if ( function_exists( 'affiliate_wp' ) && affiliate_wp()->affiliates->is_affiliate( $user_id ) ) {
        return true;
    }

    return false;
}
				

This code defines a function wpturbo_is_user_affiliate that takes a user ID as a parameter. It checks if the AffiliateWP plugin is active and if the user with the given ID is an affiliate. If both conditions are met, it returns true; otherwise, it returns false.

The function first uses the function_exists() check to ensure that the AffiliateWP plugin is active. Then, it calls the is_affiliate() method from the AffiliateWP affiliates object, passing the user ID as an argument. If the is_affiliate() method returns true, indicating that the user is an affiliate, the function returns true. Otherwise, it returns false.

You can use this function in your WordPress plugin or theme to check if a user is an affiliate and perform any additional logic based on that information.

Register an account to save your snippets or go Pro to get more features.