0

Hoping someone can offer some code snippet I can use to block other Admins or Editors from editing MY posts.....I use only the Text Editor, but when other users go into my Posts using the Visual Editor, it messes up some of the HTML and code that I've added and I have to spend a lot of time in the Text Editor fixing it.

I don't care if they edit other users' Posts, I just don't want them messing with mine because it causes me too much work. And yes, I've tried - and failed - to train them to simply stay out....the sometimes just don't even look to see who the author is before the go in to edit a Post and don't realize they've screwed it up until I notice it.

4
  • what are you doing that requires lots of custom HTML?
    – Tom J Nowell
    Commented Sep 28, 2021 at 23:28
  • Hi Tom, it really isn't anything out of the ordinary, but if I create specifically styled lists (either OL or UL) in the Text Editor, then if someone views it in the Visual Editor, WP both wraps and inserts additional OL or UL tags with a declared style of "list-item-type:none" that overrides my styles....I'm not sure if it's caused by core WP or the Classic Editor plugin, but it's been happening with the last 3 versions.....it happens also if I insert an inline ad between LI items, then WP decides to close the UL, open a new one with different styles, etc. It totally whacks out my lists.....
    – Trisha
    Commented Sep 29, 2021 at 22:50
  • that is highly unusual and not normal WordPress behaviour. I would also strongly recommend using either the block editor, or the classic editor, but not both. Opening a post written in the block editor using the classic editor can mangle and break blocks. I think you should ask how to fix your problem instead, you've fallen into the X Y trap of asking how to implement a solution to your problem, rather than how to fix the original problem
    – Tom J Nowell
    Commented Sep 30, 2021 at 10:06
  • Thanks Tom, we don't use the Block Editor at all. On this site, all users are long-time WP users who unanimously dislike the block editor, despite my encouragement to learn it. SO we use the Classic Editor plugin to maintain the WP experience they know and like. No one uses the Block Editor, but they do use the Visual Editor (vs Text Editor) Tab of the Classic Editor experience. That is what causes the problem.
    – Trisha
    Commented Sep 30, 2021 at 18:37

1 Answer 1

0

You can visit - How to assign specific users the capability to edit specific pages / posts / custom post types

OR

First of all: grant full access to projects post type (Example).

At the user profile add allowed posts' id.

Then use the below filter to restrict access if post id isn't allowed.

function allow_user_to_edit_cpt_filter( $capauser, $capask, $param){

    global $wpdb;

    $allowed_posts_id_for_current_user = array( '29', '30' ); // you need to get these ids yourself
    $post = get_post( $param[2] );

    // If current post isn't allowed then delete edit and delete capabilities
    if( !in_array( $post->ID, $allowed_post_type_ids ) ){
        if( ( $param[0] == "edit_projects") || ( $param[0] == "delete_projects" ) ) { // Change to yours capabilities
            foreach( (array) $capask as $capasuppr) {
               if ( array_key_exists($capasuppr, $capauser) ) {
                  $capauser[$capasuppr] = 0;
               }
            }
        }
    }

    return $capauser;
}
add_filter('user_has_cap', 'allow_user_to_edit_cpt_filter', 100, 3 );

Copy answer from How to assign specific users the capability to edit specific pages / posts / custom post types

4
  • Thank you Maulik, this looks very useful and I will keep it for the future, but in THIS case, I'm not using a custom post type, this is just for normal Posts.....my Editors and fellow Admins DO need to be able to edit other users (Contributors and Authors) Posts, just not my own Posts. I will clarify my question so anyone else who might answer understands I'm not referring to CPTs, just normal Posts. Cheers!
    – Trisha
    Commented Sep 29, 2021 at 21:23
  • @Trisha Yes, you can replace with projects to posts and try it, I think it will work. Commented Sep 30, 2021 at 4:51
  • custom post types and posts are the same thing, the custom part is just that your plugin/theme added it, not WordPress itself. E.g. Posts are a CPT off type post, Pages are page, etc, WP handles/stores it all the same way
    – Tom J Nowell
    Commented Sep 30, 2021 at 10:09
  • @MaulikPaddharia, I appreciate your help, but I must not be explaining clearly. I don't want to have to continually update a list of Post IDs (for individual posts authored by me) in order to keep others out of them, I am looking for a way to block all other users from editing ALL of my Posts without my having to go update code every time I create a new Post. They need to be able to edit other users' Posts, just not mine. It's possible that this just isn't something that can be done.
    – Trisha
    Commented Sep 30, 2021 at 18:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.