WordPress “Edit This” Links via Ajax

Working on a WordPress customisation recently, I added an “Edit This” link which only logged-in people can see. To get caching right, the server always outputs the same thing – an invisible link – and only in the browser does the decision get made to show the link or not. This exemplifies the pattern I only ever identified as “Ajax as a Remedy for the Cacheability-Personalization Dilemma”, but which I will now call “Browser-Side Personalisation”.

In this case, the personalised content is not secret, so it’s fine to output it in the HTML, and simply make it invisible by default. And it’s not mission-critical either, so the app degrades nicely if Javascript isn’t present – it simply won’t be displayed and the user will have to go into the “Manage Posts” area and look up the post from there.

To output “Edit This” from the server, within a WordPress loop:

javascript

  1. <div class="editPostLink"><? edit_post_link() ?></div>

Links of this nature are rendered invisible within layout.css:

  1. .editPostLink { display: none; }

… but we switch them on if the user is logged in:

javascript

  1. var loggedIn = /wordpress_logged_in/.test(document.cookie);
  2.   if (loggedIn) $(".editPostLink").show(); // it's easy with JQuery

2 thoughts on WordPress “Edit This” Links via Ajax

  1. Pingback: Latest Wordpress News | 26nov

  2. Hmm, I found this looking for a similar but more extensive WP question – how can I open the link itself in an AJAX window, so that I can do the edit, save the changes and close out the Ajax window?

    Because I can open any Admin link in a new tab in my browser, I thought this would be pretty easy and even more convenient, but I don’t see anyone doing it. Can you tell me if it’s not doable for some reason, and if it is doable, possibly give me a hint as to how?

Leave a Reply