Support

Account

Home Forums ACF PRO update_field and User Field Type Reply To: update_field and User Field Type

  • Here is my optimized code if needed.

    if ( (is_user_logged_in()) && ($_GET['action'] == 'follow') ) {
    	$user_followed_users = get_field('user_followed_users', 'user_'.$current_user->ID);
    	$user_followed_users_temp = array();
    	if (is_array($user_followed_users)) {
    		foreach($user_followed_users as $user) {
    			$user_followed_users_temp[] = $user['ID'];
    		}
    		if (!in_array($curauth->ID, $user_followed_users_temp)) {
    			$user_followed_users_temp[] = $curauth->ID;
    		} else {
    			// Clicking twice will remove the user form user followed list
    			if(($key = array_search($curauth->ID, $user_followed_users_temp)) !== false) {
    				unset($user_followed_users_temp[$key]);
    			}
    		}
    	} else {
    		$user_followed_users_temp = array($curauth->ID);
    	}
    	update_field('user_followed_users', $user_followed_users_temp, 'user_'.$current_user->ID);
    }