How to disable page's title in wp-admin from being edited?
Asked Answered
F

2

7

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

Fumy answered 4/4, 2012 at 16:45 Comment(0)
P
10

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
    }
}
?>
Predigestion answered 4/4, 2012 at 19:4 Comment(1)
This may work to hide the Page>New Page menú in the admin menu bar, if there is a CSS selector to reference that entry in the menu. Do you know if that selector exist?Fumy
F
-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.

Fumy answered 5/4, 2012 at 22:46 Comment(4)
You should never edit the core files of Wordpress. Future updates will remove this hack.Tenter
You are right, I see this answer 2 years later and I think is just stupid. But I see how much I learnt. StackOverflow is awesome.Fumy
Now 7 years later, feels even dumber. Feel free to downvote! And please keep learning how (not) to do things :)Fumy
9.5 years later... I find this solution breaking wp upgrades, but actually solving the original problem. If wordpress has no "hooks" for doing this and depending on the urgency/importance of doing this, I might approve a change like this, although I would lean to the CSS solution.Fumy

© 2022 - 2024 — McMap. All rights reserved.