Skip to content

Instantly share code, notes, and snippets.

@ideadude
Created December 17, 2019 22:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ideadude/65d94271d6f3b4f359a417bcb849c388 to your computer and use it in GitHub Desktop.
Save ideadude/65d94271d6f3b4f359a417bcb849c388 to your computer and use it in GitHub Desktop.
Give PMPro members on one subsite membership on another subsite.
/**
* When a user is given a level, give add that user to another site
* in the network and give them a level there too.
* PMPro should be active on both blogs.
* The other blog should have the membership level already setup, note the ID.
* Blog/site IDs can be found in the Sites page of the network dashboard.
* This is not mean to work with the other PMPro multisite add ons.
* Add this to a custom plugin or as a Code Snippet on your site.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function give_users_free_level_on_main_site( $level_id, $user_id ) {
// this is the site id and level id
// of the other site to give the user a level on.
$main_site_id = 1;
$main_site_level = 1;
// if level > 0 give user free level
if ( 0 < $level_id ) {
switch_to_blog( $main_site_id );
add_user_to_blog($main_site_id, $user_id, 'subscriber');
remove_action( 'pmpro_after_change_membership_level', 'give_users_free_level_on_main_site', 10, 2 ); // prevent loops
pmpro_setDBTables(); // make sure PMPro DB table names are set in $wpdb
pmpro_changeMembershipLevel( $main_site_level, $user_id );
add_action( 'pmpro_after_change_membership_level', 'give_users_free_level_on_main_site', 10, 2 );
restore_current_blog();
}
}
add_action( 'pmpro_after_change_membership_level', 'give_users_free_level_on_main_site', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment