Skip to content

Instantly share code, notes, and snippets.

@contemplate
Last active May 1, 2016 06:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save contemplate/fed3a536226b63cd01738dbbcaef89ec to your computer and use it in GitHub Desktop.
Save contemplate/fed3a536226b63cd01738dbbcaef89ec to your computer and use it in GitHub Desktop.
PMPro - Custom JS to autofill the expiration date when a PMPro level is chosen on the edit user page by admin
/*---------------------------------
Autofill Expiration on Admin Level Change
-----------------------------------*/
function profile_autofill_expiration()
{
global $wpdb;
$sqlQuery = "SELECT * FROM $wpdb->pmpro_membership_levels ";
$sqlQuery .= "ORDER BY id ASC";
$levels = $wpdb->get_results($sqlQuery, OBJECT);
?>
<script>
jQuery(document).ready(function() {
//remember current expiration values
var expires = jQuery("#expires").val();
var expires_month = jQuery("[name=expires_month]").val();
var expires_day = jQuery("[name=expires_day]").val();
var expires_year = jQuery("[name=expires_year]").val();
var changed = false;
//when changing level set or reset expiration values
jQuery("[name=membership_level]").change(function() {
<?php
foreach($levels as $level){
if($level->expiration_number){
$level_expiration_from_today = strtotime("+".$level->expiration_number." ".$level->expiration_period, current_time('timestamp'));
?>
if(jQuery(this).val() == "<?php echo $level->id; ?>")
{
jQuery("#expires").val(1);
jQuery("[name=expires_month]").val(<?php echo date('n', $level_expiration_from_today);?>);
jQuery("[name=expires_day]").val(<?php echo date('j', $level_expiration_from_today);?>);
jQuery("[name=expires_year]").val(<?php echo date('Y', $level_expiration_from_today);?>);
jQuery("#expires_date").show();
changed = true;
}
<?php
}
}
?>
if(changed == false)
{
jQuery('#expires').val(expires);
jQuery('[name=expires_month]').val(expires_month);
jQuery('[name=expires_day]').val(expires_day);
jQuery('[name=expires_year]').val(expires_year);
if(expires == 1)
jQuery('#expires_date').show();
else
jQuery('#expires_date').hide();
changed = false;
}
});
});
</script>
<?php
}
add_action( 'show_user_profile', 'profile_autofill_expiration' );
add_action( 'edit_user_profile', 'profile_autofill_expiration' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment