• 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 » Update user product access from import data

Update user product access from import data

Chad Butler · Apr 18, 2019 ·

/**
 * Set process to update user accordingly.
 *
 * This function will run for each user being updated in the main
 * framework script below. Breaking out the actual update process
 * makes the framework more modular so it can be adapted to multiple
 * use cases.
 *
 * For this use case, identify the actual meta keys used for the fields
 * imported with the membership slug and expiration date. The default
 * set up below is "membership" and "expires". The script will get
 * the meta values for those fields and run wpmem_set_user_product().
 * It will clean up for itself by deleting the meta values after it
 * is done.
 *
 * Because of the clean up process (deleting the meta keys it has
 * processed), if the script crashes from too many users, it can
 * be run again and will not overwrite existing processed users.
 */
function my_update_selected_user( $user_id ) {
    
    // Set specific criteria.
    $membership_key = "membership";
    $expiration_key = "expires";
    
    // Get the user's membership product info.
    $membership = get_user_meta( $user_id, $membership_key, true );
    $expiration = get_user_meta( $user_id, $expiration_key, true );
    
    // Only process users who have not been processed already.
    if ( $membership ) {

        // Set expiration date - either "false" or MySQL timestamp.
        if ( $expiration ) {
            $date = ( 'none' == $expiration ) ? false : date( "Y-m-d H:i:s", strtotime( $expiration ) );
        } else {
            $date = false;
        }

        // Set user product access.
        wpmem_set_user_product( $membership, $user_id, $date );

        // Clean up after yourself.
        delete_user_meta( $user_id, $membership_key );
        delete_user_meta( $user_id, $expiration_key );
        
    }
}

/**
 * A drop-in code snippet to update all users' membership
 * access and any applicable expiration date.
 *
 * To Use:
 * 1. Save the code snippet to your theme's functions.php
 * 2. Go to Tools > Update All Users.
 * 3. Follow prompts on screen.
 * 4. Remove the code snippet when completed.
 */
add_action( 'init', 'update_all_users_init' );
function update_all_users_init() {
    global $wpmem;
    $wpmem->update_all_users = New My_Update_All_Users_Class();
}
class My_Update_All_Users_Class {
 
    function __construct() {
        add_action( 'admin_menu', array( $this, 'admin_menu' ) );
    }

    function admin_menu() {
        $hook = add_management_page( 'Update All Users Page', 'Update All Users', 'edit_users', 'update-all-users', array( $this, 'admin_page' ) );
        add_action( "load-$hook", array( $this, 'admin_page_load' ) );
    }

    function admin_page_load() {
        global $update_all_complete;
        $update_all_complete = false;
        if ( isset( $_GET['page'] ) && 'update-all-users' == $_GET['page'] && isset( $_POST['update-all-confirm'] ) && 1 == $_POST['update-all-confirm'] ) {
            $users = get_users( array( 'fields'=>'ID' ) );
            // This is where we loop through users and update them.
            foreach ( $users as $user_id ) {
                
                // This is the custom process.
                my_update_selected_user( $user_id );
                
            }
            $update_all_complete = true;
        }
    }

    function admin_page() {
        global $update_all_complete;
        echo "<h2>Update All Users</h2>";
        if ( $update_all_complete ) {
            echo '<p>All users were update.<br />';
            echo 'You may now remove this code snippet if desired.</p>';
        } else {
            $form_post = ( function_exists( 'wpmem_admin_form_post_url' ) ) ? wpmem_admin_form_post_url() : '';
            echo "<p>This process will update membership access for all users.<br />It will not change any passwords or send any emails to users.";
            echo '<form name="update-all-users" id="update-all-users" method="post" action="' . $form_post . '">';
            echo '<p><input type="checkbox" name="update-all-confirm" value="1" /><label for="update-all-confirm">Update all users?</label></p>';
            echo '<p><input type="submit" name="submit" value="Submit" /></p>';
            echo '</form>';
        }
    }
}
// End of My_Update_All_Users_Class

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