Feel The Noise
Feel The Noise

Reputation: 654

Make Wordpress Pages's Titles readonly

I'm looking for a WP function that add the Read-only parameter to all Pages's Titles's input, that will make the Page's title unalterable.

Thanks a lot in advance.

Upvotes: 3

Views: 4334

Answers (3)

StudioHeads
StudioHeads

Reputation: 31

This Solution Will disable clicking on the post title and editing it using CSS. CSS targets post type "page" only. It has been tested on Gutenberg visual editor. Users Can still edit title from "Quick Edit". Add this code to your functions.php file.

function disable_title_edit() {
  if(!current_user_can('administrator')){
       if( !current_user_can('administrator')){ ////Only allow Admin
      echo '<style>.post-type-page .edit-post-visual-editor__post-title-wrapper{
       pointer-events: none;
    }</style>'; } }
    }
add_action('admin_head', 'disable_title_edit', 100);

Upvotes: 0

ewroman
ewroman

Reputation: 665

No need to make a seperate js file. Adding this to your function.php will do the same that Matthew showed.

function admin_footer_hook(){
        ?>
    <script type="text/javascript">
        if(jQuery('#post_type').val() === 'post'){
    jQuery('#title').prop('disabled', true);
    }
    </script>
<?php
}
add_action( 'admin_footer-post.php', 'admin_footer_hook' );

Upvotes: 3

Matthew Blancarte
Matthew Blancarte

Reputation: 8301

This can be accomplished with some simple JavaScript/jQuery. Create a file called admin_title_disable.js, and queue it up within functions.php. For example:

functions.php:

wp_register_script('admin_title_disable', '/path/to/admin_title_disable.js');
function disableAdminTitle () {
  wp_enqueue_script('admin_title_disable');
}
add_action('admin_enqueue_scripts', 'disableAdminTitle');

Now, in your js file:

jQuery(document).ready(function ($) {
  $('#title').attr('disabled','disabled');
});

This will set both post and page title input fields with a disabled attribute. Hope this helps!

If you want to restrict this script to a particular admin page, wrap the add_action hook in a conditional that compares $_GET['page']. You can also take advantage of the $hook parameter that is available when using admin_enqueue_scripts to check for the page. See here.

Update::

WordPress makes it a little tricky to tell between post and page edit screens, but there is a hidden input that you can take advantage of. :) Here's an updated version of the jQuery that will only run on page edit screens:

jQuery(document).ready(function ($) {
  //find the hidden post type input, and grab the value
  if($('#post_type').val() === 'page'){
    $('#title').attr('disabled','disabled');
  }
 });

Upvotes: 5

Related Questions