Как запретить удаление отдельных постов (записей)
Часто бывает что на сайте есть важные системные страницы, удалять которые нельзя, потому что какой-то функционал сайта перестанет работать.
Для таких важных страниц хорошо полностью отключить возможность удаления. Также эти страницы можно пометить в таблице что они системные:
Отключим возможность удалять указанные страницы
Тип поста и слаг (post_name) страниц которые нельзя будет удалить, указывается в списке $SPECIAL:
<?php
if( ! is_admin() ){
return;
}
WPKama_Special_Pages::init();
final class WPKama_Special_Pages {
public static array $SPECIAL = [
'page' => [
'describe_function',
'profile',
'pay',
'search',
'test',
]
];
public static function init(){
add_filter( 'display_post_states', [ __CLASS__, 'mark_in_post_list_page' ], 10, 2 );
add_action( 'pre_trash_post', [ __CLASS__, 'restrict_post_deletion' ], 10, 2 );
add_filter( 'pre_delete_post', [ __CLASS__, 'restrict_post_deletion' ], 10, 2 );
add_filter( 'page_row_actions', [ __CLASS__, 'remove_row_action' ], 10, 2 );
}
public static function mark_in_post_list_page( array $post_states, $post ): array {
if( self::is_post_in_list( $post ) ){
$post_states[] = '<small>SYSTEM</small>';
}
return $post_states;
}
public static function remove_row_action( array $actions, $post ): array {
if( self::is_post_in_list( $post ) ){
unset( $actions['delete'] );
}
return $actions;
}
public static function restrict_post_deletion( $delete, $post ){
if( self::is_post_in_list( $post ) ){
/** @noinspection ForgottenDebugOutputInspection */
wp_die( "`$post->post_name` page can not be deleted - it is special page." );
}
return $delete;
}
private static function is_post_in_list( $post ): bool {
if( empty( $post->post_type ) ){
return false;
}
$list = self::$SPECIAL[ $post->post_type ] ?? [];
return in_array( $post->post_name, $list, true );
}
}
Теперь при попытке удалить системный пост, будет вылетать такое сообщение:
