求助:WordPress中MemberPress特定会员组用户自定义字段列表实现
Fix for MemberPress Specific Membership User List Shortcode
Why the Original Code Fails
- MemberPress stores active memberships as an array in the
mepr-active-membershipsuser meta key, so a directmeta_valuematch won't return any results. - Echoing content directly in a shortcode can trigger output buffering conflicts, leading to blank pages.
- No checks for MemberPress being active, which may throw fatal errors if the plugin is disabled.
Corrected Shortcode Code
// Register shortcode to display specific MemberPress membership users add_shortcode('memberpress_members_list', 'display_memberpress_members_list'); function display_memberpress_members_list($atts) { // Check if MemberPress is active if (!class_exists('MeprMembership')) { return '<p>MemberPress is not active.</p>'; } // Extract and validate shortcode attributes $atts = shortcode_atts(array( 'membership_id' => '', 'custom_field_key' => '' ), $atts); if (empty($atts['membership_id']) || empty($atts['custom_field_key'])) { return '<p>Please provide both membership_id and custom_field_key attributes.</p>'; } // Retrieve membership object to get its name $membership = MeprMembership::get_instance($atts['membership_id']); if (!$membership) { return '<p>Invalid membership ID.</p>'; } // Query users with the target active membership $users = get_users(array( 'meta_query' => array( array( 'key' => 'mepr-active-memberships', 'value' => $atts['membership_id'], 'compare' => 'IN' ) ), 'number' => -1 // Fetch all matching users )); // Build output string $output = '<div class="memberpress-members-list">'; $output .= '<h3>Membership: ' . esc_html($membership->name) . '</h3>'; if (empty($users)) { $output .= '<p>No users found in this membership.</p>'; } else { $output .= '<ul>'; foreach ($users as $user) { // Get custom field value, fall back to display name if empty $custom_field_value = get_user_meta($user->ID, $atts['custom_field_key'], true); $display_name = !empty($custom_field_value) ? esc_html($custom_field_value) : esc_html($user->display_name); $output .= '<li>'; $output .= '<strong>' . $display_name . '</strong><br>'; $output .= 'Email: ' . esc_html($user->user_email); $output .= '</li>'; } $output .= '</ul>'; } $output .= '</div>'; return $output; }
How to Use
- Add this code to your theme's
functions.phpfile or a custom plugin. - Insert the shortcode into any post, page, or widget:
[memberpress_members_list membership_id="123" custom_field_key="your_custom_field"]
Replace123with your actual membership ID andyour_custom_fieldwith the key of your user custom field.
Key Improvements
- Uses
meta_querywithINcomparison to correctly match users in the membership array. - Returns output instead of echoing to avoid buffering issues.
- Adds validation for required parameters and MemberPress status.
- Displays the membership name as requested.
- Falls back to the user's display name if the custom field is empty.
- Escapes all output to prevent XSS vulnerabilities.
内容的提问来源于stack exchange,提问作者Michael B
相关产品推荐
相关产品推荐

