How to make a READ-ONLY field in User Profile? (here's my original code) (6 posts)

  1. produitsmama
    Posted 4 years ago #

    Hi I've created a custom field in my functions.php to the user profile.

    This field serve as a "registration" number and must be view only by the user in his profile and read-only except for an admin who is able to edit it.

    Here is a paste of the code i'm using. Can someone paste me a corrected version of what I need and explain how it work please?

    [Code moderated as per the Forum Rules. The maximum number of lines of code that you can post in these forums is ten lines. Please use the pastebin]

  2. luckdragon
    Posted 4 years ago #

    don't they already have a "registration number" that's built in and read only? i.e. $current_user->ID

  3. produitsmama
    Posted 4 years ago #

    I explain: my site is private and registration is off. My users are salesrep and in this read-only field i want i will place a unique salesrep code. the connected user can see his code but can't edit it. He must be the only one (except admin) to have acces to his code.

  4. produitsmama
    Posted 4 years ago #

    Here is the Code i'm using.

    I would like someone to give me a corrected version to make it Read-Only to users but Read/write to Admins http://pastebin.com/x27yiBm9

    I'm still learning my PHP so Syntax and where to place what is a little confusing for me. So if someone would be kind enought to tell me why new code will be placed at that specific place i would be wore that happy


  5. JohnnyN05
    Posted 4 years ago #

    You can try to disable the input field if the current user is NOT an admin (code not tested)...

    $disable_input = 'disabled="disabled"';
    if ( current_user_can( 'create_users' ) ) {  //<-- Only admins can create_users
    	$disable_input = '';
    <input type="text" name="jobpos" id="jobpos" value="<?php echo esc_attr( get_the_author_meta( 'jobpos', $user->ID ) ); ?>"
    class="regular-text" <?php echo $disable_input ?> />
  6. produitsmama
    Posted 4 years ago #

    @JohnnyN05 I'll check that bit of code of you tomorow when i'll be back to my programming.

    Thanx hope it'll work. If so I'll bo so darn happy loll

Topic Closed

This topic has been closed to new replies.

About this Topic