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 8677879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:34:36+00:00 2026-06-12T20:34:36+00:00

The following sample plugin lets you download a text file when the download button

  • 0

The following sample plugin lets you download a text file when the download button is pressed in the admin panel. The problem is that I expected the text file name would be hello_world.txt but it somehow becomes options-general.txt.

If this line header('Content-Disposition: attachment; filename="' . $file_name . '"'); is directly set the file name like header('Content-Disposition: attachment; filename="hello_world.txt"'); it works fine.

/* Plugin Name: Sample Download Button  */

$sample_download_button = new Sample_Download_Button_AdminPage('Sample Download Button');
add_action('init', array($sample_download_button, "download_text"));
add_action('admin_menu', array($sample_download_button , "admin_menu"));

class Sample_Download_Button_AdminPage {
    function __construct($pagetitle, $menutitle='', $privilege='manage_options', $pageslug='') {
        $this->pagetitle = $pagetitle;
        $this->menutitle = !empty($menutitle) ? $menutitle : $pagetitle;
        $this->privilege = $privilege;
        $this->pageslug = !empty($pageslug) ? $pageslug : basename(__FILE__, ".php");
    }
    function admin_menu() {
        add_options_page($this->pagetitle, $this->menutitle, $this->privilege, $this->pageslug, array(&$this, 'admin_page'));
    }
    function admin_page() {     
        ?>
        <div class="wrap">
            <h1>Download Button Sample</h1>
            <form action="" method="post" target="_blank">
            <input type="submit" value="download" name="download">
            </form>
        </div>
        <?php
    }   
    function download_text($file_name='hello_world.txt') {
        if (!isset($_POST['download'])) return;
        $your_text = 'hello world';
        header("Content-type: text/plain");
        header('Content-Disposition: attachment; filename="' . $file_name . '"');
        echo $your_text;
        exit;
    }       
}

Why is it? And how can I set a default parameter value? I tried it with a normal function and its default value is also not reflected. Thanks for your info.

  • 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-06-12T20:34:37+00:00Added an answer on June 12, 2026 at 8:34 pm

    I tested this a couple of ways, including copying your complete function into a ‘sandbox’ install I have.

    Not all hooks pass parameters. From what I can tell, the init hook does not. Whether it accepts/passes a parameter is determined when the hook is defined. Apparently, when a hook fires that wasn’t intended to pass a parameter, that passes an empty string to the callback. In your case, this means that it is effectively overwriting your default and leaving you without a filename, which in turn causes the browser to use the file name of the page to which the form was submitted– options-general.php.

    I’d pass the file name as a hidden field in the form and send it in $_POST, and set my default the way your have set $your_text.

    function download_text() {
        if (!isset($_POST['download'])) return;
        if (!isset($_POST['filename'])) $file_name = $_POST['filename'];
        else $file_name = 'hello_world.txt';
        $your_text = 'hello world';
        header("Content-type: text/plain");
        header('Content-Disposition: attachment; filename="' . $file_name . '"');
        echo $your_text;
        exit;
    }  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following sample plugin lets you access a custom feed url and outputs customized
I have the following problem: I am creating a simple plugin jquery carousel, to
I have the following sample code and noticed that if I attempt to use
I have the following sample data: Id Name Quantity 1 Red 1 2 Red
The following sample plugin adds the custom mod rewrite rules to .htaccess when the
I'm following this guide, http://www.2dconcept.com/jquery-grid-rails-plugin , to setup a sample jQuery datagrid. Everything appears
I received the following gradle build file ( gae.gradle ) with a sample project
I have a photoshop plugin for a file format Ive written in c++ that
I wrote the following sample code to see how ARC works @property (nonatomic, weak)
In the following sample, I build a query to do a bulk insert into

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.