Skip to content

Instantly share code, notes, and snippets.

@travislima
Forked from ronalfy/pmpro-qr-code-avatar.php
Last active March 30, 2021 10:58
Show Gist options
  • Save travislima/5ab75a4e6f31977eeee3c957729d4ba3 to your computer and use it in GitHub Desktop.
Save travislima/5ab75a4e6f31977eeee3c957729d4ba3 to your computer and use it in GitHub Desktop.
PMPro - QR Code as Avatar
<?php
/**
* This recipe replaces a user's avatar with a QR code.
* You can add this recipe to your site by using the Code Snippets plugin available for free in the WordPress repository.
* Follow this guide to add this code to your site - 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