将用户信息从前端导出到CSV

时间:2019-12-03 作者:Michael

正在尝试从WordPress前端导出到CSV。这是我到目前为止的情况。我可以从管理区域导出到CSV,但由于某些原因,我无法从前端导出。我做错了什么?

// Add action hook only if action=download_csv_sl
if ( isset($_GET[\'action\'] ) && $_GET[\'action\'] == \'download_csv_sl\' )  {
// Handle CSV Export
add_action( \'init\', \'csv_export_sl\');
}

function csv_export_sl() {
    // Check if we are in WP-Admin
    if( is_admin() ){ return false; }

    // Nonce Check
    $nonce = isset( $_GET[\'_wpnonce\'] ) ? $_GET[\'_wpnonce\'] : \'\';
    if ( ! wp_verify_nonce( $nonce, \'download_csv_sl\' ) ) {
        die( \'Security check error\' );
    }

    // Get Current Date and Time
    date_default_timezone_set(\'America/Denver\');
    $currentDate = date(\'Y_m_d-H_i_s\');

    $domain = $_SERVER[\'SERVER_NAME\'];
    $filename = \'Store_List-\' . $currentDate . \'.csv\';

    // Get Quarter and Ranking from Shop Profiles
    $getQuarter = do_shortcode(\'[usermeta key="current_quarter" user="717"]\');
    if ($getQuarter == \'1\'){
        $currentQuarter = \'ranker_q1\';
    } elseif ($getQuarter == \'2\'){
        $currentQuarter = \'ranker_q1_89\';
    } elseif ($getQuarter == \'3\'){
        $currentQuarter = \'ranker_q1_90\';
    } elseif ($getQuarter == \'4\'){
        $currentQuarter = \'ranker_q1_91\';
    } else {
        $currentQuarter = \'\';
    }

    // Name the columns in the spreadsheet
    $header_row = array(
        \'Store Number\',
        \'Address\',
        \'Franchisee\',
        \'Q\'.$getQuarter.\' Ranking\'
    );
    $data_rows = array();

    // Query users
    $user_query = new WP_User_Query( array(
        \'role__in\'    => [\'shop\',\'shop_co\',\'shop_noco\'],
        \'orderby\' => \'meta_value_num\',
        \'order\' => \'ASC\',
    ) );   
    $users = $user_query->get_results();

    foreach ( $users as $user ) {
        if (!empty($user->Group)){
            $row = array(
                $user->Store_Number,             // Get Store Number
                $user->Address,                  // Get Address
                $user->display_name,             // Get Franchisee
                $user->$currentQuarter          // Get Ranking
            );
            $data_rows[] = $row;
        }
    }

    $fh = @fopen( \'php://output\', \'w\' );
    fprintf( $fh, chr(0xEF) . chr(0xBB) . chr(0xBF) );
    header( \'Cache-Control: must-revalidate, post-check=0, pre-check=0\' );
    header( \'Content-Description: File Transfer\' );
    header( \'Content-Transfer-Encoding: UTF-8\' );
    header( \'Content-type: text/csv\' );
    header( "Content-Disposition: attachment; filename={$filename}" );
    header( \'Expires: 0\' );
    header( \'Pragma: public\' );
    fputcsv( $fh, $header_row );
    foreach ( $data_rows as $data_row ) {
        fputcsv( $fh, $data_row );
    }
    fclose( $fh );

    ob_end_flush();

    die();
}

add_shortcode(\'bc_export\', \'csv_export_storelist_shortcode\');
function csv_export_storelist_shortcode() {
    $url = $_GET[\'uri\'];
    $nonce = wp_create_nonce( \'download_csv_sl\' );
    $exportList = \'<a href="\'. $url . \'&action=download_csv_sl&_wpnonce=\'. $nonce .\'" class="page-title-action">Export to CSV</a>\';
    return $exportList;
}
问题似乎在于$url 在短代码内链接路径,但我不知道路径是什么。当前代码导致单击链接时找不到页面。

1 个回复
SO网友:Michael

没关系,我想出了一个办法。我只是完全不需要url。以下是我所做的,以防其他人想知道。

我将链接更改为表单输入按钮:

<form method="post">
    <input type="submit" name="bc_export" class="btn btn-export" value="Export Store List for \'.$atts[\'value\'].\'" />
</form>
并更改了$_GET[\'action\'] 到a$_POST[]:

if ( isset($_POST["bc_export"])) {
    add_action( \'init\', \'bc_export_function\');
}

相关推荐

将css附加到unction.php时出现白屏

当我附加下线代码时,我会看到白色屏幕,在控制台日志中,它会出现下线错误,我的浏览器是最新的密码body:not(.no-transition) #wrapper, .animsition-overlay { position: relative; opacity: 0; -webkit-animation-fill-mode: both; animation-fill-mode: both; } 控制台日志错误JQMIGRATE:安装了Migrate,版本为1.4.