How to disable page’s title in wp-admin from being edited?

I have a wp-network installed with users that can create pages in each site.

Each of those pages get a place in the primary menu, and only one user have permission to create all this menu.

Read More

I want to create a user only to be able to edit the content of the pages, but not the title.

How can I disable the title of the page to be edited from the admin menu for a specific user, or (far better) for a capability?

I thought only a possibility, that’s editing admin css to hide the title textbox, but I have two problems:

  1. I don’t like to css-hide things.
    1. I don’t know where is the admin css.
    2. I know php, but don’t know how to add a css hide to an element for a capability.

How do I want it to be

Related posts

Leave a Reply

2 comments

  1. You should definitely use CSS to hide the div#titlediv. You’ll want the title to show in the markup so the form submission, validation, etc continues to operate smoothly.

    Some elements you’ll need to know to implement this solution:

    1. current_user_can() is a boolean function that tests if the current logged in user has a capability or role.
    2. You can add style in line via the admin_head action, or using wp_enqueue_style if you’d like to store it in a separate CSS file.

    Here is a code snippet that will do the job, place it where you find fit, functions.php in your theme works. I’d put it inside a network activated plugin if you’re using different themes in your network:

    <?php
    
    add_action('admin_head', 'maybe_modify_admin_css');
    
    function maybe_modify_admin_css() {
    
        if (current_user_can('specific_capability')) {
            ?>
            <style>
                div#titlediv {
                    display: none;
                }
            </style>
            <?php
        }
    }
    ?>
    
  2. I resolved the problem, just if someone comes here using a search engine, I post the solution.

    Doing some research, I found the part of the code where the title textbox gets inserted, and I found a function to know if a user has a certain capability.

    The file where the title textbox gets added is /wp-admin/edit-form-advanced.php. This is the line before the textbox

    if ( post_type_supports($post_type, 'title') )
    

    I changed it to this

    if ( post_type_supports($post_type, 'title') and current_user_can('edit_title') )
    

    That way, the textbox is only added when the user has the capability called “edit_title”

    When this IF block ends few lines after, I added:

    else echo "<h2>".esc_attr( htmlspecialchars( $post->post_title ) )."</h2>";
    

    To see the page title but not to edit it, when the user hasn’t got “edit_title” capability.

    Then I had already installed a plugin to edit user capabilities and roles, wich help me to create a new capability (edit_title) and assign it to the role I want.