How to output an edit link on your pages for logged in users

Posted 22nd Sep 2017

#api

Having an admin area separate from your website means that you need to be in the admin area in order to make changes to your web pages.

So when you're looking at your website and want to make an edit, you need to go back to the admin area, find the page and click 'Edit'. The processwire API has two useful methods that can be combined to achieve this.

// Is the current page is editable by the current user? Returns true or false
$page->editable();

// the edit page URL for the current page
$page->editUrl();

The top piece of code returns true if:

a) You are logged in, and
b) Have the correct permissions to edit the current page

The second piece of code is a link directly to the edit page URL within the admin area. Try it out, put the following in one of your templates.

<?php if ($page->editable()): ?>
    <p><a href="<?= $page->editUrl; ?>">Edit this page</a></p>
<?php endif; ?>

When you go to your page which contains this code, and you're logged in, then 'Edit this page' will be displayed as a hyperlink straight to the admin edit page.

NOTE: There are other modules which add an admin bar which is super useful, or you could enable front-end editing, but these are outside the scope of this guide.

Feedback & support

I hope you enjoyed this tutorial. You can support pwtuts by following on @pwtuts.

Related tutorials / See all