skip to Main Content

How to disable page title editing for users except admin ? they should be able to see page title, but should not be able to edit page title in backend.

function disableAdminTitle () {
    $user = wp_get_current_user();
    if(!in_array( 'administrator', (array) $user->roles )){
        wp_enqueue_script('admin_title_disable');
    }
}
add_action('admin_enqueue_scripts', 'disableAdminTitle');

function admin_footer_hook(){
        ?>
    <script type="text/javascript">
        jQuery(document).ready(function ($) {
  $('#title').attr('disabled','disabled');
});
    </script>
<?php
}
add_action( 'admin_footer-post.php', 'admin_footer_hook' );

2

Answers


  1. You can use the solution mentioned in Make WordPress Pages's Titles readonly along with user_can.

    Login or Signup to reply.
  2. You can try this

    wp_register_script('admin_title_disable', '/path/to/admin_title_disable.js');
    function disableAdminTitle () {
        $user = wp_get_current_user();
        if(!in_array( 'administrator', (array) $user->roles )){
            wp_enqueue_script('admin_title_disable');
        }
    }
    add_action('admin_enqueue_scripts', 'disableAdminTitle');
    

    Js Code

    jQuery(document).ready(function ($) {
      $('#title').attr('disabled','disabled');
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search