wordpresstitlecapability

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.

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


Solution

  • 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
        }
    }
    ?>