delete_plugins( string[] $plugins, string $deprecated = '' )

Remove directory and files of a plugin for a list of plugins.


Description Description


Parameters Parameters

$plugins

(string[]) (Required) List of plugin paths to delete, relative to the plugins directory.

$deprecated

(string) (Optional) Not used.

Default value: ''


Top ↑

Return Return

(bool|null|WP_Error) True on success, false if $plugins is empty, WP_Error on failure. null if filesystem credentials are required to proceed.


Top ↑

Source Source

File: wp-admin/includes/plugin.php

852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
function delete_plugins( $plugins, $deprecated = '' ) {
    global $wp_filesystem;
 
    if ( empty( $plugins ) ) {
        return false;
    }
 
    $checked = array();
    foreach ( $plugins as $plugin ) {
        $checked[] = 'checked[]=' . $plugin;
    }
 
    $url = wp_nonce_url( 'plugins.php?action=delete-selected&verify-delete=1&' . implode( '&', $checked ), 'bulk-plugins' );
 
    ob_start();
    $credentials = request_filesystem_credentials( $url );
    $data        = ob_get_clean();
 
    if ( false === $credentials ) {
        if ( ! empty( $data ) ) {
            include_once( ABSPATH . 'wp-admin/admin-header.php' );
            echo $data;
            include( ABSPATH . 'wp-admin/admin-footer.php' );
            exit;
        }
        return;
    }
 
    if ( ! WP_Filesystem( $credentials ) ) {
        ob_start();
        request_filesystem_credentials( $url, '', true ); // Failed to connect, Error and request again.
        $data = ob_get_clean();
 
        if ( ! empty( $data ) ) {
            include_once( ABSPATH . 'wp-admin/admin-header.php' );
            echo $data;
            include( ABSPATH . 'wp-admin/admin-footer.php' );
            exit;
        }
        return;
    }
 
    if ( ! is_object( $wp_filesystem ) ) {
        return new WP_Error( 'fs_unavailable', __( 'Could not access filesystem.' ) );
    }
 
    if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
        return new WP_Error( 'fs_error', __( 'Filesystem error.' ), $wp_filesystem->errors );
    }
 
    // Get the base plugin folder.
    $plugins_dir = $wp_filesystem->wp_plugins_dir();
    if ( empty( $plugins_dir ) ) {
        return new WP_Error( 'fs_no_plugins_dir', __( 'Unable to locate WordPress plugin directory.' ) );
    }
 
    $plugins_dir = trailingslashit( $plugins_dir );
 
    $plugin_translations = wp_get_installed_translations( 'plugins' );
 
    $errors = array();
 
    foreach ( $plugins as $plugin_file ) {
        // Run Uninstall hook.
        if ( is_uninstallable_plugin( $plugin_file ) ) {
            uninstall_plugin( $plugin_file );
        }
 
        /**
         * Fires immediately before a plugin deletion attempt.
         *
         * @since 4.4.0
         *
         * @param string $plugin_file Path to the plugin file relative to the plugins directory.
         */
        do_action( 'delete_plugin', $plugin_file );
 
        $this_plugin_dir = trailingslashit( dirname( $plugins_dir . $plugin_file ) );
 
        // If plugin is in its own directory, recursively delete the directory.
        if ( strpos( $plugin_file, '/' ) && $this_plugin_dir != $plugins_dir ) { //base check on if plugin includes directory separator AND that it's not the root plugin folder
            $deleted = $wp_filesystem->delete( $this_plugin_dir, true );
        } else {
            $deleted = $wp_filesystem->delete( $plugins_dir . $plugin_file );
        }
 
        /**
         * Fires immediately after a plugin deletion attempt.
         *
         * @since 4.4.0
         *
         * @param string $plugin_file Path to the plugin file relative to the plugins directory.
         * @param bool   $deleted     Whether the plugin deletion was successful.
         */
        do_action( 'deleted_plugin', $plugin_file, $deleted );
 
        if ( ! $deleted ) {
            $errors[] = $plugin_file;
            continue;
        }
 
        // Remove language files, silently.
        $plugin_slug = dirname( $plugin_file );
        if ( '.' !== $plugin_slug && ! empty( $plugin_translations[ $plugin_slug ] ) ) {
            $translations = $plugin_translations[ $plugin_slug ];
 
            foreach ( $translations as $translation => $data ) {
                $wp_filesystem->delete( WP_LANG_DIR . '/plugins/' . $plugin_slug . '-' . $translation . '.po' );
                $wp_filesystem->delete( WP_LANG_DIR . '/plugins/' . $plugin_slug . '-' . $translation . '.mo' );
 
                $json_translation_files = glob( WP_LANG_DIR . '/plugins/' . $plugin_slug . '-' . $translation . '-*.json' );
                if ( $json_translation_files ) {
                    array_map( array( $wp_filesystem, 'delete' ), $json_translation_files );
                }
            }
        }
    }
 
    // Remove deleted plugins from the plugin updates list.
    if ( $current = get_site_transient( 'update_plugins' ) ) {
        // Don't remove the plugins that weren't deleted.
        $deleted = array_diff( $plugins, $errors );
 
        foreach ( $deleted as $plugin_file ) {
            unset( $current->response[ $plugin_file ] );
        }
 
        set_site_transient( 'update_plugins', $current );
    }
 
    if ( ! empty( $errors ) ) {
        if ( 1 === count( $errors ) ) {
            /* translators: %s: plugin filename */
            $message = __( 'Could not fully remove the plugin %s.' );
        } else {
            /* translators: %s: comma-separated list of plugin filenames */
            $message = __( 'Could not fully remove the plugins %s.' );
        }
 
        return new WP_Error( 'could_not_remove_plugin', sprintf( $message, implode( ', ', $errors ) ) );
    }
 
    return true;
}

Top ↑

Changelog Changelog

Changelog
Version Description
2.6.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.