Question

I have an e-learning website that issues an official certificate of completion when a students complete their training course. The user profile name on the account can be used as the student name when generating the certificate, but if the user updates their profile name it also updates the name on the certificate. This is a problem since it would allow a user to complete a course once and then update their user profile name multiple times to generate official certificates for several other people(without completing the training or paying for it).

I created a set of custom user meta fields for the student name that are only exposed on the back end and I changed the certificate setup so that it uses these custom meta fields to add the student name to the certificate. The custom meta field remains blank if I don't manually update it on the back end, but I want the user to be able to enter this information on the front end for themselves once and then prevent any future updates from the front end (only allow me to make approved updates on the back end).

How do I expose a custom user meta input field on the front end user profile page only if it is empty and then hide it if it's not null?

Was it helpful?

Solution

You could very easily do a check to see if that field already had a value in the database and if it does, display the name, if it doesn't, display a form field. You haven't provided any code so my example below is a really loose snippet of pseudo-code showing the the method.

<?php
    $current_user = get_current_user_id();
    $student_name = get_user_meta( $current_user, 'student_name', true );
    if( !empty( $student_name ) ) :
        echo '<span class="student_name_provided">' . $student_name . '</span>';
    else :
        echo '<input type="text" name="student_name" id="student_name" value=""/>';
    endif;
?>

So if !empty() or not empty, then it displays the value of the student_name user meta in a span tag that you can style however you like.

But, if the check returns an empty value for student_name it instead loads a text input field where the student can then provide their name.

Now all of this would need to be incorporated with whatever other front end editable stuff you've got set up, but the if( !empty() ) : conditional check is the key aspect because it allows you to change what you provide the user with on the front end.

Hope that helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top