Avoid Users Remove a Given Page

Hi,

I have a multisite install and I want to avoid the users remove a given page in their sites. I have the ID of the page.

Is there some plugin for it?

Thanks.

  • Vaughan
    • Support/SLS MockingJay

    Hi @andreza,

    I haven't tested this, but can you try the following

    create a file called functions.php

    Then add the following code.

    <?php
    function wpmu_prevent_post_delete() {
        $post_id = 1;
    
        $del_post = get_post($page_id);
    
        if('trash' == strtolower($del_post->post_status)) {
            $del_post->post_status = 'publish';
    
            wp_update_post($del_post);
    }
    add_action( 'after_setup_theme', 'wpmu_prevent_post_delete' );
    
    function wpmu_prevent_post_delete_hide($actions, $post) {
        $post_id = 1;
    
        if(isset( $_REQUEST['post_status'] ) && 'trash' == $_REQUEST['post_status'] && $_REQUEST['post_ID'] == $post_id)
            unset( $actions['delete'] );
    
            return $actions;
    }
    add_filter( 'page_row_actions', 'wpmu_prevent_post_delete_hide', 10, 2 );

    Change $post_id = 1; in both functions to the page ID of the page.

    Then upload the functions.php to wp-content/mu-plugins (if mu-plugins does not exist, create it)

    I haven't tested, so it might not work. but hopefully it should.

  • Vaughan
    • Support/SLS MockingJay

    Hi,

    Yes there's no need for a closing php tag on pure php files, you only need closing tags if you are mixing php with html etc.

    Does it only remove the delete option for that one page though or all pages? if it's just removing it on that page only, that's good we can just adjust the code slightly.

    <?php
    function wpmu_prevent_post_delete() {
        $post_id = 1;
    
        $del_post = get_post($page_id);
    
        if('trash' == strtolower($del_post->post_status) && !is_super_admin()) {
            $del_post->post_status = 'publish';
    
            wp_update_post($del_post);
    }
    add_action( 'after_setup_theme', 'wpmu_prevent_post_delete' );
    
    function wpmu_prevent_post_delete_hide($actions, $post) {
        $post_id = 1;
    
        if(isset( $_REQUEST['post_status'] ) && 'trash' == $_REQUEST['post_status'] && $_REQUEST['post_ID'] == $post_id && !is_super_admin())
            unset( $actions['delete'] );
    
            return $actions;
    }
    add_filter( 'page_row_actions', 'wpmu_prevent_post_delete_hide', 10, 2 );

    The above should now allow super-admins to delete them.

    Hope this helps

  • Jack Kitterhing
    • Code Norris

    Hi there @Andreza,

    Hope you're well today and thanks for your question.

    Are you using New Blog templates to create these pages that shouldn't be deleted? If you are, that has a built in option to lock posts from editing/deletion.

    Would that work for what you need? :slight_smile:

    Thank you!

    Kind Regards
    Jack.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.