Home / Admin / Core_Universal_Download Fix
Duplicate Snippet

Embed Snippet on Your Site

Core_Universal_Download Fix

/**
* WPCode Snippet: Auto-Create .htaccess in Uploads Folder for Force Download
* Description: Creates/updates .htaccess in wp-content/uploads to force .txt file downloads
* Location: Run Everywhere
* Priority: 10
*
* INSTALL ORDER: #6 - Install after REST API is updated
*
* IMPORTANT: This creates a .htaccess file in /wp-content/uploads/ to force downloads
*/

ismail daugherty PRO
<10
Code Preview
php
<?php
/**
 * WPCode Snippet: Auto-Create .htaccess in Uploads Folder for Force Download
 * Description: Creates/updates .htaccess in wp-content/uploads to force .txt file downloads
 * Location: Run Everywhere
 * Priority: 10
 */
defined( 'ABSPATH' ) || exit;
/**
 * Create or update .htaccess file in uploads folder
 */
function kic_create_uploads_htaccess_force_download() {
    
    // Only run once - check if htaccess already exists and is correct
    $upload_dir = wp_upload_dir();
    $htaccess_file = trailingslashit( $upload_dir['basedir'] ) . '.htaccess';
    
    // Check if we need to create/update the file
    $needs_update = false;
    
    if ( ! file_exists( $htaccess_file ) ) {
        $needs_update = true;
    } else {
        // Check if file contains our rules
        $current_content = file_get_contents( $htaccess_file );
        if ( strpos( $current_content, '# KIC Force Download Rules' ) === false ) {
            $needs_update = true;
        }
    }
    
    if ( ! $needs_update ) {
        return; // Already exists with our rules
    }
    
    // Build the .htaccess content
    $lines = array();
    $lines[] = '# KIC Force Download Rules for Text Files';
    $lines[] = '# Generated by KIC WPCode Snippet';
    $lines[] = '# DO NOT EDIT THIS SECTION MANUALLY';
    $lines[] = '';
    $lines[] = '<IfModule mod_rewrite.c>';
    $lines[] = 'RewriteEngine On';
    $lines[] = '';
    $lines[] = '# Force download for .txt files with ?download=1 parameter';
    $lines[] = 'RewriteCond %{QUERY_STRING} download=1';
    $lines[] = 'RewriteCond %{REQUEST_URI} \.txt$ [NC]';
    $lines[] = 'RewriteRule .* - [E=DOWNLOAD:1]';
    $lines[] = '</IfModule>';
    $lines[] = '';
    $lines[] = '<IfModule mod_headers.c>';
    $lines[] = '# Set Content-Disposition header to force download';
    $lines[] = 'Header set Content-Disposition "attachment" env=DOWNLOAD';
    $lines[] = '</IfModule>';
    $lines[] = '';
    $lines[] = '# END KIC Force Download Rules';
    $lines[] = '';
    
    $htaccess_content = implode( "\n", $lines );
    
    // Try to get existing content to preserve it
    $existing_content = '';
    if ( file_exists( $htaccess_file ) ) {
        $existing_content = file_get_contents( $htaccess_file );
        
        // Remove any existing KIC rules to avoid duplicates
        $existing_content = preg_replace(
            '/# KIC Force Download Rules.*?# END KIC Force Download Rules\s*/s',
            '',
            $existing_content
        );
    }
    
    // Combine: Our rules first, then existing content
    $final_content = $htaccess_content . "\n" . $existing_content;
    
    // Try to write the file
    $result = @file_put_contents( $htaccess_file, $final_content );
    
    if ( $result === false ) {
        // Log error for admin
        error_log( 'KIC: Failed to create/update .htaccess in uploads folder. Please create it manually.' );
        
        // Show admin notice
        add_action( 'admin_notices', 'kic_htaccess_error_notice' );
    } else {
        // Success! Show admin notice
        add_action( 'admin_notices', 'kic_htaccess_success_notice' );
    }
}
/**
 * Success notice
 */
function kic_htaccess_success_notice() {
    ?>
    <div class="notice notice-success is-dismissible">
        <p>
            <strong>✅ KIC Force Download:</strong> 
            Successfully created/updated .htaccess in uploads folder! 
            .txt files with ?download=1 will now force download.
        </p>
    </div>
    <?php
}
/**
 * Error notice
 */
function kic_htaccess_error_notice() {
    ?>
    <div class="notice notice-warning is-dismissible">
        <p>
            <strong>KIC Force Download:</strong> 
            Unable to automatically create .htaccess in uploads folder. 
            Please create it manually or check folder permissions (should be 755).
        </p>
    </div>
    <?php
}
// Run on admin init
add_action( 'admin_init', 'kic_create_uploads_htaccess_force_download', 5 );

Comments

Add a Comment