Action
uncanny-automator
slicewp_register_affiliate
Fires after an affiliate is successfully registered, providing access to the affiliate's ID for further processing.
add_action( 'slicewp_register_affiliate', $callback, 10, 1 );
Description
Fires after a new affiliate is successfully registered in SliceWP. Developers can use this action to perform custom actions when an affiliate account is created, such as sending custom welcome emails, updating affiliate profiles, or integrating with other services. The `$affiliate_id` is passed for manipulation.
Usage
add_action( 'slicewp_register_affiliate', 'your_function_name', 10, 1 );
Parameters
-
$affiliate_id(mixed) - This parameter represents the unique identifier of the affiliate being registered.
Examples
/**
* Example: Log affiliate registration details and potentially notify an admin.
*
* This callback function hooks into the 'slicewp_register_affiliate' action.
* When a new affiliate is successfully registered in SliceWP, this function
* will be triggered. It logs the affiliate's ID and, if the affiliate is
* marked for a welcome email, it sends a notification to the site administrator.
*/
add_action(
'slicewp_register_affiliate',
function ( $affiliate_id ) {
// Log the affiliate ID to the WordPress debug log for auditing.
// In a real-world scenario, you might want more sophisticated logging
// or error handling.
error_log( 'SliceWP Affiliate Registered: Affiliate ID ' . print_r( $affiliate_id, true ) );
// Example: Send an email notification to the administrator about the new affiliate.
// This assumes a welcome email is intended, as implied by the source context.
$admin_email = get_option( 'admin_email' );
if ( $admin_email ) {
$subject = 'New SliceWP Affiliate Registered';
$message = sprintf(
__( 'A new affiliate has been registered in SliceWP with the ID: %d. You may want to send them a welcome email or onboard them.', 'your-text-domain' ),
$affiliate_id
);
$headers = array( 'Content-Type: text/html; charset=UTF-8' );
wp_mail( $admin_email, $subject, $message, $headers );
}
// This is an action hook, so no return value is strictly necessary for the hook itself.
// However, if this were a filter, a return statement would be required.
},
10, // Default priority
1 // Number of accepted arguments ($affiliate_id)
);
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/integrations/slicewp/actions/slicewp-create-affiliate.php:124
protected function process_action( $user_id, $action_data, $recipe_id, $args, $parsed ) {
$email = isset( $parsed['user_email'] ) ? sanitize_email( $parsed['user_email'] ) : '';
$payment_email = isset( $parsed['payment_email'] ) ? sanitize_email( $parsed['payment_email'] ) : '';
$website = isset( $parsed['website'] ) ? sanitize_email( $parsed['website'] ) : '';
$promotional_methods = isset( $parsed['promotional_methods'] ) ? wp_kses_post( $parsed['promotional_methods'] ) : '';
$status = isset( $parsed['status'] ) ? sanitize_text_field( $parsed['status'] ) : '';
$welcome_email = isset( $parsed['welcome_email'] ) ? sanitize_text_field( $parsed['welcome_email'] ) : '';
$welcome_email = ( 'true' === $welcome_email ) ? true : false;
$existing_user = email_exists( $email );
if ( false === $existing_user ) {
$this->add_log_error( sprintf( 'The provided email: %s does not exist.', $email ) );
return false;
}
$user_id = $existing_user;
if ( true === slicewp_is_user_affiliate( $user_id ) ) {
$this->add_log_error( sprintf( 'The user: %s is already an affiliate.', $email ) );
return false;
}
$affiliate_data = array(
'user_id' => absint( $user_id ),
'date_created' => slicewp_mysql_gmdate(),
'date_modified' => slicewp_mysql_gmdate(),
'payment_email' => sanitize_email( $payment_email ),
'website' => esc_url( $website ),
'status' => $status,
);
$affiliate_id = slicewp_insert_affiliate( $affiliate_data );
if ( empty( $affiliate_id ) ) {
$this->add_log_error( 'Affiliate account could not be created!' );
return false;
}
slicewp_add_affiliate_meta( $affiliate_id, 'promotional_methods', $promotional_methods );
if ( true === $welcome_email ) {
do_action( 'slicewp_register_affiliate', $affiliate_id );
}
return true;
}