# How to display code on frontend if logged in

**URL:** <https://forum.getkirby.com/t/how-to-display-code-on-frontend-if-logged-in/13765>\
**Category:** Questions\
**Tags:** v3\
**Created:** [April 11, 2019, 8:14pm UTC](https://forum.getkirby.com/t/how-to-display-code-on-frontend-if-logged-in/13765 "2019-04-11T20:14:01Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roman](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/roman/32/6571_2.png) [@Roman](https://forum.getkirby.com/u/Roman)\
**Post date:** [April 11, 2019, 8:14pm UTC](https://forum.getkirby.com/t/how-to-display-code-on-frontend-if-logged-in/13765/1 "2019-04-11T20:14:01Z")

</div>

Hi, searched a lot, can’t find solution. I would like to add EDIT links to frontend. It’s very necessary feature, as many times I can notice something wrong in certain page, and it’s much easier to edit when I am already at that page, rather that open panel and look for it there.

So, what I need to do, is to show some code on frontend, if I am logged in. Something like:

```
<?php if(I_AM_LOGGED_IN): ?>
  <a href="/panel/pages/parent-slug">EDIT</a>
<?php endif ?>

```

and link should be different for inner pages: /panel/pages/parent-slug+inner-page-slug

---

<div class="post-metadata">

**Author:** ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)\
**Post date:** [April 11, 2019, 8:38pm UTC](https://forum.getkirby.com/t/how-to-display-code-on-frontend-if-logged-in/13765/2 "2019-04-11T20:38:41Z")

</div>

I use this on a Kirby 2 site which i think should still fly …

```auto
<?php if ($user = $site->user() and $user->hasPanelAccess()): ?>
<!-- your link -->
<?php endif; ?>

```

I think theres an easier way now populate the URL for the page in the panel, but I cant quite recall the exact thing. Try looking up `panel()` in the reference.

This should work:

```auto
href="<?= $site->url() ?>/panel/pages/<?= $page->uri() ?>"

```

but i think theres an actual helper for it now.

---

<div class="post-metadata">

**Author:** ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)\
**Post date:** [April 11, 2019, 8:43pm UTC](https://forum.getkirby.com/t/how-to-display-code-on-frontend-if-logged-in/13765/3 "2019-04-11T20:43:39Z")

</div>

Found it!

```auto
href="<?= $page->panelUrl() ?>"

```

---

<div class="post-metadata">

**Author:** ![Roman](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/roman/32/6571_2.png) [@Roman](https://forum.getkirby.com/u/Roman)\
**Post date:** [April 11, 2019, 8:46pm UTC](https://forum.getkirby.com/t/how-to-display-code-on-frontend-if-logged-in/13765/4 "2019-04-11T20:46:07Z")

</div>

Thanks, I tried similar code to check if logged in, but it’s showing content after logout too.

And this edit link is working, thank you (shows + for children after parent, as needed)!  
`href="<?= $page->panelUrl() ?>"`

---

<div class="post-metadata">

**Author:** ![Roman](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/roman/32/6571_2.png) [@Roman](https://forum.getkirby.com/u/Roman)\
**Post date:** [April 11, 2019, 8:48pm UTC](https://forum.getkirby.com/t/how-to-display-code-on-frontend-if-logged-in/13765/5 "2019-04-11T20:48:13Z")

</div>

Found this: [https://getkirby.com/docs/reference/objects/user/is-logged-in](https://getkirby.com/docs/reference/objects/user/is-logged-in)

`$user->isLoggedIn(): bool`

---

<div class="post-metadata">

**Author:** ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)\
**Post date:** [April 11, 2019, 8:53pm UTC](https://forum.getkirby.com/t/how-to-display-code-on-frontend-if-logged-in/13765/6 "2019-04-11T20:53:00Z")

</div>

Good stuff, ill give you another tip for it… this is fine for single pages but what if you want edit pages listed out on something like a blog list page?

Snippets to the rescue! if you put the link in a snippet and pass a parameter to it, you can use it in a loop…

```auto
<?php snippet('edit', ['hook' => $article]) ?>

```

Tweaked snippet…

```auto
href="<?= $hook->panelUrl() ?>"

```

Which means you can do this…

```auto
<?php foreach($page->children()->visible() as $article): ?>
<h1><?= $article->title() ?></h1>
<?php snippet('edit', ['hook' => $article]) ?>
<?php endforeach ?>

```

> [@Roman](#):
>
> $user-\>isLoggedIn(): bool

Yep, thats better but you should probably also check that they have panel access, as described above, since its possible to have users that are not allowed to login to the panel, but can log into the front end (for things like protected pages etc).

---

<div class="post-metadata">

**Author:** ![Roman](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/roman/32/6571_2.png) [@Roman](https://forum.getkirby.com/u/Roman)\
**Post date:** [April 11, 2019, 8:57pm UTC](https://forum.getkirby.com/t/how-to-display-code-on-frontend-if-logged-in/13765/7 "2019-04-11T20:57:54Z")

</div>

🙂 I actually was looking for the way to edit child pages (like article or project in a list). For examlpe on frontpage I have featured sponsors, and this is working:

```
<?php foreach($site->find('sponsors')->children()->limit(9)->filterBy('featured', 'front', ',') as $sponsor): ?>

<a href="<?= $sponsor->panelUrl() ?>">EDIT</a>

```

Still can’t understand, how to show code only to logged in user. After I log out, it’s still showing.

---

<div class="post-metadata">

**Author:** ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)\
**Post date:** [April 11, 2019, 9:01pm UTC](https://forum.getkirby.com/t/how-to-display-code-on-frontend-if-logged-in/13765/8 "2019-04-11T21:01:02Z")

</div>

> [@Roman](#):
>
> After I log out, it’s still showing.

Even if you reload the page after logging out?

There is a whole [cookbook guide](https://getkirby.com/docs/cookbook/security/access-restriction) on protecting stuff.

---

<div class="post-metadata">

**Author:** ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)\
**Post date:** [April 11, 2019, 9:02pm UTC](https://forum.getkirby.com/t/how-to-display-code-on-frontend-if-logged-in/13765/9 "2019-04-11T21:02:57Z")

</div>

> [@Roman](#):
>
> way to edit child pages (like article or project in a list). For example on frontpage I have featured sponsors, and this is working:

I would still go with the snippet idea i described above, since it keeps the code for the link in one place, rather then dotted around your templates and other snippets. Easier maintenance 🙂

---

<div class="post-metadata">

**Author:** ![Roman](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/roman/32/6571_2.png) [@Roman](https://forum.getkirby.com/u/Roman)\
**Post date:** [April 11, 2019, 9:03pm UTC](https://forum.getkirby.com/t/how-to-display-code-on-frontend-if-logged-in/13765/10 "2019-04-11T21:03:17Z")

</div>

Yes, also tried on phone in incognito mode, all the same.

---

<div class="post-metadata">

**Author:** ![Roman](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/roman/32/6571_2.png) [@Roman](https://forum.getkirby.com/u/Roman)\
**Post date:** [April 11, 2019, 9:07pm UTC](https://forum.getkirby.com/t/how-to-display-code-on-frontend-if-logged-in/13765/11 "2019-04-11T21:07:22Z")

</div>

Wow, thank you a lot! Found solution 🥳 Short and easy. So, full code for someone with the same need:

```
<?php if ($kirby->user()): ?>
<a href="<?= $page->panelUrl() ?>">EDIT</a>
<?php endif ?>
```

---

<div class="post-metadata">

**Author:** ![Roman](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/roman/32/6571_2.png) [@Roman](https://forum.getkirby.com/u/Roman)\
**Post date:** [April 11, 2019, 9:18pm UTC](https://forum.getkirby.com/t/how-to-display-code-on-frontend-if-logged-in/13765/12 "2019-04-11T21:18:28Z")

</div>

Only there is one thing: after turning on cache, looks like this `<?php if ($kirby->user()): ?>` rule is only working on children pages. On parents doesn’t show anything.

---

<div class="post-metadata">

**Author:** ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)\
**Post date:** [April 11, 2019, 9:22pm UTC](https://forum.getkirby.com/t/how-to-display-code-on-frontend-if-logged-in/13765/13 "2019-04-11T21:22:07Z")

</div>

I’m not sure how the protected stuff works out with caching turned on. I Imagine the non-logged in copy of the page is in the cache and you need to flush it, or youve used the link code in a loop that only applys to children. Are you sure it worked all over before enabling the cache?

---

<div class="post-metadata">

**Author:** ![Roman](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/roman/32/6571_2.png) [@Roman](https://forum.getkirby.com/u/Roman)\
**Post date:** [April 11, 2019, 9:22pm UTC](https://forum.getkirby.com/t/how-to-display-code-on-frontend-if-logged-in/13765/14 "2019-04-11T21:22:32Z")

</div>

Update: some parents are showing, and one child is not showing, so maybe needs some time, or the reason is what content is there, don’t know.

---

<div class="post-metadata">

**Author:** ![Roman](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/roman/32/6571_2.png) [@Roman](https://forum.getkirby.com/u/Roman)\
**Post date:** [April 11, 2019, 9:23pm UTC](https://forum.getkirby.com/t/how-to-display-code-on-frontend-if-logged-in/13765/15 "2019-04-11T21:23:13Z")

</div>

Yes, before cache is was working on all pages.

---

<div class="post-metadata">

**Author:** ![Roman](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/roman/32/6571_2.png) [@Roman](https://forum.getkirby.com/u/Roman)\
**Post date:** [April 11, 2019, 9:25pm UTC](https://forum.getkirby.com/t/how-to-display-code-on-frontend-if-logged-in/13765/16 "2019-04-11T21:25:07Z")

</div>

Ah, yes, sure, that code is saved in cached versions, so need to figure out how not to cache that fragment.

---

<div class="post-metadata">

**Author:** ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)\
**Post date:** [April 11, 2019, 9:25pm UTC](https://forum.getkirby.com/t/how-to-display-code-on-frontend-if-logged-in/13765/17 "2019-04-11T21:25:19Z")

</div>

I’m afraid i dont know a huge amount about the caching. you could at least delete the cache folder and see after logging in and see if it works, at least then you know its the cache.

Other then excluding those pages from the cache, im not sure how to solve it.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [April 11, 2019, 9:30pm UTC](https://forum.getkirby.com/t/how-to-display-code-on-frontend-if-logged-in/13765/18 "2019-04-11T21:30:36Z")

</div>

You can’t use the file cache in combination with stuff that should only be visible to logged in users.

---

<div class="post-metadata">

**Author:** ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)\
**Post date:** [April 11, 2019, 9:34pm UTC](https://forum.getkirby.com/t/how-to-display-code-on-frontend-if-logged-in/13765/19 "2019-04-11T21:34:02Z")

</div>

> [@texnixe](#):
>
> can’t use the file cache in combination with stuff that should only be visible to logged in users.

Will it work with the other types of caching?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [April 11, 2019, 9:36pm UTC](https://forum.getkirby.com/t/how-to-display-code-on-frontend-if-logged-in/13765/20 "2019-04-11T21:36:54Z")

</div>

Not with anything that stores the complete HTML page. A custom cache that stores parts of pages will work.

[Next page](https://forum.getkirby.com/t/how-to-display-code-on-frontend-if-logged-in/13765.md?page=2)
