1

Changing the title of the article can be very dangerous for search

I'm looking for a way to disable the post title field after publishing for authors and editors

Can anyone help me in this regard?

1
  • try css rule display:none for the title container ID titlediv along with a php condition when in post edit view if( isset($_GET['post']) && isset($_GET['action']) && $_GET['action'] == 'edit' && !current_user_can('manage_options') ) { 'css rule here' }
    – Nadal
    Sep 22, 2023 at 21:17

2 Answers 2

2

Use the action hook edit_form_after_title

The display:none rule might cause some JS issue so it might be best to use visibility:hidden so the element is still accessible to whatever scripts in the DOM.

This condition will hide the form fields and show the title and URL statically.

add_action('edit_form_after_title', function($post) {
    if( !empty($post->post_title) && !current_user_can('manage_options') ) {
        echo '<style>#titlediv {visibility: hidden !important; height: 1px !important;}</style>';
        echo '<h2>'.$post->post_title.'</h2>';
        echo '<a href="'.get_site_url().'/'.$post->post_name.'">'.get_site_url().'/'.$post->post_name.'</a>';
    }
});

Note that post titles can also be edited within table list view with quick edit so you'll need to hook into that location also and do the same.

Since your concern is search engine 404 related, you might wanna consider disabling a lot more which could change the post's URL or accessibility:

  • move to trash
  • category change
  • post privacy
  • publish state
  • publish date change

Also if yo use a third party visual composer like Elementor or similar, those may still allow title change and not use the ID used by WP core system.

Post object returned by var $post

new post

WP_Post Object
(
    [ID] => 369
    [post_author] => 1
    [post_date] => 2023-09-22 18:19:21
    [post_date_gmt] => 0000-00-00 00:00:00
    [post_content] => 
    [post_title] => 
    [post_excerpt] => 
    [post_status] => auto-draft
    [comment_status] => closed
    [ping_status] => closed
    [post_password] => 
    [post_name] => 
    [to_ping] => 
    [pinged] => 
    [post_modified] => 2023-09-22 18:19:21
    [post_modified_gmt] => 0000-00-00 00:00:00
    [post_content_filtered] => 
    [post_parent] => 0
    [guid] => https://example.net/?page_id=369
    [menu_order] => 0
    [post_type] => page
    [post_mime_type] => 
    [comment_count] => 0
    [filter] => raw
)

saved post edit mode

WP_Post Object
(
    [ID] => 274
    [post_author] => 1
    [post_date] => 2023-09-11 03:55:29
    [post_date_gmt] => 2023-09-11 07:55:29
    [post_content] => there is always stuff to write
    [post_title] => The Page
    [post_excerpt] => 
    [post_status] => publish
    [comment_status] => closed
    [ping_status] => closed
    [post_password] => 
    [post_name] => the-page
    [to_ping] => 
    [pinged] => 
    [post_modified] => 2023-09-22 18:08:33
    [post_modified_gmt] => 2023-09-22 22:08:33
    [post_content_filtered] => 
    [post_parent] => 0
    [guid] => https://example.net/?page_id=274
    [menu_order] => 0
    [post_type] => page
    [post_mime_type] => 
    [comment_count] => 0
    [filter] => edit
)
1
1

thanks nadal

i change to this code

add_action('edit_form_after_title', function($post) {
    if( !empty($post->post_title) && $post->post_status == 'publish' ) {
        echo '<style>#titlediv {visibility: hidden !important; height: 1px !important;}</style>';
        echo '<div id="titlewrap"><input type="text" name="post_title" size="30" value="'.$post->post_title.'" id="title-2" style="padding:3px 8px;font-size:1.7em;line-height:100%;height:1.7em;width:100%;outline:0;margin:0 0 3px;background-color:#fff;" disabled></div>';
        echo '<div id="edit-slug-box" class="hide-if-no-js"><strong>slug : </strong><a href="'.get_site_url().'/'.urldecode($post->post_name).'">'.get_site_url().'/'.urldecode($post->post_name).'</a></div>';
    }
});
1
  • you don't need to hide the container if you still want the text field visible but disabled. Better off using javascript to add the disabled attribute. <script>jQuery(function($) { $("#title,button.edit-slug").attr("disabled","disabled") });</script>
    – Nadal
    Sep 24, 2023 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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