• Skip to primary navigation
  • Skip to main content

RocketGeek

Home of WP-Members, The Original WordPress Membership Plugin

  • WordPress Plugins
    • WP-Members
      • FAQs
      • Quick Start
      • Documentation
      • Extensions
    • Advanced Options
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Download Protect
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Invite Codes
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • MailChimp Integration
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • PayPal Subscriptions
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Salesforce Web-to-Lead
    • Security
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Text Editor
      • Purchase the Plugin
      • Get the Pro Bundle
    • User List
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • User Tracking
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Memberships for WooCommerce
    • WordPass
  • Blog
    • Basics
    • Tips and Tricks
    • Filters
    • Actions
    • Code Snippets
    • Shortcodes
    • Design
    • Release Announcements
  • Store
    • Cart
    • Checkout
  • Contact
  • Log In
  • Show Search
Hide Search
Home » Code Snippets » Custom post restricted message for multiple memberships

Custom post restricted message for multiple memberships

Chad Butler · Jul 11, 2019 ·

/**
 * This example checks the products array in the filter
 * to see if there is more than one product. If it is
 * just one, it creates a simple message string of:
 * "Sorry, you must have a {name of product}" message by
 * getting the product title from the membership products
 * object in the main $wpmem object class.
 *
 * But if there are multiple products assigned, it loops
 * through and builds a string so that the product names
 * are separated by commas, with the last one adding
 * an ", or". This is a good example of how to work with
 * arrays in PHP.
 *
 * Follow the comments to get a handle on what the snippet
 * is doing at each logical step.
 */
add_filter( 'wpmem_product_restricted_msg', 'my_custom_product_restricted_msg', 10, 2 );
function my_custom_product_restricted_msg( $msg, $post_products ) {
    
    // You will need the global $wpmem object.
    global $wpmem;
    
    // Assemble a string with the product titles.
    // If $post_products array is more than one product:
    if ( count( $post_products ) > 1 ) {
        
        // Start with an empty container:
        $products = '';
        
        // Loop through the products to get their titles:
        foreach ( $post_products as $key => $post_product ) {
            
            // If this is the end of the product array, add ", or".
            // Note .= adds to the whatever is in the existing string $products
            end( $post_products );
            if ( $key === key( $post_products ) ) {
                $products .= "or " . $wpmem->membership->products[ $post_product ]['title'];
            } else {    
                // Otherwise, if this is not the end, add a comma/space:
                $products .= $wpmem->membership->products[ $post_product ]['title'] . ", ";
            }
        }

    } else {
        // OR... if $post_products array is just one product:
        $products = $wpmem->membership->products[ $post_products[0] ]['title'];
    }

    // Put the assembled $products string into your custom message:
    $msg = "Sorry, you must have a " . $products . " membership to access this content.";
    
    // Return the custom message.
    return $msg;
}

Not sure what to do with this code?

You're not a "coder" and don't know what to do? Don't worry! Code Snippets are the basic building blocks of WordPress customization, and once you know the basics, they are simple to use.

Here are some free articles to get you started:

  • Using Code Snippets from the Site
  • Using a code snippets plugin
  • The functions.php File
  • Create a plugin file for custom functions
  • Create a child theme
  • Do not modify plugin files!

For "hands on" help, consider a plugin support subscription or the Pro Bundle.

Ready to get started?

Join Today!

© 2025 · butlerblog.com · RocketGeek is built using WordPress, WP-Members, and the Genesis Framework

  • butlerblog.com
  • WP-Members Support Subscription
  • Terms of Service
  • Privacy Policy
  • Refund Policy