1

I want to add functionality to the wordpress plugin to download custom file. here is my code.

add_action('admin_menu','function_name');

and here is the download code.

header("Content-Disposition: attachment; filename=map.html");
header("Content-Length: ".strlen($content));
echo $content;
exit;

But, I'm getting error.

[22-Feb-2015 00:00:00 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /path/to/wp/wp-admin/includes/template.php:1877) in /path/to/wp/wp-content/plugins/plugin-name/lib/model.php on line 99

I'm making the request like this

http://www.website.com/wp-admin/admin.php?page=file-download&fileid=10
Muaaz Khalid
  • 2,199
  • 2
  • 28
  • 51
  • Make sure you are calling header functions before any output is sent. – Whirlwind Feb 22 '15 at 09:07
  • I'm talking about wordpress plugin. – Muaaz Khalid Feb 22 '15 at 09:14
  • here it is how my plugin download page is called https://www.website.com/wp-admin/admin.php?page=file-download&fileid=10 – Muaaz Khalid Feb 22 '15 at 09:15
  • It doesn't matter, If you mix HTML and PHP, you should never call header in the php code found after some HTML code...You probably have to change the way you are calling header, or if you can't try to use ob_start and ob_flush. – Whirlwind Feb 22 '15 at 09:18
  • possible duplicate of [How to fix "Headers already sent" error in PHP](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – Jon Surrell Feb 22 '15 at 09:22
  • Its not the duplicated question. Anyhow, there is nothing I wrote before the code is started. but its a wordpress feature that does. I'm passing the parameter `page=map-download` in URL so, first the wordpress renders the sidebar and header then my page is loaded. The question is how to prevent from such behavior? – Muaaz Khalid Feb 22 '15 at 09:27

1 Answers1

0

I suggest using this hook

http://codex.wordpress.org/Plugin_API/Action_Reference/load-%28page%29

with the return value of the add_menu_page function.

The load-$page is like the init hook for admin pages.

VRPF
  • 3,118
  • 1
  • 14
  • 15