[wordpress插件] CSV DownloadCSV下载

wordpress 插件 文章 2020-04-06 09:00 552 0 全屏看文

AI助手支持GPT4.0

评分
100
描述

A plugin for WP developers to easily add CSV download links to the admin section or front end.

用于WP开发人员的插件,可轻松将CSV下载链接添加到管理部分或前端。

When a specific GET variable is detected (from the download link) your data, which needs to be a multi-dimensional array, is passed to a function that converts it to a csv file and triggers the download.

当检测到特定的GET变量(通过下载链接)时,您的数据(必须是多维数组)将传递给一个函数,该函数将其转换为csv文件并触发下载。

Add the following code to functions.php.

将以下代码添加到 functions.php

Admin Example

管理员示例

/** 

  / ** 

* Instantiate CSVDownload class with appropriate arguments (listed in class). 

*使用适当的参数实例化CSVDownload类(在类中列出)。 

* Arguments are optional 

*参数是可选的 

*/

* /

if (class_exists('CSVDownload')) {  

如果(class_exists('CSVDownload')){  

$csv_button = New CSVDownload(array(    

$ csv_button =新的CSVDownload(array(    

'post_types' => array('page'),    

'post_types'=> array('page'),    

'post_type_ids' => array(420, 114, 749),    

'post_type_ids'=> array(420,114,749),    

'metabox_title' => 'Download CSV Data',    

'metabox_title'=>'下载CSV数据',    

'help_text' => 'CSV file containing useful data.',    

'help_text'=>'包含有用数据的CSV文件。',    

'parameter' => 'csv_export_button',    

'参数'=>'csv_export_button',    

'button_text' => 'Download'  

'button_text'=>'下载'  

));

));

}

}

/** 

/ ** 

* Get results, convert to csv file, and trigger download. 

*获取结果,转换为csv文件,并触发下载。 

*/

* /

if(isset($_GET[$csv_button->parameter])) {  

if(isset($ _ GET [$ csv_button-> parameter])){  

add_action('admin_init', function(){    

add_action('admin_init',function(){    

// Get results array    

//获取结果数组    

$results = get_csv_file_results();    

$ results = get_csv_file_results();    

// Convert results array to csv file and trigger download.    

//将结果数组转换为csv文件并触发下载。    

CSVDownload::download_csv_results(array(      

CSVDownload :: download_csv_results(array(      

'results' => $results,      

'results'=> $ results,      

'file_name' => 'csv_data'    

'文件名'=>'csv_data'    

));    

));    

exit;  

出口;  

}, 1);

},1);

}

}

/** 

/ ** 

* Get the results array for the csv button download. 

*获取用于csv按钮下载的结果数组。 

* @return array 

* @返回数组 

*/

* /

function get_csv_file_results(){  

函数get_csv_file_results(){  

// Create multi-dimensional array.  

//创建多维数组。  

$results_array = array(    

$ results_array =数组(    

array('Email','User Name','Favorite Color'), // Column headers    

array('Email','User Name','Favorite Color'),//列标题    

array('fake@email.com','coolguy1','blue'),    

array('fake@email.com','coolguy1','blue'),    

array('fake@email.com','coolguy2','orange'),    

array('fake@email.com','coolguy2','orange'),    

array('fake@email.com','coolguy3','pink'),    

array('fake@email.com','coolguy3','pink'),    

array('fake@email.com','coolguy4','red'),  

array('fake@email.com','coolguy4','red'),  

);  

);  

// Return results array  

//返回结果数组  

return $results_array;

返回$ results_array;

}

}

Front End Example

前端示例

Add a button element to your HTML.

在HTML中添加按钮元素。

?csv_export_button=1">Download

  ?csv_export_button = 1“>下载

Add init action callback and provide array data.

添加初始化操作回调并提供数组数据。

/** 

  / ** 

* Get results, convert to csv file, and trigger download. 

*获取结果,转换为csv文件,并触发下载。 

*/

* /

if(isset($_GET['csv_export_button'])) {  

if(isset($ _ GET ['csv_export_button'])){  

add_action('init', function(){    

add_action('init',function(){    

// Get results array    

//获取结果数组    

$results = get_csv_file_results();    

$ results = get_csv_file_results();    

// Convert results array to csv file and trigger download.    

//将结果数组转换为csv文件并触发下载。    

CSVDownload::download_csv_results(array(      

CSVDownload :: download_csv_results(array(      

'results' => $results,      

'results'=> $ results,      

'file_name' => 'csv_data'    

'文件名'=>'csv_data'    

));    

));    

exit;  

出口;  

}, 1);

},1);

}

}

/** 

/ ** 

* Get the results array for the csv button download. 

*获取用于csv按钮下载的结果数组。 

* @return array 

* @返回数组 

*/

* /

function get_csv_file_results(){  

函数get_csv_file_results(){  

// Create multi-dimensional array.  

//创建多维数组。  

$results_array = array(    

$ results_array =数组(    

array('Email','User Name','Favorite Color'), // Column headers    

array('Email','User Name','Favorite Color'),//列标题    

array('fake@email.com','coolguy1','blue'),    

array('fake@email.com','coolguy1','blue'),    

array('fake@email.com','coolguy2','orange'),    

array('fake@email.com','coolguy2','orange'),    

array('fake@email.com','coolguy3','pink'),    

array('fake@email.com','coolguy3','pink'),    

array('fake@email.com','coolguy4','red'),  

array('fake@email.com','coolguy4','red'),  

);  

);  

// Return results array  

//返回结果数组  

return $results_array;

返回$ results_array;

}

}

AgencyLabs.com – A digital production studio.

AgencyLabs.com –数字制作工作室。

安装步骤

    1. Upload the plugin files to the /wp-content/plugins/csv-download directory, or install the plugin through the WordPress plugins screen directly.
    2. 将插件文件上传到 / wp-content / plugins / csv-download 目录,或直接通过WordPress插件屏幕安装插件。

    3. Activate the plugin through the ‘Plugins’ screen in WordPress
    4. 通过WordPress中的“插件”屏幕激活插件

下载地址
https://downloads.wordpress.org/plugin/csv-download.zip
-EOF-

AI助手支持GPT4.0