Skip to content

Instantly share code, notes, and snippets.

@boboldehampsink
Created November 6, 2013 12:13
Show Gist options
  • Save boboldehampsink/7335131 to your computer and use it in GitHub Desktop.
Save boboldehampsink/7335131 to your computer and use it in GitHub Desktop.
Save profile and user data simultaneously
<?php
namespace Craft;
class SaveProfilePlugin extends BasePlugin
{
function getName()
{
return Craft::t('Save profile too when saving user');
}
function getVersion()
{
return '1.0';
}
function getDeveloper()
{
return 'Bob Olde Hampsink';
}
function getDeveloperUrl()
{
return 'http://www.itmundi.nl';
}
function init() {
// Only works on the front-end
if(craft()->request->isSiteRequest()) {
// Only trigger when an user is saved
craft()->on('users.onSaveUser', function(Event $event) {
// Get user
$user = $event->params['user'];
// Set given fields on user
$fields = craft()->request->getPost('fields');
$user->getContent()->setAttributes($fields);
// Save profile
craft()->users->saveProfile($user);
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment