0

I am trying to add a submenu to the Tools menu. When the custom Tools sub-menu is chosen, I want it to call a function. That function will echo things out to the browser page that is loaded. My code is like this:

  1. Hook the function that will add a submenu to the Tools menu.

    add_action('admin_menu', 'create_tools_submenu');
    
  2. The function that adds a submenu to Tools menu.

    function create_tools_submenu() {
        add_management_page( 'Page Title', 'Menu Title', 'manage_options', 'slug', 'generate_page_content' );
    }
    
  3. The function that runs when my custom Tools submenu page is loaded.

    function generate_page_content() {
        echo some stuff to the page...
    }
    

The code is partially working. A custom submenu is present under the Tools menu. However, when I select it, instead of loading a new page with the output of my function, a standard "save" popup box appears, with the filename tools.php already in the "Save as" field.

When I look at the Codex entry for add_management_page https://codex.wordpress.org/Function_Reference/add_management_page

it says that the return value is

The resulting page's hook_suffix, or false if the user does not have the $capability required.

I understand that, in my case, the resulting page's hook_suffix is tools.php, and that is the exact name of the file that appears in the popup "Save As" box when I run my code. But I don't understand, why in the world my code is even generating a "Save As" popup box? There is no where in my code that calls for a save pop up box, so... where is it coming from? Does add_management_page() always create a "Save As" popup box? I feel that I'm missing something obvious.

Thanks very much for any help. I'm really stumped by this.

  • 1
    this popup may come from the function generate_csv_page_content. is there a header of content-type different from HTML ? – mmm Oct 10 '16 at 18:49
  • In add_management_page you have generate_page_content, but then you write the callback as generate_csv_page_content. That effectively means there is no callback function and you get unexpected results. – cjbj Oct 10 '16 at 20:07
  • Yes there is a header. In generate_page_content, I have the code: header('Content-Encoding: UTF-8'); header('Content-type: text/csv; charset=UTF-8'); I was given some of this code from a person I'm working with. Those two header lines I simply copy and pasted from the code I was given. I thought it was necessary. The page I am generating should contain a link to a CSV file that can be downloaded. I thought the header code had something to do with generating the CSV file. – LeonardShelby Oct 10 '16 at 21:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.