How to lock a post or page

How to lock a post or page from editing or deleting but can be viewed on site? I am not able to lock a post, I read several articles but can’t find my answer.

Any help?

2 Answers
2

You can use the 'wp_insert_post_data' filter to set the data being updated to the ones post has right now:

add_filter('wp_insert_post_data', 'prevent_post_edit', 99, 2);

prevent_post_edit ($data, $postarr) {
  if ( ! isset($postarr['ID']) || empty($postarr['ID']) ) return $data;
  if ( current_user_can('edit_files') ) return $data; // admin can edit posts
  // prevent the update only for post and pages, change this according to tour needs
  $prevent_types = array('post', 'page');
  if ( ! in_array($data['post_type'], $prevent_types) ) return data;
  // get the post how is before the update
  $old = get_post($postarr[ID]);
  return get_object_vars($old);
}

Leave a Comment