How to Display an “Edit Article” Link on the Joomla Frontend when Logged in to the Backend

On any news site, one of the most convenient things for editors is to have an “Edit Article” link on the article page. Such a link will save a lot of time for the editors since without it they will need to go to the backend, search for the article (which will put pressure on the database server if there are many articles on the site), and then click on the article in order to edit it.

Joomla does have frontend editing, but the user must be logged in to the frontend (and not to the backend) to be able to edit articles directly from the frontend, and serious news sites don’t allow frontend logins. So, how can one overcome this limitation?

A straightforward approach would be to add an “Edit Article” link to every single article page, for everyone. That can be done by adding the following code to the article override page in the template (e.g. in the default.php file under the templates/[template-name]/html/com_content/article folder):

<a href='<?php echo(JURI::root()); ?>administrator/index.php?option=com_content&task=article.edit&id=<?php echo($this->article->id); ?>' target='_blank'>Edit Article</a>

The above code will display an “Edit Article” link (in the location where it’s placed) that will direct a backend logged-in-user to the actual edit screen of the article, which is excellent, but is not ideal. The thing is, the “Edit Article” link will be displayed for everyone, and not just the editors, and this is where it gets increasingly tricky…

A casual Joomla developer would probably say: just get the user by using JFactory::getUser(), check if the uses can edit articles, and then display the link. The problem with this approach is that the JFactory::getUser() method will return an empty object on the frontend if the user is logged in from the backend. Try it yourself!

So, how can this problem be solved?

The easiest solution to this problem is to set a site-wide cookie when the onUserLogin event is triggered, and this is done by creating a plugin and then adding the following code to the onUserLogin method of that plugin:

setcookie('edit_article_efpke4uax2fmp789', '1', 0, '/');

So, when someone logs in to the backend of the website, the value of the edit_article_efpke4uax2fmp789 will be set to 1. This will allow us to check for that cookie on the frontend (e.g. in the article layout page) and display the “Edit Link” if that cookie is present and set to 1.

Are there any caveats?

Yes – a few. First, we will have issues with the above approach if Joomla’s native System – Cache plugin is used, as the “Edit Article” link may also appear for site visitors (non-editors). Granted, they will not be able to do anything with that link (it will just redirect them to the login page), but it’s just not professional. This situation will happen when editors check the article page before the visitors, but, if the reverse happens, then the editors just won’t see the link. The solution to this problem consists of using a modified System – Cache plugin that will take into consideration the “Edit Article” link.

Another problem is that the “Edit Article” link will appear for the editors even when they’re logged out, and this is because we are not unsetting the cookie on user logout. Now, while this can be done with the onUserLogout event, it is not enough, because, in the absolute majority of cases, editors do not click on the “Logout” button, but are rather logged out automatically by the system when their sessions expire.

Also, the “Edit Article” link does not take into consideration the fact that the article might be locked (“checked out” in Joomla’s terms) by another editor. However, this is easily remedied with a simple check prior to displaying the link.

We hope that you found our post useful and informative. If you need help with implementing the “Edit Article” link on the frontend of your Joomla website, then please contact us. Our fees are affordable, our work is quick, and our quality is top notch.

No comments yet.

Leave a comment