Created
October 9, 2020 15:50
-
-
Save ronalfy/74f12f0470a906c2e2dcb7f21742547e to your computer and use it in GitHub Desktop.
PMPro - QR Code as Avatar
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 | |
/** | |
* This recipe replaces a user's avatar with a QR code. | |
* | |
* 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/ | |
*/ | |
/** | |
* Perform membership actions outputting shortcode content and QR code data. | |
*/ | |
class PMPro_Membership_Card_Customizations { | |
public function __construct() { | |
add_filter( 'get_avatar', array( $this, 'avatar_override' ), 10, 6 ); | |
add_filter( 'pmpro_membership_card_qr_code_size', array( $this, 'avatar_size' ) ); | |
} | |
/** | |
* Return a large QR Code | |
*/ | |
public function avatar_size( $size ) { | |
return '256x256'; | |
} | |
/** | |
* Return a QR Code instead of user avatar. | |
*/ | |
public function avatar_override( $avatar, $id_or_email, $size, $default, $alt, $args = array() ) { | |
// Return avatar if in admin panel or user is an admin or above. | |
if ( ! function_exists( 'pmpro_membership_card_return_qr_code_data' ) || ! function_exists( 'pmpro_hasMembershipLevel' ) || current_user_can( 'manage_options' ) || ! is_user_logged_in() ) { | |
return $avatar; | |
} | |
// Make sure user has a membership level. | |
if ( ! pmpro_hasMembershipLevel() ) { | |
return $avatar; | |
} | |
global $current_user; | |
return sprintf( '<img alt="QR CODE" src="%s" />', esc_url_raw( pmpro_membership_card_return_qr_code_data( $current_user, 'email' ) ) ); | |
} | |
} | |
new PMPro_Membership_Card_Customizations(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment