templates - How can I add text to a specific 'Edit Page'?

admin2025-06-05  4

I have a specific Page, that I do not wish to be edited. As such, I would like to place a 'Do Not Edit This Page' message within a specific 'Edit Page' file as follows:

Any ideas on how this can be achieved or what I could search for within the WordPress Codex/Developer Resource? Ideally, I would like to be able to create some sort of Template File but if not, I guess the solution would involve the use of Hooks within the functions.php file?!

I have a specific Page, that I do not wish to be edited. As such, I would like to place a 'Do Not Edit This Page' message within a specific 'Edit Page' file as follows:

Any ideas on how this can be achieved or what I could search for within the WordPress Codex/Developer Resource? Ideally, I would like to be able to create some sort of Template File but if not, I guess the solution would involve the use of Hooks within the functions.php file?!

Share Improve this question asked Dec 16, 2018 at 11:29 CraigCraig 3581 gold badge2 silver badges20 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

To do this you would indeed have to include some code in the functions.php of your theme. You can use admin_notices to add a note to this specific page like this:

add_action( 'admin_notices', 'wpse332074_donotedit' );
function wpse332074_donotedit () {
 $screen = get_current_screen();
 if ($screen->post_type == 'page') {
   $variable = $_GET['post'];
   if ($variable == id_of_page_as_integer) { 
     $class = 'notice notice-error';
     $message = __( 'Do not edit this page!', 'your-text-domain' );
     echo '<div class="' . $class . '"><p>' . $message . '</p></div>';
     }
   }
 }

However, this would allow people to ignore the message and still edit the page. If you really don't want the page to be edited, you can remove the edit, quick edit and trash links in the all pages screen by using the page_row_actions filter:

add_filter( 'page_row_actions', 'wpse332074_disable_edit', 10, 2 );
function wpse332074_disable_edit ($actions, $post) {
  if ( $post->ID == id_of_page_as_integer ) {
    unset( $actions['edit'] );
    unset( $actions['inline hide-if-no-js'] );
    unset( $actions['trash'] );
    }
  return $actions;
  }

Beware that any changes to functions.php will be lost when the theme is updated. The proper way to do this is to build a child theme.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749091850a316292.html

最新回复(0)