Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7624761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:03:08+00:00 2026-05-31T05:03:08+00:00

I’m using wordpress to create a ‘download’ popup that goes to a file attachment

  • 0

I’m using wordpress to create a ‘download’ popup that goes to a file attachment ID. The complexities aren’t important, but basically I have it open in a new window with a “Download” link.

The file type is going to be mp3 so I don’t want the browser to go to the mp3 and try to use an embedded quicktime player or anything like that, I just want it to force it to download.

Is there an easier way to do this than using php header() type stuff?

My ‘download’ popup has a whole bunch of code in it that loads the WP environment, pulls some attachment urls, changes the title of the page, etc, so its not like the php file can be blank with no whitespace except JUST the header() code (which I know it cant have anything else in the file in order to work).

Here’s the code for the popup window that comes up with the ‘download’ link. to sum up my question, how do i make the download link in the following code just download the mp3 rather than trying to load it into the browser.

As a sidenote is there a way i can close the window automatically once the download starts? Its not a big deal because I have a close window that would just be nice.

<?php
/**
 * Front to the WordPress application. This file doesn't do anything, but loads
 * wp-blog-header.php which does and tells WordPress to load the theme.
 *
 * @package WordPress
 */

/**
 * Tells WordPress to load the WordPress theme and output it.
 *
 * @var bool
 */
define('WP_USE_THEMES', false);
require('./wp-blog-header.php');

$mix_id = $_GET['m'];
$track_id = $_GET['t'];

$mix_attachment_id  = get_post_meta( $mix_id, 'zip', true );
$mix_attachment_url = wp_get_attachment_url( $mix_attachment_id );

// Update download count
$downloads = get_post_meta($mix_id, 'downloads', true);
$downloads = (int)$downloads + 1;
update_post_meta($mix_id, 'downloads', $downloads );

// Determine if track or mix download
if ($track_id == '') {
  // Mixtape Play
  $title = get_the_title($mix_id);
  $link_text = "Download Mixtape";
  $download_link = wp_get_attachment_url( $mix_attachment_id );
} else {
  // Track Download
  $title = get_the_title($track_id);
  $track_attachment_id = get_post_meta($track_id, 'mp3', true);
  $download_link = wp_get_attachment_url( $track_attachment_id );
  $link_text = "Download Track";

}
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title; ?></title>
<style type="text/css">

  body {
    text-align: center;
    margin-top: 40px;
    font-family: arial, sans-serif;

  }

  a.download {
    background: #1B62A0;
    font-size: 28px;
    padding: 10px 20px;
    color: #fff;
    -webkit-border-radius: 10px;
    -moz-border-radius: 10px;
    border-radius: 10px;
    text-decoration: none;
    display: inline;
  }

  a.download:hover {
    background: #5DD8F0;
  }

  a.close {
    text-align: center;
    margin-top: 20px;
    display: block;
  }
</style>
</head>
<body>
  <a class="download" href="<?php echo $download_link; ?>"><?php echo $link_text; ?></a>
  <a href="#" class="close" onclick="javascript:window.close(); return false;">Close Window</a>
</body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T05:03:12+00:00Added an answer on May 31, 2026 at 5:03 am

    I’ve done something similar, although not within WP. I don’t understand why you can’t just do the Header() logic at the top of the page, before any headers are sent?

    Something like below should work, just put it at the top of the page

    $s_filename = $_GET['s'];
    
    // SAVE FILE
    if( !empty( $s_filename ) )
    {
        if( file_exists( $s_filename ) ) 
        {
                $fsize = filesize( $s_filename );
                // SET HEADERS
                header("Content-Disposition: attachment; filename = " . $s_filename);
                header("Content-Type: application/octet-stream");
                header("Content-Length: " . $fsize);
                header("Cache-Control: private");
    
                readfile( $s_filename );
    
                $result = "Your file download will begin shortly.<br />\r\n";
        }
    }
    else
    {
        $result = "Error: File not found.<br />\r\n";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm trying to create an if statement in PHP that prevents a single post
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.