I’m working on a plugin which adds fields to the contact methods on the user profile page. It checks to see if the user is added to the plugin database and returns 1 if he is. This works and the plugin only shows the contact fields to the users who are added by it won’t let them save changes in the contact field.

add_filter('user_contactmethods', 'contactmethods');

function contactmethods($user_contactmethods){
global $wpdb;
global $profileuser;
$id = $profileuser->ID;
$table  = $wpdb->prefix . "table";
$myrows = NULL;
$myrows = $wpdb->query($wpdb->prepare('SELECT 1 FROM ' . $table . ' WHERE id = %d LIMIT 1', $id));
    if($ba_myrows == 1){
        $user_contactmethods['option'] = 'link';
    }
    return $user_contactmethods;
}

2 Answers
2

The problem is that $profileuser is only initialised by user-edit.php but the update is posted to /wp-admin/profile.php and does not get as far as user-edit before running your user_contactmethods handler in the context of the save – i.e. $profileuser is not available when actually running the update.

Fortunately there is another global $user_id that is initialised both on the contact page and in the contact update handler. Instead, try:

function contactmethods($user_contactmethods) {
    global $wpdb;
    global $user_id;

    $table  = $wpdb->prefix . "table";
    $myrows = NULL;
    $myrows = $wpdb->query($wpdb->prepare('SELECT 1 FROM ' . $table . ' WHERE id = %d LIMIT 1',
                           $user_id));

    if($ba_myrows == 1) {
        $user_contactmethods['option'] = 'link';
    }
    return $user_contactmethods;
}

i.e. using $user_id in place of your $id calculated from $profileuser.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *