How To Stop Accidental Editing Of Published WordPress Posts

How To Stop Accidental Editing Of Published WordPress Posts

Why is it that WordPress happily allows you edit and update published posts? Surely, once content is published, updates should have to go through the same, albeit often rudimentary, process if only to make you pause for thought?

You want to keep your publishing process simple but this is an accident just waiting to happen. Revisions can help recover from an ill-advised or unintentional update but, as we all know, prevention is always better than a cure.

Let me show you how preventing the editing of published posts is as simple as it is beneficial.

Photo of a minor car accident in Japan
Allowing editing of published posts is an accident waiting to happen

WordPress’ simplicity is as much a benefit as it is a drawback. The ease of publishing often means that we don’t take as much care as we should when we publish. After all, if something’s wrong we can easily correct it, can’t we?

It’s also far too easy to change something without thinking. If you are at least an author with publishing rights then it’s often just click-edit-click and you are done.

Published Posts Should Not Be Editable

Published posts should not be editable: updates should go through some sort of process, even if it’s just back to draft and then publish again. It puts in that mental break; a pause to allow the author to stop and think about what they are doing.

And, of course, if the post is changed back to draft then it’s not going to be available on the site. Surely, a good thing if it is still being corrected?

But we don’t want to create bottlenecks such as restricting who can publish. We want to maintain the flexibility but put some process in place for updates.

Removing Edit Links For Published Posts

Image composed of 3 screen grabs of the various edit links for a WordPress post
3 edit link locations

To stop accidental editing of published posts we need to remove the edit links. These appear in three spots:

  1. Admin bar – if you are logged in and can edit the post you get an Edit Post link
  2. Admin interface – post listing, in the actions that appear when you rollover a post title
  3. Public interface – again, if you are logged and authorised to edit the post you’ll often see an Edit link near the post metadata

We’ll accomplish this by building a simple plugin that hooks into two filters and one action. The plugin is available for download if you don’t want to build it yourself.

FREE EBOOK
Your step-by-step roadmap to a profitable web dev business. From landing more clients to scaling like crazy.

By downloading this ebook I consent to occasionally receive emails from WPMU DEV.
We keep your email 100% private and do not spam.

FREE EBOOK
Plan, build, and launch your next WP site without a hitch. Our checklist makes the process easy and repeatable.

By downloading this ebook I consent to occasionally receive emails from WPMU DEV.
We keep your email 100% private and do not spam.

Here’s the code:


<?php
/*
Plugin Name: Prevent Publish Edit
Plugin URI: https://wpmudev.com/blog/
Description: Prevents the edit links appearing for published posts, forcing posts to be moved back to draft status before editing
Author: Chris Knowles
Version: 1.0
Author URI: https://wpmudev.com/blog
*/

/*
* Removes the edit action from the post row actions if the post is published
*/
function pep_post_row_actions( $actions , $post ) {

if ( $post->post_status == 'publish' ) unset( $actions['edit'] );
return $actions;
}

/*
* Blanks the Edit link shown in the public interface if the post is published
*/
function pep_edit_post_link( $link ) {

global $post;
if ( $post->post_status == 'publish' ) $link = '';
return $link;

}

/*
* Removes the Edit Post option from the admin bar if a single post is being shown and
* the post is published
*/
function pep_before_admin_bar_render() {

global $wp_admin_bar, $post;
if ( is_single() && $post->post_status == 'publish' ) $wp_admin_bar->remove_menu('edit');

}

// set up the filters
add_filter( 'post_row_actions' , 'pep_post_row_actions' , 1 , 2 );
add_filter( 'edit_post_link' , 'pep_edit_post_link' , 1 );

// set up the action
add_action( 'wp_before_admin_bar_render' , 'pep_before_admin_bar_render' );

?>

That’s it! I told you it was simple.

It’s really just three simple if statements that get executed in the three situations I outlined earlier. Each time we check to see if the relevant post is published and if it is then we remove the edit link.

Governance and Process Get Little Attention

Governance and process often get little consideration in our rush to publish. Adding this simple step of not allowing published posts to be edited without having to go back to draft status will provide at least some pause for thought.

What techniques do you use to improve the quality of your posts and your publishing process? 

Download: prevent-publish-edit-plugin (zipped)

Tags: