Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

For a collaborative website for a company, user ID of a given user is stored in another user's meta data. For e.g.: If user A is collaborating with user B, the user ID of user A is appended to an array that contains several other user ID's and then update_user_meta is run to store the array in user B's meta data. However, when user A leaves the company his account is deleted. The action deletes the user ID from the WordPress system however the ID remains in the array stored in user B's meta data. When looking at larger scale user A's ID may appear in several other user's meta data. Since, this is a continual process there are many users having ID's of non-existent users in their meta data. This is increasing size of the arrays stored in the meta data for no reason. And the non-existent ID's keep throwing errors wherever the data is used within the site. Leading to a whole lot of mess. Though conditionals are used within the code to check for user ID existence in WordPress, there is no reason to continue storing these irrelevant ID's.

So the question is: How, can I remove non-existent user ID's from meta data of active users?

Regards, John

share|improve this question

1 Answer 1

up vote 1 down vote accepted

The easiest way to overcome this would be to create a custom "getter" to pull the data from the user meta which will check and clean and update "on the fly" something like this:

First create a function to check if the user exists,

function does_user_exists($user_id){
    global $wpdb;
    $user = $wpdb->get_var( $wpdb->prepare( 
        "
            SELECT user_login
            FROM $wpdb->users 
            WHERE ID = %s
        ", 
        $user_id
    ) );
    if ($user == null)
        return false;
    else
        return true;
}

then create a function that gets the meta and validates it,

function get_user_collab($user_id){
    $update_flag = false;
    $collabs = get_user_meta($user_id,'collab_meta_key',true);
    //loop over ids and check if the user exists, if not then we remove it
    foreach ((array)$collabs as $key => $uid) {
        if (!does_user_exists($uid)){
            unset($collabs[$key]);
            $update_flag = true;
        }
    }
    if ($update_flag)
        update_user_meta($user_id,'collab_meta_key',$collabs);

    return $collabs;
}

and now instead of calling get_user_meta just use get_user_collab

share|improve this answer
    
Thx for the reply. That's a neat solution. However, I would be worried about the performance being taxed due to the foreach statement. The arrays could get really large (20K to 100K entries for each user). Running this for each user with many (read huge traffic) users logged in simultaneously, could possibly bring things to a halt. Or maybe I am just over exaggerating? –  John Apr 2 '12 at 21:47
    
WOW why would a user have 100k user ids as collaborators? anyway its a solution to clean up "On the fly" but you can always run this or any other kind of clean up when traffic is low and not at it's peaks (dead hours) and you can even split this and create a corn or scheduled event to run your clean up scripts. –  Bainternet Apr 2 '12 at 22:14
    
The site is for an enterprise company and user base includes employees and vendors alike. Globally, some user accounts are present in all other user's meta for these accounts to push posts to every other account. The word collaborators would simply not suit such an usage. But that's how the code names it currently. I am think running this as a cron job during non-business hours and on weekends would make sense. Anyways, thx — It has been a great help. –  John Apr 2 '12 at 23:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.