Skip to content

Instantly share code, notes, and snippets.

@cartpauj
Last active August 21, 2019 15:34
Show Gist options
  • Save cartpauj/58f30a48624e12c40408fe8c51049d93 to your computer and use it in GitHub Desktop.
Save cartpauj/58f30a48624e12c40408fe8c51049d93 to your computer and use it in GitHub Desktop.
MemberPress Account Info Shortcode Extension - Specify User ID
<?php
// This code adds a new shortcode similar to MemberPress' [mepr-account-info]
// However, it also adds an "id" parameter which will get info for a specific user
// by their ID.
//
// Sample Usage: [mepr-account-info-extended field="slug" id="123"]
// field = The slug of your usermeta or custom field
// id = The WP User ID of the user you wish to pull information for
// List of available slugs here: https://docs.memberpress.com/article/112-available-shortcodes
//
// This code can be pasted into a plugin like Code Snippets: https://wordpress.org/plugins/code-snippets/
function mepr_extend_account_info_shortcode($atts = array(), $content = '') {
if(!class_exists('MeprUser')) { return ''; } // MP plugin not found
global $user_ID;
if((!isset($atts['id']) || empty($atts['id'])) && (!isset($user_ID) || empty($user_ID))) { return ''; } // No user ID
$user_id = (isset($atts['id']) && !empty($atts['id'])) ? (int)$atts['id'] : (int)$user_ID;
$ums = MeprUtils::get_formatted_usermeta($user_id);
$usermeta = array();
if(!empty($ums)) {
foreach($ums as $umkey => $umval) {
$usermeta["{$umkey}"] = $umval;
}
}
//Get some additional params yo
$userdata = get_userdata($user_id);
foreach($userdata->data as $key => $value) {
$usermeta[$key] = $value;
}
//We can begin to define more custom return cases in here...
switch($atts['field']) {
case 'full_name':
return ucfirst($usermeta['first_name']) . ' ' . ucfirst($usermeta['last_name']);
break;
case 'full_name_last_first':
return ucfirst($usermeta['last_name']) . ', ' . ucfirst($usermeta['first_name']);
break;
case 'first_name_last_initial':
return ucfirst($usermeta['first_name']) . ' ' . ucfirst($usermeta['last_name'][0]) . '.';
break;
case 'last_name_first_initial':
return ucfirst($usermeta['last_name']) . ', ' . ucfirst($usermeta['first_name'][0]) . '.';
break;
case 'user_registered':
return MeprAppHelper::format_date($usermeta[$atts['field']]);
break;
case 'mepr_user_message':
return wpautop(stripslashes(do_shortcode($usermeta[$atts['field']])));
break;
default:
return $usermeta[$atts['field']];
break;
}
}
add_shortcode('mepr-account-info-extended', 'mepr_extend_account_info_shortcode');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment