3

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?

Eugene
  • 41
  • 6
  • If you get the page instead of the file, probably the `if` statement is failing. Can you check the contents of `$_GET`? Also, is this url `/wp-admin/admin.php?page=my-plugin.php?export=1` correct? The second `?` should be a `&`. – Aioros Jan 17 '19 at 18:52
  • Sorry, I used: /wp-admin/admin.php?page=my-plugin&export=1 – Eugene Jan 17 '19 at 19:02
  • I think understand what the problem: /wp-admin/admin.php?page=my-plugin.php?export=1 - admin.php adds admin console header and footer whith I see in my file... – Eugene Jan 17 '19 at 19:05
  • I may use link: /wp-content/plugins/my-plugin/users.php?export=1 to get file without wp console but at that way i get problem with working worpress core fuctions... Then i use: require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' ); and get error: access denied to database...using password:no... – Eugene Jan 17 '19 at 19:07
  • @ Aioros - I get page inside csv file. – Eugene Jan 17 '19 at 19:14
  • I think here problem close to mine: [link]https://stackoverflow.com/questions/16722818/wordpress-admin-widget-that-exports-data I'll try it tommorow – Eugene Jan 17 '19 at 19:18

1 Answers1

0

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
    }
}
Andy P
  • 463
  • 4
  • 6