Support » Developing with WordPress » display custom field according to user role

  • Resolved janbrokes

    (@janbrokes)



    Hello

    i need to show product custom field only for user role f.e. Administrator
    i tried to add this condition but it not works. Page does not load
    ` <?php if( current_user_can(‘administrator’) { ?>
    <p>text</p>
    <?php } ?>`

    could you please help me with correct contition for this example?

    Thank you very much

Viewing 3 replies - 1 through 3 (of 3 total)
  • Aboelabbas

    (@dawn-birth)

    You forgot to close parentheses on the first line

    <?php if( current_user_can( 'administrator' ) ){ ?>
        <p>text</p>
    <?php } ?>

    Also, it is better to check for capabilities not roles:
    https://developer.wordpress.org/reference/classes/wp_user/has_cap/

    makewebbetter

    (@makewebbetter)

    Hello,

    You can use this,

    <?php
    $current_user = wp_get_current_user();
    $role = ( array ) $user->roles;
    if (in_array("administrator", $role))
    {
      // administrator text
    } else {
       // not administrator text
    }
    ?>

    you can vary if else condition according to your requirements

    janbrokes

    (@janbrokes)

    Thanks to all peple who tried to help me 🙂
    this works

    <?php if( current_user_can( 'administrator' ) ){ ?>
        <p>text</p>
    <?php } ?>

    Now i wil echo custom field and it will be correct

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘display custom field according to user role’ is closed to new replies.