How to display code on frontend if logged in

#1

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

0 Likes

#2

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

<?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:

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

but i think theres an actual helper for it now.

0 Likes

#3

Found it!

href="<?= $page->panelUrl() ?>"
1 Like

#4

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() ?>"

0 Likes

#5

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

$user->isLoggedIn(): bool

0 Likes

#6

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…

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

Tweaked snippet…

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

Which means you can do this…

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

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).

1 Like

#7

:slight_smile: 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.

0 Likes

#8

Even if you reload the page after logging out?

There is a whole cookbook guide on protecting stuff.

1 Like

#9

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 :slight_smile:

0 Likes

#10

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

0 Likes

#11

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

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

#12

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.

0 Likes

#13

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?

0 Likes

#14

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.

0 Likes

#15

Yes, before cache is was working on all pages.

0 Likes

#16

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

0 Likes

#17

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.

0 Likes

#18

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

0 Likes

#19

Will it work with the other types of caching?

0 Likes

#20

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

0 Likes