1

I know I can set postLoginRedirect in config/general.php to send the user to a specific page after login. Is there a way to grab that value in the template? This came up because I noticed that the {{siteUrl}} href in the default _layout.html template was sending the user to the homepage (index.html), but I'd like to conditionally replace that link for a logged-in user.

3

Sure, something like this should work (untested):

<a href="{{ currentUser ? url(craft.config.get('postLoginRedirect')) : siteUrl }}">Home</a>

The above should create an absolute URL to whatever's in your postLoginRedirect config setting, relative to your siteUrl.

Note that if you want an URL relative to your Control Panel (e.g. "dashboard"), you'll want to use cpUrl() instead, i.e.

<a href="{{ currentUser ? cpUrl(craft.config.get('postLoginRedirect')) : siteUrl }}">Home</a>
  • Just noticed that with 'postLoginRedirect' => 'dashboard' using the solution above makes the postLoginRedirect a relative link, so it doesn't work on sub-pages. Can you think of a solution besides hard coding an absolute link like 'postLoginRedirect' => '/dashboard' – Keith Feb 17 '16 at 23:16
  • @Keith You're correct – just updated the answer with a solution. – Mats Mikkel Rummelhoff Feb 17 '16 at 23:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.