1

How can I mark a page as "no deletable" in the wordpress database? I've already seen this solution, but it requires a plugin. Is there really no way beyond a plugin?

1

You can make use of the filters supplied via the delete/trash functions of WordPress API: wp_trash_post and wp_delete_post:

function prevent_post_trash_delete($bool, $post){
    $posts_to_keep = [30];

    if ( isset( $post->ID ) && in_array($post->ID, $posts_to_keep) ) {
        return false;
    }

    return $bool;
}

add_filter('pre_delete_post', 'prevent_post_trash_delete', 10, 2);
add_filter('pre_trash_post', 'prevent_post_trash_delete', 10, 2);

You can pass an array of IDs (so be it post, page, or any other post type) through the $posts_to_keep variable.

That way they will throw a notice when a user tries to delete them via the dashboard. You can't prevent plugins using direct SQL queries from deleting these pages.

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.