Skip to content

Instantly share code, notes, and snippets.

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 champsupertramp/4002dbc2386b80c7dccbfe6865a0fb17 to your computer and use it in GitHub Desktop.
Save champsupertramp/4002dbc2386b80c7dccbfe6865a0fb17 to your computer and use it in GitHub Desktop.
Ultimate Member - Calculate two profile fields
<?php
// Profile View
add_filter("um_profile_field_filter_hook__myMetaKeyC","um_profile_field_filter_hook__myMetaKeyC");
function um_profile_field_filter_hook__myMetaKeyC( $value, $data ){
$a = um_user("myMetaKeyA");
$b = um_user("myMetaKeyB");
$value = intval( $a ) + intval( $b );
return $value;
}
// Editing View
add_filter("um_edit_myMetaKeyC_field_value","um_edit_myMetaKeyC_field_value");
function um_edit_myMetaKeyC_field_value( $value, $key ){
$a = um_user("myMetaKeyA");
$b = um_user("myMetaKeyB");
$value = intval( $a ) + intval( $b );
return $value;
}
?>
// Here's the jQuery version of calculations in editing view:
<script type="text/javascript">
$(document).ready(function(){
var $um_field = $("input[data-key='myMetaKeyA'],input[data-key='myMetaKeyB']");
$um_field.keyup(function(){
um_calculate_fields();
});
function um_calculate_fields(){
var a = $("input[data-key='myMetaKeyA']").val();
var b = $("input[data-key='myMetaKeyB']").val()
var calc = parseInt( a ) + parseInt( b );
var total = isNaN( calc ) ? 0: calc;
$("input[data-key='myMetaKeyC']").val( total );
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment