Created
May 29, 2025 17:49
-
-
Save cdsaenz/4ad31696e5167f53af0aa85601cadd04 to your computer and use it in GitHub Desktop.
Paid Memberships Pro (PMPro) - Get a single field value from the current user (ex: in a profile page)
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 | |
/** | |
* Shortcode to display a profile field. | |
*/ | |
namespace CSDev\PMProCustom; | |
new ProfileField(); | |
class ProfileField | |
{ | |
public function __construct() | |
{ | |
add_shortcode('pmpro_profile_field', array($this, 'shortcode')); | |
} | |
function shortcode($atts) | |
{ | |
$user = pmpromd_get_user(); | |
if (empty($user) || empty($atts['field'])) { | |
return ''; | |
} | |
// Handle special cases (e.g., membership level) | |
if ($atts['field'] === 'membership_level') { | |
$level = pmpro_getMembershipLevelForUser($user->ID); | |
return $level ? $level->name : ''; | |
} | |
return $user->{$atts['field']} ?? get_user_meta($user->ID, $atts['field'], true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment