Below, you’ll find a detailed guide on how to add the Affiliates Shortcodes to your WordPress website, including their parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Affiliates Plugin shortcodes not to show or not to work correctly.
Before starting, here is an overview of the Affiliates Plugin and the shortcodes it provides:
"Affiliates is a WordPress plugin designed to track, manage, and grow your affiliate marketing strategies. This user-friendly tool simplifies the process of monitoring referrals and commissions."
- [affiliates_registration]
- [affiliates_id]
- [referrer_id]
- [referrer_user]
- [referrer]
- [affiliates_is_affiliate]
- [affiliates_is_not_affiliate]
- [affiliates_is_referred]
- [affiliates_is_not_referred]
- [affiliates_hits]
- [affiliates_visits]
- [affiliates_referrals]
- []
- [affiliates_url]
- [affiliates_login_redirect]
- [affiliates_logout]
- [affiliates_fields]
- [affiliates_bloginfo]
- [affiliates_user_meta]
- [affiliates_dashboard_earnings]
Affiliates [affiliates_registration] Shortcode
The ‘Affiliates Registration’ shortcode is a powerful tool that enables user registration for affiliate programs. It uses the ‘affiliates_registration_shortcode’ function to render a registration form on the webpage.
Shortcode: [affiliates_registration]
Examples and Usage
Basic example – A straightforward usage of the shortcode for affiliates registration without any additional parameters.
[affiliates_registration /]
PHP Function Code
In case you have difficulties debugging what causing issues with [affiliates_registration]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'affiliates_registration', array( __CLASS__, 'affiliates_registration_shortcode' ) );
Shortcode PHP function:
function affiliates_registration_shortcode( $atts, $content = null ) {
$options = shortcode_atts( self::$defaults, $atts );
return self::render_form( $options );
}
Code file location:
affiliates/affiliates/lib/core/class-affiliates-registration.php
Affiliates [affiliates_id] Shortcode
The ‘affiliates_id’ shortcode retrieves the active affiliate ID associated with the current user. It checks if the user is an affiliate and then fetches the ID from the database.
Shortcode: [affiliates_id]
Examples and Usage
Basic example – The shortcode ‘affiliates_id’ is used to fetch the affiliate ID of the current logged-in user. If the user is an active affiliate, the affiliate ID is returned; otherwise, an empty string is returned.
[affiliates_id /]
PHP Function Code
In case you have difficulties debugging what causing issues with [affiliates_id]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'affiliates_id', array( __CLASS__, 'affiliates_id' ) );
Shortcode PHP function:
function affiliates_id( $atts, $content = null ) {
global $wpdb;
$output = "";
$user_id = get_current_user_id();
if ( $user_id && affiliates_user_is_affiliate( $user_id ) ) {
$affiliates_table = _affiliates_get_tablename( 'affiliates' );
$affiliates_users_table = _affiliates_get_tablename( 'affiliates_users' );
if ( $affiliate_id = $wpdb->get_var( $wpdb->prepare(
"SELECT $affiliates_users_table.affiliate_id FROM $affiliates_users_table LEFT JOIN $affiliates_table ON $affiliates_users_table.affiliate_id = $affiliates_table.affiliate_id WHERE $affiliates_users_table.user_id = %d AND $affiliates_table.status = 'active'",
intval( $user_id )
))) {
$output .= affiliates_encode_affiliate_id( $affiliate_id );
}
}
return $output;
}
Code file location:
affiliates/affiliates/lib/core/class-affiliates-shortcodes.php
Affiliates [referrer_id] Shortcode
The ‘referrer_id’ shortcode from the Affiliates plugin is designed to fetch and display the ID of the referring affiliate. This shortcode extracts the affiliate ID from the Affiliates_Service class. If the ‘direct’ attribute is set to true or the affiliate ID isn’t the direct ID, it encodes and returns the affiliate ID.
Shortcode: [referrer_id]
Parameters
Here is a list of all possible referrer_id shortcode parameters and attributes:
direct
– Determines if the affiliate ID should be directly encoded
Examples and Usage
Basic example – The shortcode is used to fetch and display the ID of the affiliate who referred the current visitor. No parameters are required for basic usage.
[referrer_id /]
Advanced examples
Using the shortcode with the ‘direct’ parameter set to true. If the affiliate is a direct referral, their ID will be displayed. If not, nothing will be displayed.
[referrer_id direct=true /]
Using the shortcode with the ‘direct’ parameter set to false. Regardless of whether the affiliate is a direct referral or not, their ID will be displayed.
[referrer_id direct=false /]
PHP Function Code
In case you have difficulties debugging what causing issues with [referrer_id]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'referrer_id', array( __CLASS__, 'referrer_id' ) );
Shortcode PHP function:
function referrer_id( $atts, $content = null ) {
$options = shortcode_atts(
array(
'direct' => false
),
$atts
);
extract( $options );
$output = "";
require_once( 'class-affiliates-service.php' );
$affiliate_id = Affiliates_Service::get_referrer_id();
if ( $affiliate_id ) {
if ( $direct || $affiliate_id !== affiliates_get_direct_id() ) {
$output .= affiliates_encode_affiliate_id( $affiliate_id );
}
}
return $output;
}
Code file location:
affiliates/affiliates/lib/core/class-affiliates-shortcodes.php
Affiliates [referrer_user] Shortcode
The ‘referrer_user’ shortcode is a function that retrieves and displays the user information of the referrer affiliate. This shortcode allows you to display the user’s login, nicename, email, URL, or display name.
Shortcode: [referrer_user]
Parameters
Here is a list of all possible referrer_user shortcode parameters and attributes:
direct
– when set to true, it only considers direct referralsdisplay
– determines which user data to display: ‘user_login’, ‘user_nicename’, ‘user_email’, ‘user_url’, or ‘display_name’
Examples and Usage
Basic example – The shortcode ‘referrer_user’ is used to display affiliate user information. By default, it shows the ‘user_login’ of the affiliate user.
[referrer_user /]
Advanced examples
Display the ‘user_nicename’ of the affiliate user. Here, the ‘display’ attribute is set to ‘user_nicename’.
[referrer_user display="user_nicename" /]
Display the ‘user_email’ of the affiliate user. In this case, the ‘display’ attribute is set to ‘user_email’.
[referrer_user display="user_email" /]
Display the ‘user_url’ of the affiliate user. Here, the ‘display’ attribute is set to ‘user_url’.
[referrer_user display="user_url" /]
Display the ‘display_name’ of the affiliate user. In this case, the ‘display’ attribute is set to ‘display_name’.
[referrer_user display="display_name" /]
Display the ‘user_login’ of the direct affiliate user. Here, the ‘direct’ attribute is set to true and the ‘display’ attribute is set to ‘user_login’.
[referrer_user direct="true" display="user_login" /]
PHP Function Code
In case you have difficulties debugging what causing issues with [referrer_user]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'referrer_user', array( __CLASS__, 'referrer_user' ) );
Shortcode PHP function:
function referrer_user( $atts, $content = null ) {
$options = shortcode_atts(
array(
'direct' => false,
'display' => 'user_login'
),
$atts
);
extract( $options );
$output = '';
require_once( 'class-affiliates-service.php' );
$affiliate_id = Affiliates_Service::get_referrer_id();
if ( $affiliate_id ) {
if ( $direct || $affiliate_id !== affiliates_get_direct_id() ) {
if ( $user_id = affiliates_get_affiliate_user( $affiliate_id ) ) {
if ( $user = get_user_by( 'id', $user_id ) ) {
switch( $display ) {
case 'user_login' :
$output .= $user->user_login;
break;
case 'user_nicename' :
$output .= $user->user_nicename;
break;
case 'user_email' :
$output .= $user->user_email;
break;
case 'user_url' :
$output .= $user->user_url;
break;
case 'display_name' :
$output .= $user->display_name;
break;
default :
$output .= $user->user_login;
}
$output = wp_strip_all_tags( $output );
}
}
}
}
return $output;
}
Code file location:
affiliates/affiliates/lib/core/class-affiliates-shortcodes.php
Affiliates [referrer] Shortcode
The ‘referrer’ shortcode from the Affiliates plugin retrieves and displays affiliate information. It uses the ‘referrer’ function to display various data based on the ‘display’ attribute. The shortcode’s attributes include ‘direct’ and ‘display’. ‘Direct’ determines whether the referrer is direct or not, while ‘display’ specifies the type of data to show. This can range from ‘user_login’, ‘name’, ‘id’, ’email’, and more. The shortcode’s output is stripped of all HTML tags for security reasons before being returned. This ensures the displayed data is safe and clean.
Shortcode: [referrer]
Parameters
Here is a list of all possible referrer shortcode parameters and attributes:
direct
– Determines if the referrer is direct or not.display
– Specifies what information to display about the referrer.name
– Displays the name of the affiliate.id
– Displays the unique identifier of the affiliate.email
– Shows the email address of the affiliate.user_id
– Returns the user ID of the affiliate.user_login
– Provides the login username of the affiliate.user_nicename
– Returns the nice name of the affiliate.user_email
– Shows the email of the affiliate’s user account.user_url
– Returns the URL of the affiliate’s user account.display_name
– Displays the display name of the affiliate.
Examples and Usage
Basic example – Display the login name of the referrer.
[referrer display='user_login' /]
Advanced examples
Display the name of the referrer. If the ‘direct’ attribute is set to true, it will directly display the referrer’s name without checking if the affiliate ID is the direct ID or not.
[referrer direct=true display='name' /]
Display the email of the referrer. If the ‘direct’ attribute is set to false, it will check if the affiliate ID is the direct ID or not before displaying the referrer’s email.
[referrer direct=false display='email' /]
Display the user’s nicename. The ‘display’ attribute is set to ‘user_nicename’ to retrieve the nicely formatted name of the user.
[referrer display='user_nicename' /]
Display the user’s display name. The ‘display’ attribute is set to ‘display_name’ to retrieve the display name of the user.
[referrer display='display_name' /]
Display a custom registration field. The ‘display’ attribute is set to the name of the custom registration field. This will retrieve and display the value of the custom registration field for the user.
[referrer display='custom_field_name' /]
PHP Function Code
In case you have difficulties debugging what causing issues with [referrer]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'referrer', array( __CLASS__, 'referrer' ) );
Shortcode PHP function:
function referrer( $atts, $content = null ) {
$options = shortcode_atts(
array(
'direct' => false,
'display' => 'user_login'
),
$atts
);
extract( $options );
$output = '';
require_once( 'class-affiliates-service.php' );
$affiliate_id = Affiliates_Service::get_referrer_id();
if ( $affiliate_id ) {
if ( $direct || $affiliate_id !== affiliates_get_direct_id() ) {
if ( affiliates_check_affiliate_id( $affiliate_id ) ) {
$affiliate = affiliates_get_affiliate( $affiliate_id );
switch( $display ) {
case 'name' :
$output = $affiliate['name'];
break;
case 'id' :
$output = $affiliate['affiliate_id'];
break;
case 'email' :
$output = $affiliate['email'];
break;
default :
if ( $user_id = affiliates_get_affiliate_user( $affiliate_id ) ) {
if ( $user = get_user_by( 'id', $user_id ) ) {
switch( $display ) {
case 'user_id' :
$output = $user->ID;
break;
case 'user_login' :
$output = $user->user_login;
break;
case 'user_nicename' :
$output = $user->user_nicename;
break;
case 'user_email' :
$output = $user->user_email;
break;
case 'user_url' :
$output = $user->user_url;
break;
case 'display_name' :
$output = $user->display_name;
break;
default :
require_once AFFILIATES_CORE_LIB . '/class-affiliates-settings.php';
require_once AFFILIATES_CORE_LIB . '/class-affiliates-settings-registration.php';
$registration_fields = Affiliates_Settings_Registration::get_fields();
unset( $registration_fields['password'] );
if ( !empty( $registration_fields ) && isset( $registration_fields[$display] ) ) {
$output = stripslashes( get_user_meta( $user_id, $display , true ) );
}
}
}
}
}
}
}
}
$output = wp_strip_all_tags( $output );
return $output;
}
Code file location:
affiliates/affiliates/lib/core/class-affiliates-shortcodes.php
Affiliates [affiliates_is_affiliate] Shortcode
The ‘affiliates_is_affiliate’ shortcode checks if the current user is an affiliate. If they are, it returns the content within the shortcode.
Shortcode: [affiliates_is_affiliate]
Examples and Usage
Basic example – The following example demonstrates a simple usage of the ‘affiliates_is_affiliate’ shortcode. It checks if the current user is an affiliate and if so, it will display the content enclosed within the shortcode.
[affiliates_is_affiliate]Welcome, Affiliate![/affiliates_is_affiliate]
Advanced examples – In the advanced usage of the ‘affiliates_is_affiliate’ shortcode, we can add nested shortcodes within it. This allows us to display more complex content to the affiliate users. For example, we could use a table shortcode to display a detailed report to the affiliate.
[affiliates_is_affiliate][table id=1 /][/affiliates_is_affiliate]
Another advanced usage could be to display a contact form to the affiliate users. In this case, the contact form shortcode will only be processed if the current user is an affiliate.
[affiliates_is_affiliate][contact-form id="123" title="Contact form for Affiliates"][/affiliates_is_affiliate]
Please note that the actual output of these examples depends on the plugins used to provide the ‘table’ and ‘contact-form’ shortcodes and the ‘affiliates_user_is_affiliate’ function.
PHP Function Code
In case you have difficulties debugging what causing issues with [affiliates_is_affiliate]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'affiliates_is_affiliate', array( __CLASS__, 'affiliates_is_affiliate' ) );
Shortcode PHP function:
function affiliates_is_affiliate( $atts, $content = null ) {
remove_shortcode( 'affiliates_is_affiliate' );
$content = do_shortcode( $content );
add_shortcode( 'affiliates_is_affiliate', array( __CLASS__, 'affiliates_is_affiliate' ) );
$output = "";
if ( affiliates_user_is_affiliate( get_current_user_id() ) ) {
$output .= $content;
}
return $output;
}
Code file location:
affiliates/affiliates/lib/core/class-affiliates-shortcodes.php
Affiliates [affiliates_is_not_affiliate] Shortcode
The Affiliates_Is_Not_Affiliate shortcode is a conditional element that checks if a user is not an affiliate. The shortcode removes itself, processes nested shortcodes within its content, then re-adds itself. If the current user is not an affiliate, it returns the processed content.
Shortcode: [affiliates_is_not_affiliate]
Examples and Usage
Basic example – The shortcode ‘affiliates_is_not_affiliate’ is used to display content only to users who are not registered as affiliates.
[affiliates_is_not_affiliate] Welcome, prospective affiliate! [/affiliates_is_not_affiliate]
Advanced examples
Utilizing the shortcode to display different messages to non-affiliates and affiliates. If the user is not an affiliate, they will see the content within the ‘affiliates_is_not_affiliate’ shortcode. If they are an affiliate, they will see the content within the ‘affiliates_is_affiliate’ shortcode.
[affiliates_is_not_affiliate] Welcome, prospective affiliate! [/affiliates_is_not_affiliate]
[affiliates_is_affiliate] Welcome back, valued affiliate! [/affiliates_is_affiliate]
In a more complex example, the shortcode can be used in conjunction with other shortcodes. For instance, we could use a shortcode to display a registration form to non-affiliates, and a special offer to existing affiliates.
[affiliates_is_not_affiliate] [registration_form] [/affiliates_is_not_affiliate]
[affiliates_is_affiliate] Check out this special offer just for our affiliates! [/affiliates_is_affiliate]
PHP Function Code
In case you have difficulties debugging what causing issues with [affiliates_is_not_affiliate]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'affiliates_is_not_affiliate', array( __CLASS__, 'affiliates_is_not_affiliate' ) );
Shortcode PHP function:
function affiliates_is_not_affiliate( $atts, $content = null ) {
remove_shortcode( 'affiliates_is_not_affiliate' );
$content = do_shortcode( $content );
add_shortcode( 'affiliates_is_not_affiliate', array( __CLASS__, 'affiliates_is_not_affiliate' ) );
$output = "";
if ( !affiliates_user_is_affiliate( get_current_user_id() ) ) {
$output .= $content;
}
return $output;
}
Code file location:
affiliates/affiliates/lib/core/class-affiliates-shortcodes.php
Affiliates [affiliates_is_referred] Shortcode
The ‘affiliates_is_referred’ shortcode is part of the Affiliates Plugin. It checks if a visitor was referred by an affiliate and returns the content if true.
Shortcode: [affiliates_is_referred]
Examples and Usage
Basic example – A simple usage of the affiliates_is_referred shortcode without any parameters.
[affiliates_is_referred]
Advanced examples
Adding content within the shortcode. This content will be displayed only if the visitor has been referred by an affiliate.
[affiliates_is_referred]Thank you for being referred by one of our affiliates![/affiliates_is_referred]
Using the shortcode within a post or page to display custom content only to referred visitors. Here, we’re using the shortcode to wrap around a special offer, which will only be visible to visitors who were referred by an affiliate.
[affiliates_is_referred]Get a 10% discount on your first purchase![/affiliates_is_referred]
Please note that the ‘affiliates_is_referred’ shortcode does not accept any parameters/atts. It simply checks if the visitor was referred by an affiliate, and if so, it displays the content enclosed within the shortcode. If the visitor was not referred by an affiliate, the enclosed content will not be displayed.
PHP Function Code
In case you have difficulties debugging what causing issues with [affiliates_is_referred]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'affiliates_is_referred', array( __CLASS__, 'affiliates_is_referred' ) );
Shortcode PHP function:
function affiliates_is_referred( $atts, $content = null ) {
remove_shortcode( 'affiliates_is_referred' );
$content = do_shortcode( $content );
add_shortcode( 'affiliates_is_referred', array( __CLASS__, 'affiliates_is_referred' ) );
$output = '';
require_once( 'class-affiliates-service.php' );
$affiliate_id = Affiliates_Service::get_referrer_id();
if ( $affiliate_id ) {
if ( $affiliate_id !== affiliates_get_direct_id() ) {
if ( affiliates_check_affiliate_id( $affiliate_id ) ) {
$output .= $content;
}
}
}
return $output;
}
Code file location:
affiliates/affiliates/lib/core/class-affiliates-shortcodes.php
Affiliates [affiliates_is_not_referred] Shortcode
The ‘affiliates_is_not_referred’ shortcode is a PHP function that checks if a visitor was referred by an affiliate. The function removes the shortcode, processes embedded shortcodes within the content, then re-adds the shortcode. It uses the ‘Affiliates_Service’ class to get the referrer’s ID. If no affiliate ID is found, or if the ID matches the direct referral ID, the original content is returned.
Shortcode: [affiliates_is_not_referred]
Examples and Usage
Basic example – A simple usage of the shortcode which checks if the referrer is not an affiliate and displays the content if true.
[affiliates_is_not_referred]
Advanced examples
Using the shortcode to display a specific message to users who are not referred by an affiliate. The shortcode checks whether the user is referred by an affiliate or not. If not, it displays the content enclosed within the shortcode.
[affiliates_is_not_referred]You are not referred by any affiliate. Enjoy our services![/affiliates_is_not_referred]
Using the shortcode to display a welcome message along with a custom offer for users who are not referred by an affiliate. The shortcode checks whether the user is referred by an affiliate or not. If not, it displays the content enclosed within the shortcode.
[affiliates_is_not_referred]Welcome to our website! Enjoy a 10% discount on your first purchase![/affiliates_is_not_referred]
PHP Function Code
In case you have difficulties debugging what causing issues with [affiliates_is_not_referred]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'affiliates_is_not_referred', array( __CLASS__, 'affiliates_is_not_referred' ) );
Shortcode PHP function:
function affiliates_is_not_referred( $atts, $content = null ) {
remove_shortcode( 'affiliates_is_not_referred' );
$content = do_shortcode( $content );
add_shortcode( 'affiliates_is_not_referred', array( __CLASS__, 'affiliates_is_not_referred' ) );
$output = '';
require_once( 'class-affiliates-service.php' );
$affiliate_id = Affiliates_Service::get_referrer_id();
// it can not be an affiliate and direct doesn't count
if ( ( !$affiliate_id ) || ( $affiliate_id === affiliates_get_direct_id() ) ) {
$output .= $content;
}
return $output;
}
Code file location:
affiliates/affiliates/lib/core/class-affiliates-shortcodes.php
Affiliates [affiliates_hits] Shortcode
The ‘affiliates_hits’ shortcode is designed to display the number of hits an affiliate has made within a specified time frame. The shortcode accepts ‘from’, ‘until’, and ‘for’ as attributes to define the period. It checks if the current user is an affiliate and if they are, it retrieves and displays their hits.
Shortcode: [affiliates_hits]
Parameters
Here is a list of all possible affiliates_hits shortcode parameters and attributes:
from
– Sets the start date for the affiliate hits tracking.until
– Sets the end date for the affiliate hits tracking.for
– Specifies the user for whom the affiliate hits are tracked.
Examples and Usage
Basic example – Displaying the affiliate hits without any date restrictions.
[affiliates_hits /]
Advanced examples
Displaying the affiliate hits starting from a specific date. The ‘from’ parameter defines the start date from which the hits will be calculated. The date should be in ‘YYYY-MM-DD’ format.
[affiliates_hits from='2022-01-01' /]
Displaying the affiliate hits until a specific date. The ‘until’ parameter defines the end date until which the hits will be calculated. The date should be in ‘YYYY-MM-DD’ format.
[affiliates_hits until='2022-12-31' /]
Displaying the affiliate hits for a specific period. The ‘from’ and ‘until’ parameters together define the period for which the hits will be calculated. Both dates should be in ‘YYYY-MM-DD’ format.
[affiliates_hits from='2022-01-01' until='2022-12-31' /]
Displaying the affiliate hits for a specific affiliate. The ‘for’ parameter defines the affiliate for which the hits will be calculated. The affiliate should be identified by the user ID.
[affiliates_hits for='5' /]
Combining all parameters to display the affiliate hits for a specific affiliate and for a specific period. The ‘for’, ‘from’ and ‘until’ parameters together define the affiliate and the period for which the hits will be calculated.
[affiliates_hits for='5' from='2022-01-01' until='2022-12-31' /]
PHP Function Code
In case you have difficulties debugging what causing issues with [affiliates_hits]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'affiliates_hits', array( __CLASS__, 'affiliates_hits' ) );
Shortcode PHP function:
function affiliates_hits( $atts, $content = null ) {
global $wpdb;
require_once AFFILIATES_CORE_LIB . '/class-affiliates-date-helper.php';
remove_shortcode( 'affiliates_hits' );
$content = do_shortcode( $content );
add_shortcode( 'affiliates_hits', array( __CLASS__, 'affiliates_hits' ) );
$output = "";
$options = shortcode_atts(
array(
'from' => null,
'until' => null,
'for' => null
),
$atts
);
extract( $options );
self::for_from_until( $for, $from, $until );
$user_id = get_current_user_id();
if ( $user_id && affiliates_user_is_affiliate( $user_id ) ) {
$affiliates_table = _affiliates_get_tablename( 'affiliates' );
$affiliates_users_table = _affiliates_get_tablename( 'affiliates_users' );
if ( $affiliate_id = $wpdb->get_var( $wpdb->prepare(
"SELECT $affiliates_users_table.affiliate_id FROM $affiliates_users_table LEFT JOIN $affiliates_table ON $affiliates_users_table.affiliate_id = $affiliates_table.affiliate_id WHERE $affiliates_users_table.user_id = %d AND $affiliates_table.status = 'active'",
intval( $user_id )
))) {
$output .= affiliates_get_affiliate_hits( $affiliate_id, $from, $until, true );
}
}
return $output;
}
Code file location:
affiliates/affiliates/lib/core/class-affiliates-shortcodes.php
Affiliates [affiliates_visits] Shortcode
The ‘affiliates_visits’ shortcode from the Affiliates plugin allows you to track and display the visits of each affiliate. It fetches data based on specific time frames (‘from’ and ‘until’) and for a particular affiliate (‘for’). It checks if the current user is an active affiliate and retrieves their visit data.
Shortcode: [affiliates_visits]
Parameters
Here is a list of all possible affiliates_visits shortcode parameters and attributes:
from
– sets the start date for the affiliate visits countuntil
– defines the end date for the affiliate visits countfor
– specifies the user for whom the visits are counted
Examples and Usage
Basic example – Display the affiliate visits without any specific date range
[affiliates_visits /]
Advanced examples
Display the affiliate visits for a specific date range
[affiliates_visits from="2022-01-01" until="2022-01-31" /]
Display the affiliate visits for a specific affiliate
[affiliates_visits for="123" /]
Display the affiliate visits for a specific affiliate and a specific date range
[affiliates_visits for="123" from="2022-01-01" until="2022-01-31" /]
PHP Function Code
In case you have difficulties debugging what causing issues with [affiliates_visits]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'affiliates_visits', array( __CLASS__, 'affiliates_visits' ) );
Shortcode PHP function:
function affiliates_visits( $atts, $content = null ) {
global $wpdb;
remove_shortcode( 'affiliates_visits' );
$content = do_shortcode( $content );
add_shortcode( 'affiliates_visits', array( __CLASS__, 'affiliates_visits' ) );
$output = "";
$options = shortcode_atts(
array(
'from' => null,
'until' => null,
'for' => null
),
$atts
);
extract( $options );
self::for_from_until( $for, $from, $until );
$user_id = get_current_user_id();
if ( $user_id && affiliates_user_is_affiliate( $user_id ) ) {
$affiliates_table = _affiliates_get_tablename( 'affiliates' );
$affiliates_users_table = _affiliates_get_tablename( 'affiliates_users' );
if ( $affiliate_id = $wpdb->get_var( $wpdb->prepare(
"SELECT $affiliates_users_table.affiliate_id FROM $affiliates_users_table LEFT JOIN $affiliates_table ON $affiliates_users_table.affiliate_id = $affiliates_table.affiliate_id WHERE $affiliates_users_table.user_id = %d AND $affiliates_table.status = 'active'",
intval( $user_id )
) ) ) {
$output .= affiliates_get_affiliate_visits( $affiliate_id, $from, $until, true );
}
}
return $output;
}
Code file location:
affiliates/affiliates/lib/core/class-affiliates-shortcodes.php
Affiliates [affiliates_referrals] Shortcode
The ‘affiliates_referrals’ shortcode from the Affiliates plugin is a tool used to fetch and display referral data for a logged-in affiliate. The shortcode retrieves the number of referrals (‘count’) or total earnings (‘total’) for an affiliate within a specified date range. It also allows for the display of different referral statuses like accepted, closed, pending, or rejected. If no data is available, a custom message can be displayed.
Shortcode: [affiliates_referrals]
Parameters
Here is a list of all possible affiliates_referrals shortcode parameters and attributes:
status
– Defines the status of the referralfrom
– Sets the start date for the referral perioduntil
– Sets the end date for the referral periodshow
– Determines what information to display: count or totalcurrency
– Sets the currency for the referral totalsfor
– Specifies the user for whom the referrals are shownif_empty
– Specifies the message to display if no referrals are found
Examples and Usage
Basic example – Displays the number of referrals for the current affiliate
[affiliates_referrals show='count' /]
Advanced examples
Displays the total amount of referrals for the current affiliate in a specific currency
[affiliates_referrals show='total' currency='USD' /]
Shows the number of referrals for the current affiliate within a specific date range
[affiliates_referrals show='count' from='2021-01-01' until='2021-12-31' /]
Displays the total amount of referrals for the current affiliate, with a custom message when there are no referrals
[affiliates_referrals show='total' if_empty='No referrals yet' /]
Shows the number of accepted referrals for the current affiliate
[affiliates_referrals show='count' status='accepted' /]
PHP Function Code
In case you have difficulties debugging what causing issues with [affiliates_referrals]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'affiliates_referrals', array( __CLASS__, 'affiliates_referrals' ) );
Shortcode PHP function:
function affiliates_referrals( $atts, $content = null ) {
global $wpdb;
remove_shortcode( 'affiliates_referrals' );
$content = do_shortcode( $content );
add_shortcode( 'affiliates_referrals', array( __CLASS__, 'affiliates_referrals' ) );
$output = "";
$options = shortcode_atts(
array(
'status' => null,
'from' => null,
'until' => null,
'show' => 'count',
'currency' => null,
'for' => null,
'if_empty' => null
),
$atts
);
extract( $options );
self::for_from_until( $for, $from, $until );
$user_id = get_current_user_id();
if ( $user_id && affiliates_user_is_affiliate( $user_id ) ) {
$affiliates_table = _affiliates_get_tablename( 'affiliates' );
$affiliates_users_table = _affiliates_get_tablename( 'affiliates_users' );
if ( $affiliate_id = $wpdb->get_var( $wpdb->prepare(
"SELECT $affiliates_users_table.affiliate_id FROM $affiliates_users_table LEFT JOIN $affiliates_table ON $affiliates_users_table.affiliate_id = $affiliates_table.affiliate_id WHERE $affiliates_users_table.user_id = %d AND $affiliates_table.status = 'active'",
intval( $user_id )
))) {
switch ( $show ) {
case 'count' :
switch ( $status ) {
case null :
case AFFILIATES_REFERRAL_STATUS_ACCEPTED :
case AFFILIATES_REFERRAL_STATUS_CLOSED :
case AFFILIATES_REFERRAL_STATUS_PENDING :
case AFFILIATES_REFERRAL_STATUS_REJECTED :
$referrals = affiliates_get_affiliate_referrals( $affiliate_id, $from, $until, $status );
break;
default :
$referrals = "";
}
$output .= $referrals;
break;
case 'total' :
if ( $totals = self::get_total( $affiliate_id, $from, $until, $status ) ) {
if ( count( $totals ) > 0 ) {
$output .= '<ul>';
foreach ( $totals as $currency_id => $total ) {
$output .= '<li>';
$output .= apply_filters( 'affiliates_referrals_display_currency', $currency_id );
$output .= ' ';
$output .= apply_filters( 'affiliates_referrals_display_total', number_format_i18n( $total, apply_filters( 'affiliates_referrals_decimals', affiliates_get_referral_amount_decimals( 'display' ) ) ), $total, $currency_id );
$output .= '</li>';
}
$output .= '</ul>';
}
}
if ( !$totals || count( $totals ) === 0 ) {
if ( $if_empty !== null ) {
$output .= '<ul>';
$output .= '<li>';
$output .= apply_filters( 'affiliates_referrals_display_total_none', wp_filter_nohtml_kses( $if_empty ) );
$output .= '</li>';
$output .= '</ul>';
}
}
break;
}
}
}
return $output;
}
Code file location:
affiliates/affiliates/lib/core/class-affiliates-shortcodes.php
Affiliates [null] Shortcode
The ‘affiliates_earnings’ shortcode from the Affiliates plugin displays a table of the affiliate’s monthly earnings. The shortcode: [affiliates_earnings] allows customization of the display through various attributes such as ‘show_paid’, ‘per_page’, ‘page’, ‘order’, and ‘orderby’. The table includes columns for the month, earnings, and if ‘show_paid’ is set to true, it also shows the paid amount. The shortcode also supports pagination for easy navigation through the earnings data.
Shortcode: [null]
Parameters
Here is a list of all possible null shortcode parameters and attributes:
show_paid
– Determines whether to display paid earnings or not.per_page
– Sets the number of earnings to display per page.page
– Specifies the page number to display.order
– Defines the order of displayed earnings, either ascending or descending.orderby
– Sets the parameter by which the earnings are ordered, in this case, by date.
Examples and Usage
Basic example – Display affiliate earnings with default parameters
[affiliates_earnings /]
With this basic usage of the shortcode, you can display the affiliate earnings with default parameters. This will show the earnings of the current logged-in user, ordered by date in descending order, and limited to 12 items per page.
Advanced examples – Customizing the display of affiliate earnings
Display affiliate earnings with ‘show_paid’ attribute set to true
[affiliates_earnings show_paid=true /]
With the ‘show_paid’ attribute set to true, the earnings table will include an additional column showing whether the earnings have been paid.
Display affiliate earnings with a custom number of items per page
[affiliates_earnings per_page=5 /]
By setting the ‘per_page’ attribute to a custom value, you can control the number of earnings displayed per page. In this example, the table will show 5 earnings per page.
Display affiliate earnings in ascending order
[affiliates_earnings order='asc' /]
By setting the ‘order’ attribute to ‘asc’, you can display the earnings in ascending order based on the date.
PHP Function Code
In case you have difficulties debugging what causing issues with [null]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'affiliates_earnings', array( __CLASS__, 'affiliates_earnings' ) );
Shortcode PHP function:
function affiliates_earnings( $atts, $content = null ) {
global $wpdb;
$output = '';
$atts = shortcode_atts( array( 'show_paid' => false, 'per_page' => 12, 'page' => 1, 'order' => 'desc', 'orderby' => 'date' ), $atts );
if ( isset( $_REQUEST['earnings-page'] ) ) {
$atts['page'] = intval( $_REQUEST['earnings-page'] );
}
if ( is_string( $atts['show_paid'] ) ) {
$atts['show_paid'] = strtolower( $atts['show_paid'] );
}
switch ( $atts['show_paid'] ) {
case true :
case 'true' :
case 'yes ':
$atts['show_paid'] = true;
break;
case false :
case 'false' :
case 'no' :
$atts['show_paid'] = false;
break;
default :
$atts['show_paid'] = false;
}
$atts['per_page'] = max( intval( $atts['per_page'] ), 1 );
$atts['page'] = max( intval( $atts['page'] ), 1 );
$atts['order'] = strtolower( $atts['order'] );
switch( $atts['order'] ) {
case 'asc' :
case 'desc' :
break;
default :
$atts['order'] = 'desc';
}
$atts['orderby'] = 'date';
$user_id = get_current_user_id();
if ( $user_id && affiliates_user_is_affiliate( $user_id ) ) {
if ( $affiliate_ids = affiliates_get_user_affiliate( $user_id ) ) {
$cols = 2;
$output .= '<table class="affiliates-earnings">';
$output .= '<thead>';
$output .= '<tr>';
$output .= '<th>';
$output .= __( 'Month', 'affiliates' );
$output .= '</th>';
$output .= '<th>';
$output .= __( 'Earnings', 'affiliates' );
$output .= '</th>';
if ( $atts['show_paid'] ) {
$cols++;
$output .= '<th>';
$output .= __( 'Paid', 'affiliates' );
$output .= '</th>';
}
$output .= '</tr>';
$output .= '</thead>';
$output .= '<tbody>';
$rows = array();
$referrals_table = _affiliates_get_tablename( 'referrals' );
$range = $wpdb->get_row(
"SELECT MIN(datetime) from_datetime, MAX(datetime) thru_datetime FROM $referrals_table WHERE affiliate_id IN (" . implode( ',', $affiliate_ids ) . ") "
);
if ( $range ) {
if ( !empty( $range->from_datetime ) ) { // Covers for NULL when no referrals recorded yet, too.
$t = strtotime( date( 'Y-m-01 00:00:00', strtotime( $range->from_datetime ) ) );
$eom = strtotime( date( 'Y-m-t 23:59:59', time() ) );
while ( $t < $eom ) {
$from = date( 'Y-m', $t ) . '-01 00:00:00';
$thru = date( 'Y-m-t', strtotime( $from ) );
$sums = array();
$sums_paid = array();
foreach( $affiliate_ids as $affiliate_id ) {
// accepted and closed
if ( $totals = self::get_total( $affiliate_id, $from, $thru ) ) {
if ( count( $totals ) > 0 ) {
foreach ( $totals as $currency_id => $total ) {
$sums[$currency_id] = isset( $sums[$currency_id] ) ? Affiliates_Math::add( $sums[$currency_id], $total, affiliates_get_referral_amount_decimals() ) : $total;
}
}
}
// closed
if ( $totals_paid = self::get_total( $affiliate_id, $from, $thru, AFFILIATES_REFERRAL_STATUS_CLOSED ) ) {
if ( count( $totals_paid ) > 0 ) {
foreach ( $totals_paid as $currency_id => $total ) {
$sums_paid[$currency_id] = isset( $sums[$currency_id] ) ? Affiliates_Math::add( $sums[$currency_id], $total, affiliates_get_referral_amount_decimals() ) : $total;
}
}
}
}
$rows[$t] = array( 'from' => $from, 'sums' => $sums, 'sums_paid' => $sums_paid );
$t = strtotime( '+1 month', $t );
}
}
}
// sort rows
if ( $atts['order'] === 'desc' ) {
$rows = array_reverse( $rows );
}
$pages = count( $rows ) / $atts['per_page'];
$offset = $atts['per_page'] * ( $atts['page'] - 1 );
$rows = array_splice( $rows, $offset, $atts['per_page'] );
if ( count( $rows ) > 0 ) {
foreach ( $rows as $t => $row ) {
$from = $row['from'];
$sums = $row['sums'];
$sums_paid = $row['sums_paid'];
$output .= '<tr>';
// month & year
$output .= '<td>';
$output .= date_i18n( __( 'F Y', 'affiliates' ), strtotime( $from ) ); // translators: date format; month and year for earnings display
$output .= '</td>';
// earnings
$output .= '<td>';
if ( count( $sums ) > 1 ) {
$output .= '<ul>';
foreach ( $sums as $currency_id => $total ) {
$output .= '<li>';
$output .= apply_filters( 'affiliates_earnings_display_currency', $currency_id );
$output .= ' ';
$output .= apply_filters( 'affiliates_earnings_display_total', number_format_i18n( $total, apply_filters( 'affiliates_earnings_decimals', affiliates_get_referral_amount_decimals( 'display' ) ) ), $total, $currency_id );
$output .= '</li>';
}
$output .= '</ul>';
} else if ( count( $sums ) > 0 ) {
foreach ( $sums as $currency_id => $total ) {
$output .= apply_filters( 'affiliates_earnings_display_currency', $currency_id );
$output .= ' ';
$output .= apply_filters( 'affiliates_earnings_display_total', number_format_i18n( $total, apply_filters( 'affiliates_earnings_decimals', affiliates_get_referral_amount_decimals( 'display' ) ) ), $total, $currency_id );
}
} else {
$output .= apply_filters( 'affiliates_earnings_display_total_none', __( 'None', 'affiliates' ) );
}
$output .= '</td>';
// paid
if ( $atts['show_paid'] ) {
$output .= '<td>';
if ( count( $sums_paid ) > 1 ) {
$output .= '<ul>';
foreach ( $sums_paid as $currency_id => $total ) {
$output .= '<li>';
$output .= apply_filters( 'affiliates_earnings_display_currency', $currency_id );
$output .= ' ';
$output .= apply_filters( 'affiliates_earnings_display_total', number_format_i18n( $total, apply_filters( 'affiliates_earnings_decimals', affiliates_get_referral_amount_decimals( 'display' ) ) ), $total, $currency_id );
$output .= '</li>';
}
$output .= '</ul>';
} else if ( count( $sums_paid ) > 0 ) {
foreach ( $sums as $currency_id => $total ) {
$output .= apply_filters( 'affiliates_earnings_display_currency', $currency_id );
$output .= ' ';
$output .= apply_filters( 'affiliates_earnings_display_total', number_format_i18n( $total, apply_filters( 'affiliates_earnings_decimals', affiliates_get_referral_amount_decimals( 'display' ) ) ), $total, $currency_id );
}
} else {
$output .= apply_filters( 'affiliates_earnings_display_total_none', __( 'None', 'affiliates' ) );
}
$output .= '</td>';
}
$output .= '</tr>';
}
} else {
$output .= sprintf( '<td colspan="%d">', $cols );
$output .= apply_filters( 'affiliates_earnings_display_total_no_earnings', __( 'There are no earnings yet.', 'affiliates' ) );
$output .= '</td>';
}
$output .= '</tbody>';
$output .= '</table>';
$current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$url = remove_query_arg( 'earnings-page', $current_url );
if ( count( $rows ) > 0 ) {
if ( $atts['page'] > 1 ) {
$output .= sprintf( '<a style="margin: 4px;" class="button" href="%s">%s</a>', esc_url( add_query_arg( 'earnings-page', $atts['page'] - 1, $url ) ), esc_html_x( 'Previous', 'Label used to show previous page of affiliate earnings results', 'affiliates' ) );
}
if ( $atts['page'] < $pages ) {
$output .= sprintf( '<a style="margin: 4px;" class="button" href="%s">%s</a>', esc_url( add_query_arg( 'earnings-page', $atts['page'] + 1, $url ) ), esc_html_x( 'Next', 'Label used to show next page of affiliate earnings results', 'affiliates' ) );
}
}
}
}
return $output;
}
Code file location:
affiliates/affiliates/lib/core/class-affiliates-shortcodes.php
Affiliates [affiliates_url] Shortcode
The Affiliates_URL shortcode is a dynamic tool for WordPress affiliate plugins. It generates an affiliate URL based on the user’s ID and returns the output. This shortcode can return the home URL, the current URL without the affiliate parameter, or the permalink, depending on the ‘url’ attribute. It checks if the current user is an affiliate and generates the affiliate URL accordingly.
Shortcode: [affiliates_url]
Parameters
Here is a list of all possible affiliates_url shortcode parameters and attributes:
url
– Specifies the URL to be used for the affiliate’s link
In the ‘affiliates_url’ shortcode, the ‘url’ parameter has three possible values:
'' (empty string)
– If no value is provided, the URL defaults to the home URL of the website.'current'
– The URL is set to the current page URL without the affiliate parameter.'permalink'
– The URL is set to the permalink of the current post or page.
If a different value is provided, it is ignored and the ‘url’ parameter has no effect. If the user is an active affiliate, the shortcode outputs the affiliate’s URL based on the chosen ‘url’ parameter value. If the user is not an active affiliate, the shortcode outputs an empty string.
Examples and Usage
Basic example – Displaying the home URL of the affiliate
[affiliates_url /]
Advanced examples
Displaying the URL of the current page without the affiliate parameter
[affiliates_url url=current /]
Displaying the permalink of the post or page
[affiliates_url url=permalink /]
Displaying a specific URL as the affiliate link
[affiliates_url url=http://example.com /]
In these examples, the ‘url’ attribute is used to specify the type of URL to be displayed. By default, it displays the home URL. If ‘current’ is specified, it displays the URL of the current page without the affiliate parameter. If ‘permalink’ is specified, it displays the permalink of the post or page. If a specific URL is provided, it displays that URL as the affiliate link.
PHP Function Code
In case you have difficulties debugging what causing issues with [affiliates_url]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'affiliates_url', array( __CLASS__, 'affiliates_url' ) );
Shortcode PHP function:
function affiliates_url( $atts, $content = null ) {
global $wpdb, $wp;
$options = shortcode_atts(
array(
'url' => ''
),
$atts
);
extract( $options );
switch( $url ) {
case '' :
$url = home_url();
break;
case 'current' :
$pname = get_option( 'aff_pname', AFFILIATES_PNAME );
$current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$url = remove_query_arg( $pname, $current_url );
break;
case 'permalink' :
$url = get_permalink();
break;
default :
}
remove_shortcode( 'affiliates_url' );
$content = do_shortcode( $content );
add_shortcode( 'affiliates_url', array( __CLASS__, 'affiliates_url' ) );
$output = '';
$user_id = get_current_user_id();
if ( $user_id && affiliates_user_is_affiliate( $user_id ) ) {
$affiliates_table = _affiliates_get_tablename( 'affiliates' );
$affiliates_users_table = _affiliates_get_tablename( 'affiliates_users' );
if ( $affiliate_id = $wpdb->get_var( $wpdb->prepare(
"SELECT $affiliates_users_table.affiliate_id FROM $affiliates_users_table LEFT JOIN $affiliates_table ON $affiliates_users_table.affiliate_id = $affiliates_table.affiliate_id WHERE $affiliates_users_table.user_id = %d AND $affiliates_table.status = 'active'",
intval( $user_id )
))) {
$content = $content !== null ? trim( $content ) : '';
if ( strlen( $content ) > 0 ) {
// wp_texturize() has already been applied to $content and
// it indiscriminately replaces ampersands with the HTML
// entity & - we need to undo this so path separators
// are not messed up. Note that it does that even though we
// have indicated to exclude affiliates_url via the
// no_texturize_shortcodes filter.
$url = trim( $content );
$url = str_replace( '&', '&', $url );
$url = strip_tags( $url );
$url = preg_replace('/\r|\n/', '', $url );
$url = trim( $url );
}
$output .= affiliates_get_affiliate_url( $url, $affiliate_id );
}
}
return $output;
}
Code file location:
affiliates/affiliates/lib/core/class-affiliates-shortcodes.php
Affiliates [affiliates_login_redirect] Shortcode
The Affiliates Login Redirect shortcode is designed to manage user login redirections. This shortcode ensures that if a user is not logged in, they are redirected to a specified URL. This shortcode checks if a user is logged in. If not, it either redirects them to the URL specified in the shortcode or the current page if no URL is provided.
Shortcode: [affiliates_login_redirect]
Parameters
Here is a list of all possible affiliates_login_redirect shortcode parameters and attributes:
redirect_url
– The URL where users are redirected after logging in.
Examples and Usage
Basic Example – A shortcode for the affiliates login redirect without any parameters. This will redirect the user to the current page after login.
[affiliates_login_redirect /]
Advanced Examples
A shortcode for the affiliates login redirect with a specific redirect URL. This will redirect the user to the specified URL after login.
[affiliates_login_redirect redirect_url="http://example.com/welcome" /]
A shortcode for the affiliates login redirect with a dynamic redirect URL. This will redirect the user to a URL based on the page ID after login.
[affiliates_login_redirect redirect_url="" /]
Note: In the last example, replace 123 with the ID of the page where you want to redirect the user after login.
PHP Function Code
In case you have difficulties debugging what causing issues with [affiliates_login_redirect]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'affiliates_login_redirect', array( __CLASS__, 'affiliates_login_redirect' ) );
Shortcode PHP function:
function affiliates_login_redirect( $atts, $content = null ) {
extract( shortcode_atts( array( 'redirect_url' => '' ), $atts ) );
$form = '';
if ( !is_user_logged_in() ) {
if ( empty( $redirect_url ) ) {
$redirect_url = get_permalink();
}
$form = wp_login_form( array( 'echo' => false, 'redirect' => $redirect_url ) );
}
return $form;
}
Code file location:
affiliates/affiliates/lib/core/class-affiliates-shortcodes.php
Affiliates [affiliates_logout] Shortcode
The Affiliates Logout shortcode enables a user-friendly log out feature for your WordPress site. This shortcode checks if a user is logged in. If they are, it displays a ‘Log out’ link. Otherwise, it returns an empty string. This adds simplicity and convenience to your user’s experience.
Shortcode: [affiliates_logout]
Examples and Usage
Basic example – A simple usage of the affiliates logout shortcode. It doesn’t require any parameters and will display a logout link if the user is logged in.
[affiliates_logout /]
Advanced examples
Unfortunately, the given shortcode does not accept any parameters/attributes. Therefore, it’s not possible to provide an advanced example with more than two parameters. The shortcode simply checks if a user is logged in and returns a logout link if true. Otherwise, it returns an empty string.
However, if you wish to customize the shortcode, you can modify the function within your PHP file and add your own parameters. For example, you could add a parameter to change the text of the logout link.
Here is an example of how you could modify the function to accept a ‘text’ parameter:
function affiliates_logout( $atts, $content = null ) {
$atts = shortcode_atts(
array(
'text' => __( 'Log out', 'affiliates' ),
), $atts, 'affiliates_logout' );
if ( is_user_logged_in() ) {
return '' . $atts['text'] . '';
} else {
return '';
}
}
With this change, you can now use the ‘text’ parameter in your shortcode to change the logout link text:
[affiliates_logout text="Sign out" /]
PHP Function Code
In case you have difficulties debugging what causing issues with [affiliates_logout]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'affiliates_logout', array( __CLASS__, 'affiliates_logout' ) );
Shortcode PHP function:
function affiliates_logout( $atts, $content = null ) {
if ( is_user_logged_in() ) {
return '<a href="' . esc_url( wp_logout_url() ) .'">' . __( 'Log out', 'affiliates' ) . '</a>';
} else {
return '';
}
}
Code file location:
affiliates/affiliates/lib/core/class-affiliates-shortcodes.php
Affiliates [affiliates_fields] Shortcode
The ‘affiliates_fields’ shortcode from the Affiliates plugin enables user-specific fields to be displayed and edited. It checks if a user is logged in and an affiliate, then displays their fields.
Shortcode: [affiliates_fields]
Parameters
Here is a list of all possible affiliates_fields shortcode parameters and attributes:
edit
– allows changes to the affiliate’s details when set to ‘yes’load_styles
– loads the plugin’s default styles when set to ‘yes’name
– defines specific fields to be shown in the formuser_id
– targets a specific user, uses current user’s ID if not specified
Examples and Usage
Basic example – Displaying affiliate fields for the current user
[affiliates_fields /]
Advanced examples
Displaying affiliate fields for the current user with the option to edit the fields and load the associated styles
[affiliates_fields edit="yes" load_styles="yes" /]
Specifying the user by ID and only showing specific fields (in this case ‘username’ and ’email’)
[affiliates_fields user_id="123" name="username,email" /]
Setting the ‘edit’ attribute to ‘no’ to make the fields read-only, and disabling style loading
[affiliates_fields edit="no" load_styles="no" /]
These examples demonstrate the flexibility of the ‘affiliates_fields’ shortcode. By adjusting the attributes, you can control which fields are displayed, whether they can be edited, and whether the associated styles are loaded.
PHP Function Code
In case you have difficulties debugging what causing issues with [affiliates_fields]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'affiliates_fields', array( __CLASS__, 'affiliates_fields' ) );
Shortcode PHP function:
function affiliates_fields( $atts, $content = null ) {
global $wpdb;
$output = '';
if ( is_user_logged_in() ) {
$atts = shortcode_atts(
array(
'edit' => 'yes',
'load_styles' => 'yes',
'name' => '',
'user_id' => null
),
$atts
);
$atts['load_styles'] = strtolower( trim( $atts['load_styles' ] ) );
if ( $atts['load_styles'] == 'yes' ) {
wp_enqueue_style( 'affiliates-fields' );
}
$atts['edit'] = strtolower( trim( $atts['edit' ] ) );
$fields = null;
if ( !empty( $atts['name'] ) ) {
$fields = array_map( 'strtolower', array_map( 'trim', explode( ',', $atts['name'] ) ) );
}
if ( current_user_can( AFFILIATES_ADMINISTER_AFFILIATES ) && !empty( $atts['user_id'] ) ) {
$user_id = intval( trim( $atts['user_id'] ) );
} else {
$user_id = get_current_user_id();
}
$user = get_user_by( 'id', $user_id );
if ( affiliates_user_is_affiliate( $user_id ) ) {
require_once AFFILIATES_CORE_LIB . '/class-affiliates-settings.php';
require_once AFFILIATES_CORE_LIB . '/class-affiliates-settings-registration.php';
$registration_fields = Affiliates_Settings_Registration::get_fields();
if ( $atts['edit'] != 'yes' ) {
unset( $registration_fields['password'] );
}
if ( !empty( $fields ) ) {
$_registration_fields = array();
foreach( $fields as $name ) {
if ( isset( $registration_fields[$name] ) ) {
$_registration_fields[$name] = $registration_fields[$name];
}
}
$registration_fields = $_registration_fields;
}
// handle form submission
if ( $atts['edit'] === 'yes' ) {
if ( !empty( $_POST['affiliate-nonce'] ) && wp_verify_nonce( $_POST['affiliate-nonce'], 'save' ) ) {
if ( !empty( $registration_fields ) ) {
$error = false;
// gather field values
foreach( $registration_fields as $name => $field ) {
if ( $field['enabled'] ) {
$value = isset( $_POST[$name] ) ? $_POST[$name] : '';
$value = Affiliates_Utility::filter( $value );
if ( $field['required'] && empty( $value ) && !( is_user_logged_in() && isset( $field['type'] ) && $field['type'] == 'password' ) ) {
$error = true;
$output .= '<div class="error">';
$output .= __( '<strong>ERROR</strong>', 'affiliates' );
$output .= ' : ';
$output .= sprintf( __( 'Please fill out the field <em>%s</em>.', 'affiliates' ), $field['label'] );
$output .= '</div>';
}
$registration_fields[$name]['value'] = $value;
// password check
$type = isset( $field['type'] ) ? $field['type'] : 'text';
if ( $type == 'password' ) {
if ( !empty( $value ) ) {
$value2 = isset( $_POST[$name . '2'] ) ? $_POST[$name . '2'] : '';
$value2 = Affiliates_Utility::filter( $value2 );
if ( $value !== $value2 ) {
$error = true;
$output .= '<div class="error">';
$output .= __( '<strong>ERROR</strong>', 'affiliates' );
$output .= ' : ';
$output .= sprintf( __( 'The passwords for the field <em>%s</em> do not match.', 'affiliates' ), $field['label'] );
$output .= '</div>';
}
}
}
}
}
$userdata = array();
foreach( $registration_fields as $name => $field ) {
if ( $registration_fields[$name]['enabled'] ) {
$userdata[$name] = $registration_fields[$name]['value'];
}
}
if ( !$error ) {
$updated_user_id = Affiliates_Registration::update_affiliate_user( $user_id, $userdata );
if ( is_wp_error( $updated_user_id ) ) {
$error_messages = implode( '<br/>', $updated_user_id->get_error_messages() );
if ( !empty( $error_messages ) ) {
$output .= '<div class="error">';
$output .= $error_messages;
$output .= '</div>';
}
} else {
$output .= '<div class="updated">';
$output .= __( 'Saved', 'affiliates' );
$output .= '</div>';
}
}
}
}
}
// get user again in case anything changed as we're using it below
$user = get_user_by( 'id', $user_id );
// show form
$n = 0;
if ( !empty( $registration_fields ) ) {
if ( $atts['edit'] === 'yes' ) {
$output .= '<form class="affiliates-fields" method="post">';
$output .= '<div>';
} else {
$output .= '<div class="affiliates-fields">';
$output .= '<div>';
}
foreach( $registration_fields as $name => $field ) {
if ( $field['enabled'] ) {
$label = $field['label'];
if ( defined( 'AFFILIATES_WPML' ) && AFFILIATES_WPML ) {
// original value, domain, name, language code (optional and not used here)
$label = apply_filters( 'wpml_translate_single_string', $field['label'], 'affiliates', Affiliates_Registration::get_wpml_string_name( $name ) );
}
$n++;
$output .= '<div class="field">';
$output .= '<label>';
$output .= esc_html( stripslashes( $label ) );
$type = isset( $field['type'] ) ? $field['type'] : 'text';
$extra = $atts['edit'] != 'yes' ? ' readonly="readonly" ' : '';
switch( $name ) {
case 'user_login' :
$extra .= ' readonly="readonly" ';
$value = $user->user_login;
break;
case 'user_email' :
$value = $user->user_email;
break;
case 'user_url' :
$value = $user->user_url;
break;
case 'password' :
$value = '';
break;
default :
$value = get_user_meta( $user_id, $name , true );
if ( empty( $value ) && class_exists( 'Affiliates_Attributes' ) ) {
if ( $name === 'paypal_email' || $name === 'payment_email' ) {
if ( $affiliate_ids = affiliates_get_user_affiliate( $user_id ) ) {
if ( $affiliate_id = array_shift( $affiliate_ids ) ) {
$affiliates_attributes_table = _affiliates_get_tablename( 'affiliates_attributes' );
$payment_email = $wpdb->get_var( $wpdb->prepare(
"SELECT attr_value FROM $affiliates_attributes_table WHERE affiliate_id = %d AND attr_key = 'paypal_email'",
$affiliate_id
) );
if ( !empty( $payment_email ) ) {
update_user_meta( $user_id, $name, $payment_email );
$value = get_user_meta( $user_id, $name, true );
}
}
}
}
}
}
$output .= sprintf(
'<input type="%s" class="%s" name="%s" value="%s" %s %s />',
esc_attr( $type ),
'regular-text ' . esc_attr( $name ) . ( $type != 'password' && $field['required'] ? ' required ' : '' ),
esc_attr( $name ),
esc_attr( stripslashes( $value ) ),
( $type != 'password' && $field['required'] ) ? ' required="required" ' : '',
$extra
);
$output .= '</label>';
$output .= '</div>';
if ( $type == 'password' ) {
// the second passwort field is also not required
$output .= '<div class="field">';
$output .= '<label>';
$output .= sprintf( __( 'Repeat %s', 'affiliates' ), esc_html( stripslashes( $label ) ) );
$output .= sprintf(
'<input type="%s" class="%s" name="%s" value="%s" %s %s />',
esc_attr( $type ),
'regular-text ' . esc_attr( $name ),
esc_attr( $name . '2' ),
esc_attr( $value ),
'',
$extra
);
$output .= '</label>';
$output .= '</div>';
}
}
}
if ( $atts['edit'] === 'yes' ) {
$output .= wp_nonce_field( 'save', 'affiliate-nonce', true, false );
$output .= '<div class="save">';
$output .= sprintf( '<input class="button" type="submit" name="save" value="%s" />', __( 'Save', 'affiliates' ) );
$output .= '</div>';
$output .= '</div>';
$output .= '</form>';
} else {
$output .= '</div>';
$output .= '</div>';
}
}
}
}
return $output;
}
Code file location:
affiliates/affiliates/lib/core/class-affiliates-shortcodes.php
Affiliates [affiliates_bloginfo] Shortcode
The ‘affiliates_bloginfo’ shortcode retrieves information about your blog. It accepts ‘key’ and ‘filter’ attributes, allowing you to specify what blog info to retrieve and how to sanitize it.
Shortcode: [affiliates_bloginfo]
Parameters
Here is a list of all possible affiliates_bloginfo shortcode parameters and attributes:
key
– Defines the specific piece of blog information to retrieve.filter
– Specifies how to sanitize the blog information for safe output.
Examples and Usage
Basic example – In this example, we are using the ‘affiliates_bloginfo’ shortcode to retrieve the blog name.
[affiliates_bloginfo key="name" /]
Advanced examples
In this advanced example, we are using the ‘affiliates_bloginfo’ shortcode to retrieve the blog’s URL and we are applying the ‘esc_url’ filter to ensure it’s a safe and valid URL.
[affiliates_bloginfo key="url" filter="esc_url" /]
Here’s another advanced example where we are fetching the blog’s character set and applying the ‘esc_attr’ filter to escape any special characters.
[affiliates_bloginfo key="charset" filter="esc_attr" /]
PHP Function Code
In case you have difficulties debugging what causing issues with [affiliates_bloginfo]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'affiliates_bloginfo', array( __CLASS__, 'affiliates_bloginfo' ) );
Shortcode PHP function:
function affiliates_bloginfo( $atts, $content = null ) {
$options = shortcode_atts(
array(
'key' => '',
'filter' => 'esc_html'
),
$atts
);
$key = $options['key'];
$filter = $options['filter'];
$result = get_bloginfo( $key );
switch ( $filter ) {
case 'esc_attr' :
$result = esc_attr( $result );
break;
case 'esc_url' :
$result = esc_url( $result );
break;
default :
$result = esc_html( $result );
}
return $result;
}
Code file location:
affiliates/affiliates/lib/core/class-affiliates-shortcodes.php
Affiliates [affiliates_user_meta] Shortcode
The Affiliates User Meta shortcode is used to retrieve a specific user’s meta data. This shortcode only works if the user is logged in and a key is provided. It fetches the meta data associated with the key for the current user. The output is then escaped for safe rendering.
Shortcode: [affiliates_user_meta]
Parameters
Here is a list of all possible affiliates_user_meta shortcode parameters and attributes:
key
– The specific user metadata to retrieve
Examples and Usage
Basic example – The shortcode is used to display a specific user meta data by referencing the key parameter.
[affiliates_user_meta key="first_name" /]
Advanced examples
Here, the shortcode is used to display the logged-in user’s nickname. If the user is not logged in, it will not return anything. This is useful if you want to personalize your site for your users.
[affiliates_user_meta key="nickname" /]
In this advanced example, the shortcode is used to display the logged-in user’s email. This can be useful for a variety of reasons, such as allowing users to confirm their email address or to provide a contact point for other users.
[affiliates_user_meta key="user_email" /]
PHP Function Code
In case you have difficulties debugging what causing issues with [affiliates_user_meta]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'affiliates_user_meta', array( __CLASS__, 'affiliates_user_meta' ) );
Shortcode PHP function:
function affiliates_user_meta( $atts, $content = null ) {
$output = "";
$options = shortcode_atts(
array(
'key' => ''
),
$atts
);
extract( $options );
$output = '';
if ( is_user_logged_in() && ( $key !== '' ) ) {
if ( $user_id = get_current_user_id() ) {
$output = get_user_meta( $user_id, $key, true );
}
}
return esc_html( $output );
}
Code file location:
affiliates/affiliates/lib/core/class-affiliates-shortcodes.php
Affiliates [affiliates_dashboard_earnings] Shortcode
The Affiliates Dashboard Earnings shortcode is designed to display the earnings section on the affiliate’s dashboard. This shortcode checks if the user is an affiliate, then generates the earnings section.
Shortcode: [affiliates_dashboard_earnings]
Examples and Usage
Basic example – Embeds the affiliate’s dashboard earnings section in a post or page.
[affiliates_dashboard_earnings /]
PHP Function Code
In case you have difficulties debugging what causing issues with [affiliates_dashboard_earnings]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'affiliates_dashboard_earnings', array( __CLASS__, 'shortcode' ) );
Shortcode PHP function:
function shortcode( $atts, $content = '' ) {
$output = '';
if ( affiliates_user_is_affiliate() ) {
/**
* @var Affiliates_Dashboard_Earnings $section
*/
$section = Affiliates_Dashboard_Section_Factory::get_section_instance( Affiliates_Dashboard_Earnings::get_key() );
ob_start();
$section->render();
$output = ob_get_clean();
}
return $output;
}
Code file location:
affiliates/affiliates/lib/core/dashboard/class-affiliates-dashboard-earnings-shortcode.php
Conclusion
Now that you’ve learned how to embed the Affiliates Plugin shortcodes, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.
Leave a Reply