We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 53073
    • 10 Posts
    Hello

    I'd like to remove some specific extended user fields, pragmatically.
    How to do that ?
      • 3749
      • 24,544 Posts
      The extended fields are just a just a JSON string in the 'extended' field of the modUserProfile object. If there are fields you don't want to use, it doesn't hurt to leave them there, though maybe you want to stop showing them in the Manager when you go to "Manage Users."

      This (untested) should remove the extended field for a given user:

      $fieldName='someField';
      $userId = 12; // or whatever
      
      $profile = $modx->getObject('modUserProfile', array('internalKey' => $userId));
      
      if ($profile) {
         $extended = $profile->get('extended');
         if (isset($extended[$fieldName])) {
             unset($extended[$fieldName]);
             $profile->set('extended', $extended);
             $profile->save();
         }
      }
      


      To remove the field for all users, it would look like this:

      $fieldName='someField';
      
      $profiles = $modx->getCollection('modUserProfile');
      
      foreach ($profiles as $profile) {
         $extended = $profile->get('extended');
         if (isset($extended[$fieldName])) {
             unset($extended[$fieldName]);
             $profile->set('extended', $extended);
             $profile->save();
         }
      }
      


      If you want to remove *all* extended fields, you can do this in the code above:
      foreach ($profiles as $profile) {
          $profile->set('extended', array());
          $profile->save();
      }








        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting