Blogging

Frontend Editing for MovableType

by Michael Schmidt - Aug 13, 2003

The concept of “Frontend Editing” has been used in high-end CMS like [offsite]RedDot SmartEdit or [offsite]Typo3 for quite some time now. Its purpose is to edit content directly on the page or at least initiate the editing dialogue from a link close to the content. This article describes a solution for implementing “Frontend Editing” for MovableType.

[Thumbnail sized screenshot of adognamedfish.com showing Adminlinks in use]Using frontend editing has several advantages. First you don’t have to work your way through the whole MT interface to correct the spelling error you just found somewhere in your archives or to delete the latest troll comments. A link that leads you to the appropriate dialogue could be right next to the content. Check out the screenshot from [offsite]Edda’s blog. Quite convenient. Second, these links are extremely useful if the blog will be used by authors you don’t want to confront with underlying concepts or who you don’t want to train in the usage of MT. This is often the case if you use MT to power non-blog websites. I would say that MT is comparably easy to use but that doesn’t save me from having to explain the interface over and over again to my users. With “Frontend Editing” they need much less training on the MT interface and can now intuitively edit their webpage with just one click.

The idea to do this with MT is not new. It is even partly built in already. If you use the search templates that come with MT you might have noticed the <MTEntryEditLink> Search Results Tag which creates a link to edit the entry represented by a search result. Also [offsite]Jay Allen mentioned in a comment on [offsite]Stopdesign that he is implementing links on his clients’ websites which make it possible to open corresponding edit dialogues directly onsite. I picked up his idea and converted it into a plugin.

Implementation

So, how does it work? The idea is to embed links in our templates that lead directly back to the appropriate edit dialogue. As we’ll see in the following showcase this is pretty easy to do and can even be done manually. As an example we’ll create an entry-sensitive link. The URL for the “Edit” page of an MT entry looks like this:

/mt/mt.cgi?__mode=view&_type=entry&id=630&blog_id=12

Now to add an “Edit Entry” link to each of our entries all we have to do is to replace the entry and blog ID with the appropriate values. This can be achieved by using the <MTEntryID> and <MTBlogID> template tags.

<MTEntries>
    <MTEntryTitle>
    <MTEntryBody>

<a href="<MTCGIPath>mt.cgi?__mode=view&_type=entry&id=<MTEntryID>&blog_id=<MTBlogID>">
      Edit Entry
</a>

</MTEntries>

As you might already have noticed this link will be visible for everybody. This isn’t a critical problem because MT’s authentication mechanism is still protecting our content. If an unauthorised user tries to edit an entry all she’ll see is the login screen. But it is preferable to dynamically show or hide these links and make them only visible for “authenticated” users. This will prompt the usage of a server side scripting language though. Since a lot of people already use PHP to enhance their blogs with additional functionality I decided to use PHP for that purpose. We’ll set a cookie and use PHP to test whether the cookie is set or not.

The following code will only show the content between the two PHP statements if a cookie named admin is set:

<?php if (isset($_COOKIE["admin"])) {?>

<a href="<MTCGIPath>mt.cgi?__mode=view&_type=entry&id=<MTEntryID>&blog_id=<MTBlogID>">
      Edit Entry
</a>

<?php } ?>

So in this example only users with the admin cookie will be able to see the “Edit Entry” link. To set and unset the Admin cookie we’ll use the following code:

<?php
# set admin cookie. It will last for 1 year
setcookie("admin", "admin", time()+31536000, "/");

# unset admin cookie
setcookie("admin", "admin", time()-3600, "/");
?>

If you don’t want to introduce your blog to PHP just to hide your AdminLinks you can try to camouflage them. To make the links less obvious you could for example put them on an inconspicuous design element, use just a dot as link caption or create an extra style which is different from your usual link style.

Update: I just found out that almost exactly the same approach was used by Breena in her tip about [offsite]Edit Link for Entries with PHP. Go figure…

AdminLinks Plugin

To present you with at least something new I will show you how to use the AdminLinks plugin for MT. It doesn’t do anything fancy you couldn’t do yourself if you followed the above example. But the plugin is easier to use and makes your templates more readable. The plugin provides you with two new template tags.

  1. <MTAdminLink>: Outputs a context sensitive link for direct manipulation of blog, entries or comments.
  2. <MTAdminOnly>: Container tag that hides its contents from not authenticated visitors using the method described above.

The usage of <MTAdminOnly> is straight forward. Everything you enclose within this container tag will be protected by the PHP statement posted above. It will only be displayed for users with set admin cookie.

To specify what kind of link <MTAdminLink> should create you have to provide a type attribute. The following values are possible:

  • blogmenu: creates link to main page of blog
  • newentry: creates link to new entry dialogue of blog
  • editentry: creates link to edit entry dialogue of entry
  • editcomment: creates link to edit comment dialogue of comment
  • deletecomment: creates link to delete dialogue of comment
  • rebuild: creates link to rebuild dialogue of blog

Here’s an example:

<MTAdminOnly>
  <a href="<MTAdminLink type="editentry">">Edit Entry</a>
</MTAdminOnly>

In this example <MTAdminLink> creates a link to edit an entry. Since it is enclosed in <MTAdminOnly> tags the link is only visible for people with set admin cookie.

The enclosed [onsite]picture shows an example of a site that is using the AdminLinks plugin. The red circled parts of the site are AdminLinks which are not visible to the casual visitor.

I hope people will find this plugin useful. I sure did myself.

Ressources

Michael is a geek. He's currently sweating in Berlin, Germany.

Trackbacks

TrackBack is a system for enabling "conversations between weblogs". The links listed below are links to posts in other weblogs that reference this article:

To manually trackback this article use the following URL:

http://www.brain-dump.com/trackback/1

Comments

#1 | 20:04 on 14 October 2003 | by Esther

I just wanted to publicly announce that I really like this plugin. It saves me tons of clicks every time I want to delete yet another troll comment from my site...

#2 | 14:42 on 5 February 2004 | by gary

Wanted to say thanks for this script, but to add that there is a problem with implementing it with some hosts, it appears! I could *not* get the on/off scripts to work anywhere in my account except the root folder. No matter what the permissions were for folder or files, I would get either file not founds or permissions denied errors. A tip from a http://www.Forum4Bloggers.com user fixed it though, although i don't know why. My host account, by default, has indexing turned off. When I enabled indexing (std. text) for the folder where I stored the two .php files, then they worked just fine. Weird, but true!

#3 | 15:02 on 5 February 2004 | by Michael

Thank you for your comment, Gary. I read the thread on http://forum4bloggers.com/viewtopic.php?t=125.

Well, this looks strange indeed. I have a slight suspicion why this doesn't work. I will look into it tonight.

You can try to replace the header redirection in the log(on|off) files to a static URL. Maybe getting your root directory dynamically somehow collides with the directory listing.

#4 | 16:17 on 5 February 2004 | by gary

Great. Also thought about the possibility of adding a simple print echo (?) statement to each to show something like "AdminLinks are turned on." or equal. As is, nothing is returned, leaving the user to wonder if it worked! I put a dummy index page in the folder with the two scripts so I see that when the script completes, but would be nice to have the more positive feedback that it's now "on" or "off." This is probably really simple for codeheads, but alas, that's not me...

#5 | 16:26 on 5 February 2004 | by gary

"You can try to replace the header redirection in the log(on|off) files to a static URL."
Just wanted to post that this tricked worked, and although I wasn't sure which static URL to use, I used the url to where these two script files are. Scripts ran fine without errors (and this was a new directory where I hadn't turned indexing on).

#6 | 21:03 on 5 February 2004 | by Michael

I investigated the problem Gary reported. The dynamic redirection in the logon/logoff scripts was causing problems in directories without a welcome file (e.g. index.html). I now replaced the redirection with a static message.

#7 | 17:07 on 6 February 2004 | by gary

Great! I did put a dummy index.html page in the directories, though, and it still did not work. For me the static url and/or the index on/off fixed it.

On another note, if you're keeping a list of cool toys to add to this, how about a way to link to edit the template that created the page? I'm helping someone over at the MT support forum on this issue and think it would fit well with this plugin.

#8 | 12:13 on 8 February 2004 | by Michael

Gary,

I already thought about the template links. I even tried to implement it but I think it can't be done without hacking the MT sources. And that's something I don't want to have as a requirement for this simple plugin.

What you can do is to create the template links yourself. It should be quite easy if you follow the examples given in this article.

#9 | 22:28 on 8 February 2004 | by gary

Thanks, Michael. I have worked the edit template links successfully inside the MTAdminOnly tags, and it works well. Was a bit tricky getting the link for my floating side bar as well as the contents template, but managed that as well. Could also have done a sql query to pick up the template_id, but couldn't figure out it would know what template it was. Anyway, manual worked fine. I posted a screen cap of what I ended up with at the thread link you noted above. Slick stuff!

#10 | 19:01 on 10 February 2004 | by Michael Pate

I have it working except for one little thing: it is neglecting to put the slash in between the directory path and the mt.cgi script (which makes it a little less useful). You can surf over and see since I removed the code that hides the edit links. Is there something I am missing somewhere?

#11 | 19:11 on 10 February 2004 | by Michael

Michael,

I already got a complaint about that last week. Make sure the CGIPath variable in your mt.cfg file has a trailing slash.

See:
http://www.movabletype.org/docs/mtinstall.html#installation%20directories

#12 | 15:32 on 2 March 2004 | by Ton Ensing

Ahh, just the thing I was looking for. Thanks for creating this beauty. :) Got two questions tho. Would there be a way to just use the entry form in MT's main Edit or New Entry screens, that is without the main MT skin (buttons, searchbox and username) to edit an entry or post a new one? Any suggestions? Second: would it be possible to login as a special guest user, with special guest access (as defined in the main MT author/privileges settings).

#13 | 16:02 on 2 March 2004 | by Michael

Ton,

1. This isn't possible without hacking the MT templates but it's possible.
2. Sure, no problem. Just set up a special user and log in.

I can imagine what you want to do. Maybe setting up your own post entry form and using the XML-RPC API could do the trick without serious hacks. Jay Allen did something like that. See the links in the third paragraph of this article.

#14 | 22:00 on 12 March 2004 | by Jason Mevius

This is a great addition (I dropped it into my personal site immediately). I was wondering about a possible extension/feature request.

I have a client that schedules training courses. They desire a calendar of all of the courses scheduled in the upcoming year. I have used Brad Choate's Supplemental Calendar Tags (http://www.bradchoate.com/weblog/2003/01/27/calx) to generate the current month + the next 11 months. That part works great.

When I saw this plugin, I instantly thought of making the dates in the publicly viewable calendar clickable 'Add Course' links for that date. Can this be used to pass the date to MT in one operation?

#15 | 00:14 on 18 March 2004 | by Geoff

Wouldn't it be easier to use the MT session instead of a seperate cookie? Or am I overlooking something?

#16 | 08:54 on 20 March 2004 | by Lisa

I've noted a few issues using MTAdminLink. I love it to death though.

1) When I use this to delete a comment, I get a hash error on rebuilding. I can replicate this every time - let me know if you need the error.

2) When I use this to edit a comment and then save the post, i get an error 500.

I only think these are related to the plugin because they don't do that when I go through the normal steps of using the MT interface.

Maybe it's just me, though. =)

Thanks for making it available! It has saved me a TON of time.

#17 | 15:59 on 21 March 2004 | by Michael

Hey Lisa,

I already stumbled across that Hash error before. To be honest I don't know what MT complains about. I guess it's a bug in MT. The comment gets deleted fine, right?

I think the same error message appears if you delete a comment from within MT but you just can't see it because the window is closed immediately.

On my blog I constructed the delete comment link like this:

<a href="" onclick="javascript:window.open('<MTAdminLink type="deletecomment">', 'confirm_delete', 'width=370,height=150');">Delete</a>

This will open a popup (just like in MT) and redirect you to the edit entry dialog => no visible Hash error... ;)

I can't reproduce your second problem. Are you sure this is related to MTAdminLinks?

Rock on,

Michael

#18 | 16:15 on 21 March 2004 | by Michael

Geoff,

now that you mention it I can't remember why I chose to go with a custom cookie. I think it would in deed be possible to check for MT's cookie.

I can vaguely remember that there were problems when checking for MT cookies when you log in with different users or you host multiple blogs on one domain.

It should be easy enough to modify the plugin to check for another cookie. See "sub adminOnly" in AdminLinks.pl.

#19 | 22:16 on 26 March 2004 | by Lisa

The second problem appears to have disappeared, it may have been an issue with my change in hosts...

I'll change it to that popup, that is *brilliant* thank you for posting it. =)

I'd love to see this use MT's cookies, tht would make it a lot more useful when on the road. But I'm not nearly clever enough to make the change so if anyone does and you release it with the plugin, I'll grab it then. It's a minor thing anyway, for me. =)

#20 | 16:09 on 6 May 2004 | by Jay Levitt

I installed the AdminLinks plugin, and added the MTAdminOnly tags around an "Edit Entry" link, just as the example shows. However, not realizing I needed to add the logon/logoff pages, I went ahead and rebuilt.. and yet it still shows the Edit Entry link on each entry, despite the fact that I haven't logged in. Others see it too.

Any ideas what I'm doing wrong, or how I can troubleshoot? I know nothing about PHP.

#21 | 16:54 on 6 May 2004 | by Michael

Jay, I checked out your blog and saw what the problem is. For the AdminOnly container to work your pages have to be parsed as PHP. Check out the source code. You will find PHP source code.

If your host supports PHP it might be enough to tell MT to use .php as ending instead of .html. You can change this setting in the weblog configuration screen.

Another option would be to disguise the admin links as a dot or a small icon or something.

#22 | 19:26 on 6 May 2004 | by Jay Levitt

Bingo! Thanks for answering so quickly - and for posting it to my blog as well. It makes sense, of course; how would Apache know to parse PHP if I don't give it the .php extension (or serve all HTML as PHP)? For now, I did the former.. problem solved.

#23 | 03:36 on 19 May 2004 | by Michael Hanscom

I've been using a similar (non-plugin based) trick to give myself 'edit' links for quite some time now, first on my MovableType weblog, now on TypePad. With a little creative stylesheet work, the links aren't even visible to the great majority of visitors.

My writeup from Jan. 2003: http://www.michaelhanscom.com/eclecticism/2003/01/mt_easy_comment.html

#24 | 15:48 on 19 May 2004 | by Jay Cohen

We've really appreciated how Basecamp (a web-based project management system that we use - from the folks at 37signals) has "front-end" (or what we like to call "inline") editing of posts/messages and comments. Surely better than having to go to the "admin" section somewhere else. Makes everything speedier, more usable, and certainly more convenient.

#25 | 16:02 on 19 May 2004 | by Jay Cohen

Whoops... Used HTML for that Basecamp link and it died.
Should be: http://www.basecampHQ.com

#26 | 19:11 on 19 May 2004 | by The Blog Father

Just a simple question - does this work with MT3.0D? It looks like a very handy feature but I just need to know if it works with the latest edition of Moveable Type.

#27 | 10:37 on 20 May 2004 | by Arvind

Nope it doesn't, just tried it out and the link doesn't show, the cookie is there though !

#28 | 20:47 on 21 May 2004 | by Quadsk8

The MTAdminOnly link shows up on my pages, but when I delete a comment I get:

MT::App::CMS=HASH(0x835d868) Use of uninitialized value in concatenation (.) or string at lib/MT/App/CMS.pm line 1263.

when I edit a comment I get:

MT::App::CMS=HASH(0x835d790) Use of uninitialized value in join or string at (eval 14) line 2.

What can I be doing wrong?

#29 | 20:55 on 21 May 2004 | by Quadsk8

Nevermind, I used your popup solution #17, that did work
Thanks anyhow!

Lawrence

#30 | 10:38 on 22 May 2004 | by Neil T.

The Blog Father: I believe it does work on MT3, except the cookie is called 'mt_user' instead of 'admin'.

#31 | 23:13 on 23 May 2004 | by mjulius

Wow. This is what I have been looking for for some time. I have a problem though. I have tried both solutions (manual and the plugin) with the same result. The first time that I use it it works fine but after that it sends me directly to the MT login screen. The blog that I use this on is a personal blog in a password protected folder. Would this somehow cause a problem with accessing whatever it needs to directly edit the entry?

#32 | 00:14 on 24 May 2004 | by Jason Brinkle

Downloaded the AdminLinks plugin. One *small* problem: the php scripts have a malformed ending title tag. It says instead of . Otherwise, a great plugin. Thanks!

#33 | 06:20 on 26 May 2004 | by Emily

i can see all the links but the problem is when i click on them, it takes me to the login screen. only when i have previuosly logged in does it bring me straight to the targeted url. what should i do?

#34 | 18:54 on 6 June 2004 | by eBlog

Hi, is that plugin MT3 friendly regarding to the cookie ?
Report :
- It's not clear in the readme that the php files must be browsed before.

">Nouvelle Entre

- This shows 2 similar links, how comes ?

#35 | 18:57 on 6 June 2004 | by eBlog

<MTEntries last="0">
<a href="<MTAdminLink type="newentry">">Nouvelle Entre</a>
</MTEntries>

#36 | 05:14 on 31 July 2004 | by Hashim

the code almost works in typepad. It looks like they changed something that messes it up.

#37 | 01:50 on 15 August 2004 | by SeekingO.com

It makes sense, of course; how would Apache know to parse PHP if I don't give it the .php extension (or serve all HTML as PHP)? You can use .htaccess file directives to parse without a .php extension like http://www.seekingo.com. Cheers!

#38 | 00:05 on 5 December 2004 | by New User

To get apache to parse both .php and .html files inthe target dir put this in the dir's .htaccess (assuming overides are allowed):

AddType application/x-httpd-php .php .html

#39 | 19:26 on 4 January 2005 | by Jon

With the latest releases of MT (I'm using 3.14), there's a new feature that allows for dynamic PHP publishing. On static pages, the AdminLinks plugin is working as great as ever (I started using it with MT 2.x), but on dynamically created pages, I get this error and nothing else:

Smarty error: [in mt:34 line 33]: syntax error: unrecognized tag 'MTAdminLink' (Smarty_Compiler.class.php, line 556)

I'd like to continue using the plugin, it's been really useful, but I wanted to start using dynamic publishing too. They just don't seem to work together very well at the moment, or maybe it's just me. ;)

#40 | 14:01 on 3 February 2005 | by Graham

If a user somehow gets on the set cookies-page he still can see the adminlinks. Why isn't it possible to use the MT-security itself for this?

When I go to the MT-login page I'm logged in automatically. So a cookie is set? Why can't that cookie be reused? It it possible to set your cookie right after (the first)login in MT only so I know for sure that that cookie is set for an author?

#41 | 16:11 on 9 April 2005 | by tomi

NICE SITE... LOOK THIS...

#42 | 18:06 on 9 May 2005 | by NewMexiKen

Same issue as Comment #39. ???

#43 | 08:27 on 12 May 2005 | by yosshi

If i change AdminScript name in mt.cfg, this plugin doesn't work any more because AdminScript name is directly written as mt.cgi in AdminLinks.pl.

#44 | 10:33 on 20 August 2006 | by Eric winther

I tried to use MTAdminOnly tag inside a dynamic archive and I got this error message:

Smarty error: [in mt:234 line 46]: syntax error: unrecognized tag 'MTAdminOnly' (Smarty_Compiler.class.php, line 580)

How can I modify the plugin in order to make it compatible with smarty tags?

Post A Comment

Please try and keep your comments on-topic, informative and polite. Flaming and trolling is discouraged and may be deleted. In fact, we reserve the right to edit or delete any post for any reason.


If you don't want your email address to show up, put in a URL as well as an email and the URL will go in the author link.



Your comment:

To make line and paragraph breaks, press return (don't use <p> or <br>). Please don't use inline HTML (it gets removed anyway). URLs are automagically converted to proper links.