Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save travislima/6f1d45d3ff236cda4e0d8d871adc8d9b to your computer and use it in GitHub Desktop.
Save travislima/6f1d45d3ff236cda4e0d8d871adc8d9b to your computer and use it in GitHub Desktop.
Generate fields for free membership levels, or specific levels.
<?php
/**
* This will generate information for username, password and password 2. This is great for free levels.
* Adjust the $generate_data_level ID's to allow data generation for specific levels.
* Use CSS to hide these fields on the checkout page from the frontend.
* Add this code to your PMPro Customizations Plugin - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
// Function to make fields optional for free levels.
function my_generate_fields_for_users() {
if ( !isset( $_REQUEST['level'] ) ) {
return;
}
$generate_data_levels = array( '1', '2', '5' ); // Level ID's to generate data for.
if ( ! in_array( $_REQUEST['level'], $generate_data_levels ) ) {
return;
}
// Generate Email Address as username.
if(!empty($_REQUEST['bemail'])) {
$_REQUEST['username'] = $_REQUEST['bemail'];
}
if(!empty($_POST['bemail'])) {
$_POST['username'] = $_POST['bemail'];
}
if(!empty($_GET['bemail'])) {
$_GET['username'] = $_GET['bemail'];
}
// Generate passwords.
if( ! empty( $_REQUEST['username'] ) && empty( $_REQUEST['password'] ) ) {
$_REQUEST['password'] = wp_generate_password( 12, true, false ); // Generate Password.
$_REQUEST['password2'] = $_REQUEST['password'];
}
}
add_action( 'init', 'my_generate_fields_for_users' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment