0

My question is pretty much in the title.

I have 2 or more administrators, but I only want the admin with X username to edit the title of the posts while others can only view them.

Feel free suggesting solutions that involve scripting or the use of plugins. Thanks!

Note: This is only for the backend/dashboard.

  • Sorry? No plugins. No code -- aka "scripting". Exactly what kind of solution do you expect? – s_ha_dum Dec 12 '13 at 5:03
  • Hmm, let me re-phrase. I meant "feel free". Edited! – user2772219 Dec 12 '13 at 5:17
  • You are only worried about the title? Nothing else? Not the permalink as well? – s_ha_dum Dec 12 '13 at 5:19
  • The title and/or the permalink. I think I just need an example on how it's done so I can apply it to other fields. – user2772219 Dec 12 '13 at 5:24
1

Once you say "edit" I think you are no worried about new posts, just edit existent post.

So, my idea is remove the support fot title when the admin is no the one allowed.

I'll write a separate function that retturn true if the current user is the wanted admin, in this way it can be used in different part of the site.

function is_preferred_admin() {
  $u = wp_get_current_user();
  // change 'admin' with the wanted login here
  return user_can($u, 'manage_options') && $u->user_login === 'admin';
}

add_action('load-post.php', 'remove_title_support');

function remove_title_support() {
  if ( is_preferred_admin() ) return;
  $scr = get_current_screen();
  remove_post_type_support( $scr->post_type, 'title' );
}

Consider that this remove UI, but is not a real capability preventing. To do that, filter wp_insert_post_data and prevent title changing:

add_filter('wp_insert_post_data', 'prevent_edit_title', 999, 2);

function prevent_edit_title( $new, $oldarr ) {
  if( is_preferred_admin() ) return;
  if ( ! isset($oldarr['ID']) || empty($oldarr['ID'])  ) return;
  $old = get_post($oldarr['ID']);
  $new['post_title'] = $old->post_title; // no change allowed
  $new['post_name'] = $old->post_name; // no change allowed
  return $new;
}
0

This is a significant extension of @G.M's answer. Code it commented to show the changes.

The difference is that this will print a read only title if the admin name does not match.

// use 'edit_form_after_title' to create a place for your read only title box
function add_before_editor($post) {
  global $post;
  do_meta_boxes('post', 'pre_editor', $post);
}
add_action('edit_form_after_title','add_before_editor');

// the read only box content
function read_only_title_wpse_126209($post) { 
  global $post_type_object; ?>
  <div id="titlediv">
  <div id="titlewrap">
      <div class="screen-reader-text" id="title-prompt-text" for="title">Title (Read Only)</div>
      <?php echo esc_attr( htmlspecialchars( $post->post_title ) ); ?>
  </div>
  <div class="inside"><?php
    $sample_permalink_html = $post_type_object->public ? get_sample_permalink_html($post->ID) : '';
    $shortlink = wp_get_shortlink($post->ID, 'post');
    if ( !empty($shortlink) ) {
        $sample_permalink_html .= '<div id="read-only-shortlink">' . esc_attr($shortlink) . '</div>';
    }
    if ( $post_type_object->public && ! ( 'pending' == get_post_status( $post ) && !current_user_can( $post_type_object->cap->publish_posts ) ) ) {
        $has_sample_permalink = $sample_permalink_html && 'auto-draft' != $post->post_status; 
    } ?>
  </div>
  </div><?php
}

// will add the read only box when called
function read_only_title_box() {
    add_meta_box(
        'read_only_title', // id, used as the html id att
        __( 'Title (Read Only)' ), // meta box title
        'read_only_title_wpse_126209', // callback function, spits out the content
        'post', // post type or page. This adds to posts only
        'pre_editor', // context, where on the screen
        'low' // priority, where should this go in the context
    );
}

// G-M's function 
function is_preferred_admin() {
  $u = wp_get_current_user();
  // change 'admin' with the wanted login here
  return user_can($u, 'manage_options') && $u->user_login === 'admin';
}
add_action('load-post.php', 'remove_title_support');

// G-M's function 
function remove_title_support() {
  if ( is_preferred_admin() ) return;
  $scr = get_current_screen();
  remove_post_type_support( $scr->post_type, 'title' );

  // my addition to the function to load the read only box
  // add meta box to print current title
  add_action( 'add_meta_boxes', 'read_only_title_box' );
}

Some code taken from edit-form-advanced.php and from here: https://wordpress.stackexchange.com/a/100495/21376

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.