How to prevent users from editing specific pages?

On our MU install, we have several users that have pages with some scripting or certain bits f code in the WP Editor, and we don't want them to be able to edit those pages, because the visual editor breaks them, and causes us nightmares.

How do we prevent users from editing pages on their own sites, either whole cloth preventing page edits, or specific pages?

I'm sure this will require a plugin for permission management, but we wanted to ask here because we need it to work with Mulituser.

  • Sajid

    Hello Adam,
    Hope you are doing good today

    Those pages are on main site or on their own subsites ?

    Also, how did you (or your users) created these subsites. I am asking because, if its using New Blog Templates plugin then you can chose to lock pages and posts.

    If no then we can still try to do something similar with it. But before that, I would want to take a closer look at your site and see what pages are these and how they are being managed. For this purpose, please enable support staff access so I could take a closer look.

    To grant access go to Dashboard -> WPMU DEV -> Support -> Support Access -> Grant Support Access or see this manual for more details regarding how to enable support staff access.

    Take care and have a nice day

    Best Regards,
    Sajid - WPMU DEV Support

  • Sajid

    Hello Adam,
    Hope you are doing good today

    I come up with following code snippet to lock the specific pages for subsite admins. The following code will lock the home page (ID 2).

    add_filter('user_has_cap', 'wpmudev_lock_specific_pages', 10, 3);
    
        /**
         * Properly filtering out forbidden capabilities for non-super admin users.
         */
        function wpmudev_lock_specific_pages ( $all, $cap, $args ) {
    
            global $post;
    
            if ( ! $args )
                return $all; // Something is wrong here.
    
            // Bail out if we're not asking about a post:
            if ( 'edit_post' != $args[0] )
                return $all;
    
            // Bail out for users who can't publish posts:
            if ( ! isset( $all['publish_posts'] ) or ! $all['publish_posts'] )
                return $all;
    
            if ( is_super_admin() )
                return $all;
    
            if ( ! isset( $post->ID ) )
                return $all;
    
            $blocked = array( 2 );
    
            if ( in_array( $post->ID, $blocked ) ) {
                $post = get_post( $args[2] );
                $post->post_title = $post->post_title . ' ' . __( '[Blocked by Super Admin]', 'blog_templates' );
                $all[ $cap[0] ] = false;
            }
    
            return $all;
    
        }

    Just add the coma separated ID numbers of pages/posts are any custom post type in blocked array. The following line in above code (this means, it can now block pages with ID numbers 2 and 4).
    $blocked = array( 2, 4 );

    You can add it in functions.php file of your child theme but I would recommend you use above code as mu-plugin since you will need this feature on multiple subsites on your netwrok.

    If you go with mu-plugin route then, go to your /wp-content/ folder via FTP, and find the folder called "mu-plugins". If there is no folder with that name, then you'll want to create one. Then, create a file inside that folder, and give the file any name you like and paste the code in there.

    You don't need to activate that plugin, as it will be always be automatically activated. Please be sure to add a PHP start tag at the beginning of the code if you insert this code as a mu-plugin, like this:

    <?php

    Hope that helps! Feel free to post a reply if you need further assistance

    Best Regards,
    Sajid - WPMU DEV Support