Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dparker1005/b810ed3989242100de46efa661210c26 to your computer and use it in GitHub Desktop.
Save dparker1005/b810ed3989242100de46efa661210c26 to your computer and use it in GitHub Desktop.
When a user logs in, check if they have a sponoring level but don't have a sponsor code. If this is the case, create one.
<?php
// Copy from below here...
/**
* When a user logs in, check if they have a sponoring level but don't have
* a sponsor code. If this is the case, create one.
*
* IMPORTANT: If your sponsored members code is set up setting `seats` instead of
* `max_seats`, this code will need to be udpated accordingly.
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_pmprosm_create_sponsor_code_if_needed( $user_login, $user ) {
if ( ! function_exists( 'pmpro_getMembershipLevelForUser' ) || ! function_exists( 'pmprosm_isMainLevel' ) ) {
return;
}
// Get the user's membership level.
$user_level = pmpro_getMembershipLevelForUser( $user->ID );
if ( empty( $user_level ) ) {
return;
}
// Make sure that the level is a sponsoring level.
if ( ! pmprosm_isMainLevel( $user_level->id ) ) {
return;
}
// Check if the user already has a sponsor code.
$code_id = pmprosm_getCodeByUserID( $user->ID );
if ( ! empty( $code_id ) ) {
return;
}
// Create a sponsor code for the user.
$pmprosm_values = pmprosm_getValuesByMainLevel( $user_level->id );
// TODO: Change 'max_seats' to 'seats' if needed. Depends on your Sponsored Members setup.
pmprosm_createSponsorCode( $user->ID, $user_level->id, $pmprosm_values['max_seats'] );
update_user_meta( $user->ID, 'pmprosm_seats', $pmprosm_values['max_seats'] );
}
add_action( 'wp_login', 'my_pmprosm_create_sponsor_code_if_needed', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment