I like the idea of enabling any user of my website to suggest edits to a page. Much like the edit system on Stack Exchange but different in that anyone should be able to edit, not just registered users. All edits would go through an approval process.
How could I implement that?
Diff the post content, title and author
As had to do something related some month ago, here’s the easiest and most future proof way (that I could fine) to check if there’s a change made to the content or title or if the author changed:
To explain my scenario briefly: I was fetching posts from a remote location via a remote API. Then I returned the
global $post
, during a single post loop, containing either the original data or the new data. This way I moved around setting all the other post values that I didn’t need to check for changes.Proposing the edit
The main fact one has to keep in mind when searching a place where a copy-edit of the post content could be (temporarily) saved is that the content is a db entry of
longtext
. So the place where one wants to save the suggested edit should meet that requirement. Comments do that.Then there’s the nice fact that they offer an API to place them nicely in a loop, so it integrates smoothly, is highly customizable and can quickly be setup. Last but not least most themes already come with comments integrated so it’s easy to piggyback on the system and hook into nearly any available theme.
I’d suggest to just (either) extend or alter the comment form. Use either the following or add additional fields with a callback hooked to
comment_form_default_fields
.So I’ve added a
hidden
field forcomment_approved
with an value of0
to set it in the queue. Not sure if this will work or if this (core) value is actually comment meta data and needs to get added by usingadd_comment_meta()
during saving. If not, you could use something along the following lines of codeDisplaying the comments on the admin side
Here I’d go with a simple class extension and a custom admin page:
More info can be found on WPEngineer.
Approving edits
You could then add custom actions and process the proposed edits using the first code I showed to check if there was a change and then simply update the post. The comment itself holds a value with a key of
comment_post_ID
, so identifying the edited posts ID is straight forward.Final note
I’d like to see the final plugin as well. Please link it here 🙂
My idea is something simple.
You can make an
Edit Suggestion
link at bottom of posts that has a custom defined template, in that use a textbox (maybe with an editor) that’s linked to a custom taxonomy with the default value ofpost content
.Any changes to
content
will be compared tooriginal post content
after submitting (as Draft) and EnteringCAPTCHA code
with Diff algorithms like PHP inline-diff package or Text-Diff PEAR Package or alternatively using a PHP function according to this for not too-long texts with combination of CSS.Then by saving values in 3 custom Meta Boxes (in that taxonomy back-end add/edit page) that show
and saving
Post ID
maybe with aupdate_option()
function for later use.After reading edited version and acceptance by admin, that post will be replaced the original one as you coded in functions.php.
Well this is rather complicated, a proper answer would take considerable time to write. So this isn’t a real answer just some thoughts..
Using WordPress build in
wp_update_post
via ajax would give you the needed revision history but not the capability needed to approve edits.Creating drafts from edits is not possible by default but has been discussed here, Is there any way to draft a revision of a published page or post? What workarounds have you used?
You can try and use Front-end Editor but you will have no control over edits being published, so try and mash it with with another plugin like Revisionary which does allow for permissions based edits, I have no idea if they will work together.
If they don’t you will have to hack a plugin based on the 2 above plugins or write something from scratch.
My scratch approach would be to have a button that goes to another page that outputs the post content/data using JSON, which is easier to work with when using Ajax and WYSIWYG editors. The save button would post as a draft instead of publish and that way you would have control over the edits (see above WPSE discussion on how to accomplish this, it’s rather daunting).
There are additional complications when doing this such as sanitization, encoding, spam, media handling, custom fields, timestamps, memory, etc. The good news is that wordpress already has a revision system you can plug into and a decent ability to handle multiple editors.
ps. This is a good idea for a plugin.