We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 44407
    • 3 Posts
    I am trying to write a plugin that adds custom extended fields when a new user is created...This is what I have. What am I doing wrong?

    MODX Revolution 2.2.8-pl (traditional)

    onUserSave
    
    <?php
    $id = //get the id of the newly created user somehow
    $modx->query('UPDATE modx_user_attributes SET extended=`{"mailchimp":{"mailing_list":"","event_list":"","template_list":""}}` WHERE internalKey=$id');
    
      • 42562
      • 1,145 Posts
      The Login extra has that feature already, unless you want something more.
      http://rtfm.modx.com/display/ADDON/Login.Extended+User+Profiles

      Use the Register snippet. New users will have the extended fields you specify.

      Maybe you are manually creating the users?
        TinymceWrapper: Complete back/frontend content solution.
        Harden your MODX site by passwording your three main folders: core, manager, connectors and renaming your assets (thank me later!)
        5 ways to sniff / hack your own sites; even with renamed/hidden folders, burst them all up, to see how secure you are not.
        • 3749
        • 24,544 Posts
        If you do need to add extended fields when creating a user manually, this should do it. Your method, if it worked, would wipe out any existing extended fields that might have been created by an extra.

        If you're creating the users in the Manager, I would use OnUserFormSave.

        /* only act for new users */
        if ($mode != modSystemEvent::MODE_NEW) return;
        
        $profile = $user->getOne('Profile');
        if ($profile) {
            $extended = $profile->get('extended');
            $extended['mailing_list'] = '';
            $extended['event_list'] = '';
            $extended['template_list'] = '';
        
            $profile->set('extended', $extended);
            $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