Last active
January 28, 2023 04:20
-
-
Save wpscholar/780c28ca13b1fec8986840cf39d9e531 to your computer and use it in GitHub Desktop.
A WordPress plugin that changes the user role to "University Customer" if a user registers with a .edu email address.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* University Customer Role Assignment | |
* | |
* @package UniversityCustomerRoleAssignment | |
* @author Micah Wood | |
* @copyright Copyright 2023 by Micah Wood - All rights reserved. | |
* @license GPL2.0-or-later | |
* | |
* @wordpress-plugin | |
* Plugin Name: University Customer Role Assignment | |
* Plugin URI: https://gist.github.com/wpscholar/780c28ca13b1fec8986840cf39d9e531 | |
* Description: Changes the user role to "University Customer" if they register with a .edu email address. | |
* Version: 1.0 | |
* Requires PHP: 7.4 | |
* Requires at least: 5.8 | |
* Author: Micah Wood | |
* Author URI: https://wpscholar.com | |
* License: GPL V2 or later | |
* License URI: https://www.gnu.org/licenses/gpl-2.0.html | |
*/ | |
add_action( | |
'woocommerce_created_customer', | |
function ( $customer_id ) { | |
$user = get_user_by( 'id', $customer_id ); | |
$domain = substr( strrchr( $user->user_email, '@' ), 1 ); | |
$isEdu = strrpos( $domain, '.edu' ) == strlen( $domain ) - strlen( '.edu' ); | |
if ( $isEdu ) { | |
$user->add_role( 'university-customer' ); | |
$user->remove_role( 'customer' ); | |
} | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment