Skip to content

Instantly share code, notes, and snippets.

@cdsaenz
Created May 29, 2025 17:49
Show Gist options
  • Save cdsaenz/4ad31696e5167f53af0aa85601cadd04 to your computer and use it in GitHub Desktop.
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)
<?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