<p>I had a similar problem a while ago when a plugin listened for <code>onSaveUser</code> and then saved the user itself, causing an infinite loop. Someone provided me something like:</p>

<pre><code>craft()-&gt;on('users.onSaveUser', function(Event $event) {
    static $recursionLevel = 0;
    if ($recursionLevel == 0) {
        $recursionLevel++;
        // do your stuff
    }
});
</code></pre>

CC BY-SA 3.0
2017-07-07T15:05:54.577Z
2017-07-07T15:05:54.577Z
Clive Portman
<p>I have a plugin that uses its init function to save custom user data from front end registrations. It uses:</p>

<pre><code> if ($event-&gt;params['isNewUser']) {}
</code></pre>

<p>and</p>

<pre><code>if (craft()-&gt;request-&gt;isSiteRequest()) {}
</code></pre>

<p>To perform front end save routines.</p>

<p>I want to add a small  custom user save rule for admin usage outside of the IsNewUser logic in order to preserve like this:</p>

<pre><code> if (craft()-&gt;request-&gt;isCpRequest()) {
   $user = craft()-&gt;users-&gt;getUserById($uidfromURL);
   $user-&gt;getContent()-&gt;URN = 'my custom value';
   $success = craft()-&gt;users-&gt;saveUser($user);
 }
</code></pre>

<p>The problem is that this code just loops thru the plugin. I need the custom field to be manually editable, but i also need to create a default value too on initial user creation in admin.</p>

<users>
How to prevent user save loop in CP?
# How to prevent user save loop in CP?

(Asked by: username_0 on 2017-07-07)

I have a plugin that uses its init function to save custom user data from front end registrations. It uses:
 if ($event->params['isNewUser']) {}

and
if (craft()->request->isSiteRequest()) {}

To perform front end save routines.
I want to add a small  custom user save rule for admin usage outside of the IsNewUser logic in order to preserve like this:
 if (craft()->request->isCpRequest()) {
   $user = craft()->users->getUserById($uidfromURL);
   $user->getContent()->URN = 'my custom value';
   $success = craft()->users->saveUser($user);
 }

The problem is that this code just loops thru the plugin. I need the custom field to be manually editable, but i also need to create a default value too on initial user creation in admin.

**Tags:** users

---

## 1 answers

### Answer by username_1 on 2017-07-07. Score: 1

I had a similar problem a while ago when a plugin listened for onSaveUser and then saved the user itself, causing an infinite loop. Someone provided me something like:
craft()->on('users.onSaveUser', function(Event $event) {
    static $recursionLevel = 0;
    if ($recursionLevel == 0) {
        $recursionLevel++;
        // do your stuff
    }
});


---

