User Permission - Run User Code (Wordpress)

I am developing a custom plugin in PHP, so existing plugins are not usable. I want to make sure that I want to display different URLs in the message for some users. For users registered in Wordpress, contact me and are "approved." I want to configure this additional user profile field so that I can use this field in state. Thus, guests and users without this field or without the correct value in this field will receive url1, and other url2.

requirements

  • only users with administrator role
  • can create / edit additional user profile fields.
  • for all other users

I know how I can add an additional user profile field in Wordpress (see links below), but I don’t know how to restrict editing of this field to users in this role and how a user with an administrator role can create / edit this filed for all users . It seems to me that I should add a new code under the Wordpress panel / users / new user and / or panel / users / authors and users.

I did some google searches and found several sites on how to add an extra user profile field.

, - , , , " " .

+3
1

:

$ID="2";
$user = new WP_User($ID);
if ($user->wp_capabilities['administrator']==1) {
    //code here
}

. wordpress

, , , , :

function my_show_extra_profile_fields( $user ) {
    if ($user->wp_capabilities['administrator']!=1) {
        return false;
    } ?>
    <h3>Extra profile information</h3>

    <table class="form-table">

        <tr>
            <th><label for="twitter">Twitter</label></th>

            <td>
                <input type="text" name="twitter" id="twitter" value="<?php echo esc_attr( get_the_author_meta( 'twitter', $user->ID ) ); ?>" class="regular-text" /><br />
                <span class="description">Please enter your Twitter username.</span>
            </td>
        </tr>

    </table>
<?php }
+3

Source: https://habr.com/ru/post/1728594/


All Articles