/** * A drop-in code snippet to set all users on the site with * a specified membership product and assign expiration based * on imported meta field. * * Prerequisites: * * (Optional) IF an existing expiration date is to be set, assign * that to the existing users via an import, using a custom meta * field for the current expiration. Set the $imported_meta to the * meta key used for this field. * @see: https://test.rocketgeek.com/blog/importing-users-using-a-plugin/ * * To Use: * 1. Save the code snippet to your theme's functions.php * 2. Go to Tools > Assign User Membership. * 3. Follow prompts on screen. * 4. Remove the code snippet when completed. */ add_action( 'init', 'assign_membership_to_all_users_init' ); function assign_membership_to_all_users_init() { global $wpmem; $wpmem->assign_membership = new My_Assign_Membership_Class(); } class My_Assign_Membership_Class { public function __construct() { add_action( 'admin_menu', array( $this, 'admin_menu' ) ); } public function admin_menu() { $hook = add_management_page( 'Assign User Membership', 'Assign User Membership', 'edit_users', 'assign-all-users-membership', array( $this, 'admin_page' ) ); add_action( "load-$hook", array( $this, 'admin_page_load' ) ); } public function admin_page_load() { global $activate_all_complete; $activate_all_complete = false; if ( isset( $_GET['page'] ) && 'assign-all-users-membership' == $_GET['page'] && isset( $_POST['update-all-confirm'] ) && 1 == $_POST['update-all-confirm'] ) { $users = get_users( array( 'fields'=>'ID' ) ); $membership = sanitize_text_field( $_POST['membership_to_assign'] ); $expiration = ( isset( $_POST['import_meta_key'] ) ) ? sanitize_text_field( $_POST['import_meta_key'] ) : false; foreach ( $users as $user_id ) { $initial_expiration = ( $expiration ) ? get_user_meta( $user_id, $expiration, true ) : false; wpmem_set_user_product( $membership, $user_id, $initial_expiration ); } $activate_all_complete = true; } } public function admin_page() { global $wpdb, $wpmem, $activate_all_complete; echo "<h2>Assign User Membership</h2>"; if ( $activate_all_complete ) { echo '<p>All users were updated.<br />'; echo 'You may now remove this code snippet if desired.</p>'; } else { // Handle form post. $form_post = ( function_exists( 'wpmem_admin_form_post_url' ) ) ? wpmem_admin_form_post_url() : ''; // Create a form field with all products in a dropdown. $product_selector = wpmem_form_field( array( 'name' => 'membership_to_assign', 'type' => 'membership', 'required' => 1, ) ); // Create a form field with all possible user meta keys in a dropdown. $values = $wpdb->get_results( "SELECT DISTINCT meta_key FROM " . $wpdb->usermeta ); $exp_meta_values[] = 'Select expiration field meta key (if any)|'; foreach ( $values as $value ) { $exp_meta_values[] = $value->meta_key . "|" . $value->meta_key; } $expiration_meta = wpmem_form_field( array( 'name' => 'import_meta_key', 'type' => 'select', 'value' => $exp_meta_values, ) ); echo '<form name="assign-all-users-membership" id="assign-all-users-membership" method="post" action="' . $form_post . '">'; echo "<p>Membership to assign for all users: " . $product_selector . "</p>"; echo '<p>For each user, if an expiration date was imported, select the meta key containing the<br />'; echo 'expiration date (or leave blank to assign the default expiration) <br />'; echo $expiration_meta . "</p>"; echo '<p><input type="checkbox" name="update-all-confirm" value="1" required /><label for="update-all-confirm">Update all users? (If you do not confirm, no users will be updated.)</label></p>'; echo '<p><input type="submit" name="submit" value="Submit" /></p>'; echo '</form>'; } } } // End of My_Assign_Membership_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
- 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.