You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助: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-memberships user meta key, so a direct meta_value match 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

  1. Add this code to your theme's functions.php file or a custom plugin.
  2. Insert the shortcode into any post, page, or widget:
    [memberpress_members_list membership_id="123" custom_field_key="your_custom_field"]
    Replace 123 with your actual membership ID and your_custom_field with the key of your user custom field.

Key Improvements

  • Uses meta_query with IN comparison 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 04:58:18