Reputation: 47
I faced with problem. I have my self written plugin (my-plugin). It has admin page "users".
That page uses script /wp-content/plugins/my-plugin/my-plugin.php which inludes file /wp-content/plugins/my-plugin/users.php In admin area i use such url: /wp-admin/admin.php?page=my-plugin.php
I made there csv export link: /wp-admin/admin.php?page=my-plugin.php?export=1
code in users.php:
if (isset($_GET['export']))
{
user_export($_GET['export']);
}
function user_export($type)//export exel
{
global $wpdb;
$filename = "users_".date("Y-m-d_H-i",time());
$result = mysql_query("SHOW COLUMNS FROM `".$wpdb->prefix . "users`") or die(mysql_error());
$i = 0;
if (mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result))
{
$csv_output .= $row['Field']."; ";
$i++;
}
}
$csv_output .= "\n";
$values = mysql_query("SELECT * FROM `".$wpdb->prefix . "users`") or die(mysql_error());
while ($rowr = mysql_fetch_row($values))
{
for ($j=0;$j<$i;$j++)
{
$csv_output .= $rowr[$j]."; ";
}
$csv_output .= "\n";
}
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header("Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;
}
I'm getting the file by clicking on the link, but that file contains all wordpress admin panel design.
I need get just get excel export file by clicking on link on admin page of my plugin.
May anybody help me?
Upvotes: 3
Views: 2083
Reputation: 513
I realise this is a stale question, but I found it while looking to do a similar file export from an admin screen, so hopefully this may help someone else.
You can specify a handler for this when setting up your admin page to appear in the admin menu:
function my_plugin_menu()
{
$my_page = add_menu_page(
[ ... parameters here ... ]
);
// Handle export before any page output
add_action( 'load-' . $my_page, 'user_export_handler' );
}
add_action( 'admin_menu', 'my_plugin_menu' );
function user_export_handler()
{
// code from OP's question
if (isset($_GET['export']))
{
user_export($_GET['export']);
exit; // prevent admin page loading and getting appended to file
}
}
Upvotes: 0