• 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 » Import user memberships

Import user memberships

Chad Butler · Jul 22, 2023 ·

/**
 * This is a code snippet to import user membership data for both expirable and non-expiring
 * memberships for use with the "Import and export users and customers" plugin.
 * @link https://wordpress.org/plugins/import-users-from-csv-with-meta/
 *
 * In your CSV import file, add a column for each membership as follows:
 * "_wpmem_products_" + "membership-slug". 
 * (Get the slug from Memberships > All memberships)
 *
 * Example: if the membership slug is "my-simple-membership", then the column heading is:
 * "_wpmem_products_my-simple-membership".
 *
 * If the membership is a non-expiration membership, values are 1 for enabled, 0 for disabled
 * If the membership is an expiration membership, the expiration date MUST be YYYY-MM-DD. Any
 * other date format will not be read correctly and will cause an error.
 *
 * The script will adjust dates to the proper timestamp format and update the user's
 * membership array.
 */
 add_action( 'post_acui_import_single_user', function( $headers, $data, $user_id ) {

    $memberships = wpmem_get_memberships();

    foreach ( $headers as $i => $header ) {
        $membership_meta = substr( $header, 16 );
        if ( array_key_exists( $membership_meta, $memberships ) ) {
            if ( 0 != $data[ $i ] ) {
                $expiration_period = ( isset( $memberships[ $membership_meta ]['expires'] ) ) ? $memberships[ $membership_meta ]['expires'] : false;
                $date = ( $expiration_period ) ? $data[ $i ] : false;
                wpmem_set_user_product( $membership_meta, $user_id, $date );
            } else {
                wpmem_remove_user_product( $membership_meta, $user_id );
            }
        }
    }
    
}, 10, 3);

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