Return to Snippet

Revision: 46136
at May 13, 2011 11:46 by stewartduffy

Initial Code
Template Name: Export Pods Data

function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
 } else {
 return $pageURL;

<form action="<?php echo curPageURL(); ?>" method="post">
	<label for="out_file">Output File</label><br />
	<input type="text" name="out_file" id="out_file" /><br />
	<label for="use_pod">Pod to Export</label><br />
	<input type="text" name="use_pod" id="use_pod" /><br />
	<input type="submit" name="export_submit" />

if (isset($_POST['export_submit'])) {
	$out_file = $_POST['out_file'];
	$use_pod = $_POST['use_pod'];
	export($out_file, $use_pod);

	function export($out_file, $use_pod){
		$export = "csv";               // Can be "php" or "csv" (default: php)
		$Record = new PodAPI($use_pod, $export); 
		$data = $Record->export();
		global $user_ID , $user_level;
		get_currentuserinfo();     //Get the information about the current user.
		if ($user_ID && 10 == $user_level) {                       //Check to see if the user is logged in and is an administrator
		  if (($fp = fopen($out_file, 'w')) === FALSE) {         //Attempt to open $out_file for writing and check for success.
		    die ("Could not open $out_file. Export aborted.");  //Fail if unable to.
		  if ('csv' == strtolower($export)) {            //If a CSV file is desired
		    $data_keys = array_keys($data[0]);      //Get the pod column labels
		    fputcsv($fp, $data_keys);                    //Write the column labels as the first line of the CSV file.
		    foreach ($data as $line) {                    //Loop through the data line by line
		       foreach ($line as &$field) {                //Loop through each line of data by field
		          if (is_array($field)) {                     //If the field is a PICK column
		           array_walk($field, 'comma_trans');      //Translate any commas in the field to HTML "&#44;"
		           $field = 'array(' . implode(', ', $field) . ')';  //Implode the items into a comma separated list wrapped in array().
		      fputcsv($fp, $line);       //Write the line to the file.
		  } else {                           //Otherwise, output the data as PHP
		    fwrite($fp, var_export($data, TRUE));
		  fclose($fp); //Close the file
		  echo "Export complete. <a href='$out_file'>Open $out_file</a><br /><br />\n";
		} else {                                  //User doesn't have permission
		  include(get_404_template());   			//Nothing to see here, move along...
		  exit();                                  //Exit.

function comma_trans(&$item)
    $item = strtr($item, ',', '&#44'); //Translates any commas into their HTML entity.

Initial URL

Initial Description
1) name the file something like "template-pods-export.php"
2) create a new Pods Page & choose the newly created template
3) goto your worpress URL/export
4) enter filename.csv & pod name you want to export.
5) Submit.
6) Away Laughing

Initial Title
Wordpress, PODS CMS data export template

Initial Tags

Initial Language