diff --git a/admin/auth_config.php b/admin/auth_config.php
index 0281b12..db500e0 100644
--- a/admin/auth_config.php
+++ b/admin/auth_config.php
@@ -51,6 +51,7 @@ if ($frm = data_submitted() and confirm_sesskey()) {
 }
 
 $user_fields = $authplugin->userfields;
+$custom_fields = $authplugin->custom_fields;
 //$user_fields = array("firstname", "lastname", "email", "phone1", "phone2", "institution", "department", "address", "city", "country", "description", "idnumber", "lang");
 
 /// Get the auth title (from core or own auth lang files)
@@ -74,7 +75,7 @@ print_simple_box_start('center', '80%', '', 5, 'informationbox');
 echo $authdescription;
 print_simple_box_end();
 echo "<hr />\n";
-$authplugin->config_form($frm, $err, $user_fields);
+$authplugin->config_form($frm, $err, $user_fields, $custom_fields);
 print_simple_box_end();
 echo '<p style="text-align: center"><input type="submit" value="' . get_string("savechanges") . "\" /></p>\n";
 echo "</div>\n";
@@ -89,7 +90,7 @@ exit;
 // but some may want a custom one if they are offering
 // other options
 // Note: lockconfig_ fields have special handling.
-function print_auth_lock_options ($auth, $user_fields, $helptext, $retrieveopts, $updateopts) {
+function print_auth_lock_options ($auth, $user_fields, $helptext, $retrieveopts, $updateopts, $custom_fields = array()) {
 
     echo '<tr><td colspan="3">';
     if ($retrieveopts) {
@@ -176,6 +177,73 @@ function print_auth_lock_options ($auth, $user_fields, $helptext, $retrieveopts,
         }
         echo '</tr>';
     }
+
+    if(!empty($custom_fields)) {
+        echo '<tr><td colspan="3">';
+
+        print_heading('Custom Fields');
+
+        echo '</td></tr>';
+
+        foreach($custom_fields as $field) {
+
+            // Define some vars we'll work with
+            if (!isset($pluginconfig->{"field_map_$field"})) {
+                $pluginconfig->{"field_map_$field"} = '';
+            }
+            if (!isset($pluginconfig->{"field_updatelocal_$field"})) {
+                $pluginconfig->{"field_updatelocal_$field"} = '';
+            }
+            if (!isset($pluginconfig->{"field_updateremote_$field"})) {
+                $pluginconfig->{"field_updateremote_$field"} = '';
+            }
+            if (!isset($pluginconfig->{"field_lock_$field"})) {
+                $pluginconfig->{"field_lock_$field"} = '';
+            }
+
+            // define the fieldname we display to the  user
+            $fieldname = $field;
+            if ($fieldname === 'lang') {
+                $fieldname = get_string('language');
+            } elseif (preg_match('/^(.+?)(\d+)$/', $fieldname, $matches)) {
+                $fieldname =  get_string($matches[1]) . ' ' . $matches[2];
+            } elseif ($fieldname == 'url') {
+                $fieldname = get_string('webpage');
+            } else {
+                $fieldname = get_field('user_info_field', 'name', 'shortname', $fieldname);
+            }
+            if ($retrieveopts) {
+                $varname = 'field_map_' . $field;
+
+                echo '<tr valign="top"><td align="right">';
+                echo '<label for="lockconfig_'.$varname.'">'.$fieldname.'</label>';
+                echo '</td><td>';
+
+                echo "<input id=\"lockconfig_{$varname}\" name=\"lockconfig_{$varname}\" type=\"text\" size=\"30\" value=\"{$pluginconfig->$varname}\" />";
+                echo '<div style="text-align: right">';
+                echo '<label for="menulockconfig_field_updatelocal_'.$field.'">'.get_string('auth_updatelocal', 'auth') . '</label>&nbsp;';
+                choose_from_menu($updatelocaloptions, "lockconfig_field_updatelocal_{$field}", $pluginconfig->{"field_updatelocal_$field"}, "");
+                echo '<br />';
+                if ($updateopts) {
+                    echo '<label for="menulockconfig_field_updateremote_'.$field.'">'.get_string('auth_updateremote', 'auth') . '</label>&nbsp;';
+                    choose_from_menu($updateextoptions, "lockconfig_field_updateremote_{$field}", $pluginconfig->{"field_updateremote_$field"}, "");
+                    echo '<br />';
+
+
+                }
+                echo '<label for="menulockconfig_field_lock_'.$field.'">'.get_string('auth_fieldlock', 'auth') . '</label>&nbsp;';
+                choose_from_menu($lockoptions, "lockconfig_field_lock_{$field}", $pluginconfig->{"field_lock_$field"}, "");
+                echo '</div>';
+            } else {
+                echo '<tr valign="top"><td align="right">';
+                echo '<label for="menulockconfig_field_lock_'.$field.'">'.$fieldname.'</label>';
+                echo '</td><td>';
+                choose_from_menu($lockoptions, "lockconfig_field_lock_{$field}", $pluginconfig->{"field_lock_$field"}, "");
+            }
+            echo '</td>';
+            echo '</tr>';
+        }
+    }
 }
 
 ?>
diff --git a/auth/ldap/auth.php b/auth/ldap/auth.php
index dd05be6..48cedc0 100644
--- a/auth/ldap/auth.php
+++ b/auth/ldap/auth.php
@@ -36,6 +36,12 @@ require_once($CFG->libdir.'/authlib.php');
 class auth_plugin_ldap extends auth_plugin_base {
 
     /**
+     * moodle custom fields to sync with
+     * @var array()
+     */
+    var $custom_fields = array();
+
+    /**
      * Constructor with initialisation.
      */
     function auth_plugin_ldap() {
@@ -89,6 +95,12 @@ class auth_plugin_ldap extends auth_plugin_base {
             // so leave $this->config->objectclass as is.
         }
 
+        $custom_fields = get_records('user_info_field', '', '', '', 'shortname');
+
+        foreach($custom_fields as $cf) {
+            $this->custom_fields[] = $cf->shortname;
+        }
+
     }
 
     /**
@@ -233,7 +245,9 @@ class auth_plugin_ldap extends auth_plugin_base {
                 } else {
                     $newval = $textlib->convert($user_entry[0][$value], $this->config->ldapencoding, 'utf-8');
                 }
-                if (!empty($newval)) { // favour ldap entries that are set
+
+                    //need to allow for 0 or '0' will ldap ever return an empty string or will the array_key_exists catch such things?
+                if (isset($newval) || $newval !== '') { // favour ldap entries that are set
                     $ldapval = $newval;
                 }
             }
@@ -1107,14 +1121,26 @@ class auth_plugin_ldap extends auth_plugin_base {
             }
             $user_entry = $user_entry[0];
 
-            //error_log(var_export($user_entry) . 'fpp' );
-
             foreach ($attrmap as $key => $ldapkeys) {
                 // only process if the moodle field ($key) has changed and we
                 // are set to update LDAP with it
-                if (isset($olduser->$key) and isset($newuser->$key)
-                  and $olduser->$key !== $newuser->$key
-                  and !empty($this->config->{'field_updateremote_'. $key})) {
+
+                //updating a custom field or a standard field/ 0 for none at all
+                $update_attr_type = 0;      
+
+                if(isset($olduser->$key) and isset($newuser->$key)
+                    and $olduser->$key !== $newuser->$key) {
+                        //updating a user profile field
+                    $update_attr_type = 2;
+                    $profile_field = $key;
+                } else if(isset($olduser->{'profile_field_' . $key}) and isset($newuser->{'profile_field_' . $key})
+                    and $olduser->{'profile_field_' . $key} !== $newuser->{'profile_field_' . $key}){
+                        //updating a custom profile field
+                    $update_attr_type = 1;
+                    $profile_field = 'profile_field_' . $key;
+                }
+
+                if (!empty($profile_field) and !empty($this->config->{'field_updateremote_'. $key})) {
                     // for ldap values that could be in more than one
                     // ldap key, we will do our best to match
                     // where they came from
@@ -1127,13 +1153,14 @@ class auth_plugin_ldap extends auth_plugin_base {
                         $ambiguous = false;
                     }
 
-                    $nuvalue = $textlib->convert($newuser->$key, 'utf-8', $this->config->ldapencoding);
+                    $nuvalue = $textlib->convert($newuser->$profile_field, 'utf-8', $this->config->ldapencoding);
                     empty($nuvalue) ? $nuvalue = array() : $nuvalue;
-                    $ouvalue = $textlib->convert($olduser->$key, 'utf-8', $this->config->ldapencoding);
+                    $ouvalue = $textlib->convert($olduser->$profile_field, 'utf-8', $this->config->ldapencoding);
 
                     foreach ($ldapkeys as $ldapkey) {
                         $ldapkey   = $ldapkey;
-                        $ldapvalue = $user_entry[$ldapkey][0];
+                        $ldapvalue = empty($user_entry[$ldapkey][0])? null: $ldapvalue;
+                        
                         if (!$ambiguous) {
                             // skip update if the values already match
                             if ($nuvalue !== $ldapvalue) {
@@ -1715,6 +1742,19 @@ class auth_plugin_ldap extends auth_plugin_base {
                 }
             }
         }
+
+        //add custom fields
+        foreach($this->custom_fields as $field) {
+            if(!in_array($field, $this->userfields)) { //just in case so we don't overwrite any values in the user fields
+                if(!empty($this->config->{"field_map_$field"})) {
+                    $moodleattributes[$field] = $this->config->{"field_map_$field"};
+                    if (preg_match('/,/',$moodleattributes[$field])) {
+                        $moodleattributes[$field] = explode(',', $moodleattributes[$field]); // split ?
+                    }
+                }
+            }
+        }
+
         $moodleattributes['username'] = $this->config->user_attribute;
         return $moodleattributes;
     }
@@ -2014,7 +2054,7 @@ class auth_plugin_ldap extends auth_plugin_base {
      *
      * @param array $page An object containing all the data for this page.
      */
-    function config_form($config, $err, $user_fields) {
+    function config_form($config, $err, $user_fields, $custom_fields=array()) {
         include 'config.html';
     }
 
diff --git a/auth/ldap/config.html b/auth/ldap/config.html
index 52eb219..fad12ca 100644
--- a/auth/ldap/config.html
+++ b/auth/ldap/config.html
@@ -491,7 +491,7 @@ $help .= get_string('auth_updateremote_expl','auth');
 $help .= '<hr />';
 $help .= get_string('auth_updateremote_ldap','auth');
 
-print_auth_lock_options('ldap', $user_fields, $help, true, true);
+print_auth_lock_options('ldap', $user_fields, $help, true, true, $custom_fields);
 
 ?>
 
diff --git a/lib/moodlelib.php b/lib/moodlelib.php
index 6834d82..4da422b 100644
--- a/lib/moodlelib.php
+++ b/lib/moodlelib.php
@@ -2983,8 +2983,41 @@ function update_user_record($username, $authplugin) {
                 // nothing_ for this field. Thus it makes sense to let this value
                 // stand in until LDAP is giving a value for this field.
                 if (!(empty($value) && $lockval === 'unlockedifempty')) {
-                    set_field('user', $key, $value, 'username', $username)
-                        || error_log("Error updating $key for $username");
+                    if(in_array($key, $authplugin->userfields)) {
+                        //if the field modified is one of the standard fields then change it
+                        set_field('user', $key, $value, 'username', $username) || error_log("Error updating $key for $username");
+                    } else if(in_array($key, $authplugin->custom_fields)) {
+                        //if there is no value in the user_info_data then
+                        $info_field = get_record('user_info_field', 'shortname', $key);
+
+                        $userid = get_field('user', 'id', 'username', $username);
+
+                        $data = get_field('user_info_data', 'data', 'userid', $userid, 'fieldid', $info_field->id);
+
+                        if($data === false) {
+                            $data = $info_field->defaultdata;
+                        }
+
+                        if(strcmp($data, $value) !== 0) {
+                            $valid = true;
+
+                                //check to make sure that the value we are placing in is a valid one
+                            if(strcmp($info_field->datatype, 'menu') == 0){
+                                $validValues = explode("\n", $info_field->param1);
+                                if(!in_array($value, $validValues)) {
+                                    $valid = false;
+                                }
+                            } else if(strcmp($info_field->datatype, 'checkbox') == 0) {
+                                if($value != 1 && $value != 0) {
+                                    $valid = false;
+                                }
+                            }
+
+                            if($valid) {
+                                set_field('user_info_data', 'data', $value, 'userid', $userid, 'fieldid', $info_field->id);
+                            }
+                        }
+                    }
                 }
             }
         }
diff --git a/user/edit.php b/user/edit.php
index 6fca8fe..fe1db39 100644
--- a/user/edit.php
+++ b/user/edit.php
@@ -97,7 +97,6 @@
     //Load custom profile fields data
     profile_load_data($user);
 
-
     //create form
     $userform = new user_edit_form();
     if (empty($user->country)) {
