• Resolved tcmacadmind

    (@tcmacadmind)


    Hi, I have a Club Membership website and use UM to register all of our members. I have created a Profile form that works well and I have recently added a field for Date First Joined, but would like to also create a field to automatically show the number of years and months that a member has been a member calculated from the Date First Joined field. Can this be done and if so how?

    I am not very experienced in WordPress or the programming of this sort of thing so a plain English explanation or link to a guide/plugin would be great.

    Thanks for any assistance that may be forthcoming.

    cheers Dave

Viewing 15 replies - 1 through 15 (of 27 total)
  • Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @tcmacadmind

    Unfortunately, there’s no calculated field in UM. this requires customization on your end. Let’s see if others in the forum have done something similar and share their solution here.

    Regards,

    Thread Starter tcmacadmind

    (@tcmacadmind)

    Thanks Champ Camba, yes lets hope someone has a solution.

    cheers

    @tcmacadmind

    You can test this shortcode based on the WP function “human_time_diff”
    https://developer.wordpress.org/reference/functions/human_time_diff/

    Usage: [my_human_time_diff meta_key="meta_key_time_name"]
    Add the shortcode by using the UM form shortcode field.
    Replace the “meta_key_time_name” with the name of your “field for Date First Joined” which must be a Unix timestamp (= large integer)

    Add this code snippet to your child-theme functions.php file
    or add to the “Code Snippets” plugin

    <?php
    
    /*
    *       [my_human_time_diff meta_key="meta_key_time_name"]
    */
    
    add_shortcode( 'my_human_time_diff', 'my_human_time_diff' );
    
        function my_human_time_diff( $atts ) {
    
            if( is_array( $atts ) && isset( $atts['meta_key'] )) {
                um_fetch_user( um_profile_id());
                echo human_time_diff( um_user( $atts['meta_key'] ), current_time('timestamp')) . ' ago';
            }
        }
    • This reply was modified 4 years, 8 months ago by missveronica.
    Thread Starter tcmacadmind

    (@tcmacadmind)

    Hi miss veronica, thanks you so much for your assistance, this looks promising however, when I posted the code snippet into my Code Snippets plugin I got the following error and am not sure what went wrong;

    Don't Panic
    
    The code snippet you are trying to save produced a fatal error on line 1:
    
    syntax error, unexpected '&', expecting end of file
    The previous version of the snippet is unchanged, and the rest of this site should be functioning normally as before.
    
    Please use the back button in your browser to return to the previous page and try to fix the code error. If you prefer, you can close this page and discard the changes you just made. No changes will be made to this site.

    So I backed out and am wondering if you are able to provide a solution. Also in your advice you said to;

    Replace the “meta_key_time_name” with the name of your “field for Date First Joined” which must be a Unix timestamp (= large integer)

    but I am unsure how to determine if it is a Unix timestamp (= large integer).

    Any assistance would be greatly appreciated.

    @tcmacadmind

    You can remove the first line of the code snippet “<?php” for use in Code Snippets.

    @tcmacadmind

    I have improved the code snippet to include both Unix timestamps and normal readable dates plus time as begin times.

    Error messages are displayed for error in the shortcode parameter and if the difference begin time value is invalid.

    /*
    *       [my_human_time_diff meta_key="meta_key_time_name"]
    */
    
    add_shortcode( 'my_human_time_diff', 'my_human_time_diff' );
    
        function my_human_time_diff( $atts ) {
    
            if( is_array( $atts ) && isset( $atts['meta_key'] )) { 
                um_fetch_user( um_profile_id());
    
                if( is_numeric( um_user( $atts['meta_key'] ) )) {                
                    $first_time = um_user( $atts['meta_key'] );
                } else {
                    if( is_string( um_user( $atts['meta_key'] ) )) $first_time = strtotime( um_user( $atts['meta_key'] ));
                    else $first_time = false;
                }
                if( $first_time ) echo '<p>' . human_time_diff( $first_time, strtotime( wp_date( 'Y-m-d H:i:s' ))) . ' ago</p>';
                else echo '<p>Difference begin time invalid</p>';
            } else echo '<p>Shortcode parameter error</p>';
        }
    Thread Starter tcmacadmind

    (@tcmacadmind)

    WOW thanks again missveronica, your assistance has been awesome. I updated the snippet and it is working as it should expect that the result is not displaying in the field on the form but above the form. Is it something that I have done wrong with how I set up the date first joined field. I did it as you suggested in your first response but could not find how to ensure that it was a Unix timestamp (= large integer). Do you think this may be the issue or maybe something else?

    Thanks again for your assistance.
    Dave

    @tcmacadmind

    The latest version of the code snippet can handle both large integers (Unix timestamp) and formatted date+time like: 2021-07-23 01:55

    I have enclosed the display text in <p>….</p>

    Here I have changed all <p>…</p> to <div>….</div> but I can’t see any changes where the text is displyed by UM.

    /*
    *       [my_human_time_diff meta_key="meta_key_time_name"]
    */
    
    add_shortcode( 'my_human_time_diff', 'my_human_time_diff' );
    
        function my_human_time_diff( $atts ) {
    
            if( is_array( $atts ) && isset( $atts['meta_key'] )) { 
                um_fetch_user( um_profile_id());
    
                if( is_numeric( um_user( $atts['meta_key'] ) )) {                
                    $first_time = um_user( $atts['meta_key'] );
                } else {
                    if( is_string( um_user( $atts['meta_key'] ) )) $first_time = strtotime( um_user( $atts['meta_key'] ));
                    else $first_time = false;
                }
                if( $first_time ) return '<div>' . human_time_diff( $first_time, strtotime( wp_date( 'Y-m-d H:i:s' ))) . ' ago</div>';
                else return '<div>Difference begin time invalid</div>';
            } else return '<div>Shortcode parameter error</div>';
        }
    • This reply was modified 4 years, 7 months ago by missveronica.
    • This reply was modified 4 years, 7 months ago by missveronica.

    @tcmacadmind

    the result is not displaying in the field on the form but above the form

    Has been fixed now.

    Many changes in the code snippet
    and the version above, which now is returning the text to UM
    instead of doing the display within the code snippet,
    will give UM the possibility to display the text
    where you have placed the shortcode field in your form.

    • This reply was modified 4 years, 7 months ago by missveronica.
    • This reply was modified 4 years, 7 months ago by missveronica.
    Thread Starter tcmacadmind

    (@tcmacadmind)

    Hi again missveronica, the latest version of the snippet works well. Thank you again for your help with this. The only issue now is it displays an error if the [my_human_time_diff meta_key=”meta_key_time_name”] field is empty. I hate to ask again but is it possible to get it to do nothing if the cell is empty but display the result if a date is in the [my_human_time_diff meta_key=”meta_key_time_name”]. field.

    thanks’
    Dave

    @tcmacadmind

    I hate to ask again but is it possible to get it to do nothing if the cell is empty

    Has been fixed now.

    /*
    *       [my_human_time_diff meta_key="meta_key_time_name"]
    */
    
    add_shortcode( 'my_human_time_diff', 'my_human_time_diff' );
    
        function my_human_time_diff( $atts ) {
    
            if( is_array( $atts ) && isset( $atts['meta_key'] )) { 
                um_fetch_user( um_profile_id());
    
                if( is_numeric( um_user( $atts['meta_key'] ) )) {                
                    $first_time = um_user( $atts['meta_key'] );
                } else {
                    if( is_string( um_user( $atts['meta_key'] ) )) $first_time = strtotime( um_user( $atts['meta_key'] ));
                    else $first_time = false;
                }
                if( $first_time ) return '<div>' . human_time_diff( $first_time, strtotime( wp_date( 'Y-m-d H:i:s' ))) . ' ago</div>';
                else return '';
            } else return '<div>Shortcode parameter error</div>';
        }
    Thread Starter tcmacadmind

    (@tcmacadmind)

    Hi again missveronica. Thank you so much for your assistance with this it works well and does what I needed.

    The only issue is that the returned amount does not show the field label name so it just sits out where the field is without a name.

    Apart from that all is great.

    Thanks again for your help, it is people like yourself that make the whole Word Press family feel welcome and included. I hope to one day be knowledgeable enough to also assist others with issues.

    Thanks again
    Dave
    Admin for The Twin Cities Model Aircraft Club
    tcmac.com.au

    @tcmacadmind

    Now I have added a parameter “label” to the shortcode:

    [my_human_time_diff meta_key=”meta_key_time_name” label=”MyLabelText”]

    /*
    *       [my_human_time_diff meta_key="meta_key_time_name" label="MyLabelText"]
    */
    
    add_shortcode( 'my_human_time_diff', 'my_human_time_diff' );
    
        function my_human_time_diff( $atts ) {
    
            if( is_array( $atts ) && isset( $atts['meta_key'] )) { 
                um_fetch_user( um_profile_id());
    
                if( is_numeric( um_user( $atts['meta_key'] ) )) {                
                    $first_time = um_user( $atts['meta_key'] );
                } else {
                    if( is_string( um_user( $atts['meta_key'] ) )) $first_time = strtotime( um_user( $atts['meta_key'] ));
                    else $first_time = false;
                }
                if( $first_time ) {
                    if( isset( $atts['label'] )) $label = $atts['label'] . ' ';
                    else $label = ''; 
                    return '<div>' . $label . human_time_diff( $first_time, strtotime( wp_date( 'Y-m-d H:i:s' ))) . ' ago</div>';
                } else return '';
            } else return '<div>Shortcode parameter error</div>';
        }
    Thread Starter tcmacadmind

    (@tcmacadmind)

    Hi missveronica, thanks for the new update. I replaced the old one with this new one but it still did not display the label name, do I need to do anything else as well?

    Dave

    @tcmacadmind

    Now I have added a parameter “label” to the shortcode:

    [my_human_time_diff meta_key=”meta_key_time_name” label=”MyLabelText”]

    An additional parameter to the shortcode requires you to enter your label value.

    This value might be the meta_key name or anything else decided by the site owner.
    Automatic addition of the meta_key name is not a very user friendly option.

Viewing 15 replies - 1 through 15 (of 27 total)

The topic ‘Calculated field for profile Form’ is closed to new replies.