Created
May 26, 2026 13:13
-
-
Save xlplugins/ce0c43aa591c641a5352ead13b23aa58 to your computer and use it in GitHub Desktop.
download plugin snippet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Plugin Name: WP ZIP Downloader | |
| * Description: Download installed plugins and themes as ZIP files without ZipArchive. | |
| * Version: 1.0.0 | |
| */ | |
| if ( ! defined( 'ABSPATH' ) ) { | |
| exit; | |
| } | |
| function wzdl_download_zip( $source, $zip_name, $remove_path ) { | |
| require_once ABSPATH . 'wp-admin/includes/class-pclzip.php'; | |
| if ( ! file_exists( $source ) ) { | |
| wp_die( 'Source not found.' ); | |
| } | |
| $upload_dir = wp_upload_dir(); | |
| $zip_path = trailingslashit( $upload_dir['basedir'] ) . sanitize_file_name( $zip_name ); | |
| $zip = new PclZip( $zip_path ); | |
| $result = $zip->create( | |
| $source, | |
| PCLZIP_OPT_REMOVE_PATH, | |
| $remove_path | |
| ); | |
| if ( ! $result ) { | |
| wp_die( 'ZIP creation failed: ' . esc_html( $zip->errorInfo( true ) ) ); | |
| } | |
| header( 'Content-Type: application/zip' ); | |
| header( 'Content-Disposition: attachment; filename="' . basename( $zip_path ) . '"' ); | |
| header( 'Content-Length: ' . filesize( $zip_path ) ); | |
| readfile( $zip_path ); | |
| @unlink( $zip_path ); | |
| exit; | |
| } | |
| /** | |
| * Plugin download links. | |
| */ | |
| add_filter( 'plugin_action_links', function ( $actions, $plugin_file ) { | |
| if ( ! current_user_can( 'manage_options' ) ) { | |
| return $actions; | |
| } | |
| $url = wp_nonce_url( | |
| admin_url( 'plugins.php?wzdl_download_plugin=' . rawurlencode( $plugin_file ) ), | |
| 'wzdl_plugin_' . $plugin_file | |
| ); | |
| $actions['download_zip'] = '<a href="' . esc_url( $url ) . '">Download ZIP</a>'; | |
| return $actions; | |
| }, 10, 2 ); | |
| /** | |
| * Theme download page. | |
| */ | |
| add_action( 'admin_menu', function () { | |
| add_theme_page( | |
| 'Download Theme ZIP', | |
| 'Download Theme ZIP', | |
| 'manage_options', | |
| 'wzdl-theme-download', | |
| function () { | |
| echo '<div class="wrap"><h1>Download Theme ZIP</h1><table class="widefat striped">'; | |
| echo '<thead><tr><th>Theme</th><th>Action</th></tr></thead><tbody>'; | |
| foreach ( wp_get_themes() as $slug => $theme ) { | |
| $url = wp_nonce_url( | |
| admin_url( 'themes.php?wzdl_download_theme=' . rawurlencode( $slug ) ), | |
| 'wzdl_theme_' . $slug | |
| ); | |
| echo '<tr>'; | |
| echo '<td>' . esc_html( $theme->get( 'Name' ) ) . '</td>'; | |
| echo '<td><a class="button button-primary" href="' . esc_url( $url ) . '">Download ZIP</a></td>'; | |
| echo '</tr>'; | |
| } | |
| echo '</tbody></table></div>'; | |
| } | |
| ); | |
| }); | |
| /** | |
| * Handle downloads. | |
| */ | |
| add_action( 'admin_init', function () { | |
| if ( isset( $_GET['wzdl_download_plugin'] ) ) { | |
| $plugin_file = sanitize_text_field( wp_unslash( $_GET['wzdl_download_plugin'] ) ); | |
| check_admin_referer( 'wzdl_plugin_' . $plugin_file ); | |
| $plugin_dir = dirname( $plugin_file ); | |
| if ( $plugin_dir !== '.' ) { | |
| $source = WP_PLUGIN_DIR . '/' . $plugin_dir; | |
| $name = $plugin_dir . '.zip'; | |
| } else { | |
| $source = WP_PLUGIN_DIR . '/' . basename( $plugin_file ); | |
| $name = basename( $plugin_file, '.php' ) . '.zip'; | |
| } | |
| wzdl_download_zip( $source, $name, WP_PLUGIN_DIR ); | |
| } | |
| if ( isset( $_GET['wzdl_download_theme'] ) ) { | |
| $theme_slug = sanitize_key( wp_unslash( $_GET['wzdl_download_theme'] ) ); | |
| check_admin_referer( 'wzdl_theme_' . $theme_slug ); | |
| $theme = wp_get_theme( $theme_slug ); | |
| if ( ! $theme->exists() ) { | |
| wp_die( 'Theme not found.' ); | |
| } | |
| wzdl_download_zip( | |
| $theme->get_stylesheet_directory(), | |
| $theme_slug . '.zip', | |
| get_theme_root() | |
| ); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment