PHP Developer

Wednesday, 1 July 2015

Export post in CSV Format

Export post in CSV Format :

This plugin helps you to Export custom post with CSV file. CSV file include full data of custom post like featured image link, custom meta box value, title, content, etc.


<?php
/**
 * Plugin Name: Export Cause with CSV File
 * Plugin URI: https://plus.google.com/u/0/115833273431242118637
 * Description: This plugin helps you to Export Cause (custom post) with CSV file. CSV file include full data of Cause(custom post) like featured image link, custom meta box value, title, content, etc.
 * Version: 1.0.0
 * Author: Sunil Sharma
 * Author URI: http://astro-global.blogspot.in/
 */

$pathss = $_SERVER['DOCUMENT_ROOT'];
if(!defined('DS')){
    define('DS',DIRECTORY_SEPARATOR);
}
include_once $_SERVER['DOCUMENT_ROOT'].'/wp-load.php';

function export_cause_activation()
{
    if ( ! current_user_can( 'activate_plugins' ) )
        return;
}
function export_cause_deactivation()
{
    if ( ! current_user_can( 'activate_plugins' ) )
        return;
}
function export_cause_uninstall()
{
    if ( ! current_user_can( 'activate_plugins' ) )
        return;
}
register_activation_hook( __FILE__, 'export_cause_activation' );
register_deactivation_hook( __FILE__, 'export_cause_deactivation' );
register_uninstall_hook( __FILE__, 'export_cause_uninstall' );

function export_cause_option(){
    $wp_query = new WP_Query();
    $wp_query->query('posts_per_page=-1&post_type=project&orderby=ID&order=DESC');
    $get_all_cause = $wp_query->posts;   
    $content = array();
    $title = array("Title", "Main Information", "Sub Information", "Category", "Currency", "Goal", "Contact", "Expiry Date", "Image", "Phone Number", "Website", "Email Id", "Facebook Link", "Facebook Likes", "Twitter Link", "Twitter Followers", "Linkedin Link", "Linkedin members", "Number of Teams", "State", "City", "Paypal");    //$title variable is create for header row in csv
   
    foreach($get_all_cause as $all_causes){
    $row = array();
    $row[] = $all_causes->post_title;
    $row[] = $all_causes->post_content;
    $row[] = get_post_meta($all_causes->ID, 'bereiding', true);   
    $cause_categories = Array();
    $cause_cats = wp_get_object_terms($all_causes->ID, 'project-category');
    foreach($cause_cats as $cat) {
        $cause_categories[] = $cat->name;
    }
    $row[] = implode(",",$cause_categories); 
   //get category of post in string

    $settings = get_post_meta($all_causes->ID, 'settings', true);
    $row[] = $settings['currency'];
    $row[] = $settings['target'];
    $row[] = get_post_meta($all_causes->ID, 'project_contactname', true);
    $row[] = $settings['date'];
    $row[] = wp_get_attachment_url( get_post_thumbnail_id($all_causes->ID) );
    $row[] = get_post_meta($all_causes->ID, 'project_phonenum', true);
    $row[] = get_post_meta($all_causes->ID, 'project_website', true);
    $row[] = get_post_meta($all_causes->ID, 'project_email', true);
    $row[] = get_post_meta($all_causes->ID, 'project_fblink', true);
    $row[] = get_post_meta($all_causes->ID, 'project_fblikes', true);
    $row[] = get_post_meta($all_causes->ID, 'project_twlink', true);
    $row[] = get_post_meta($all_causes->ID, 'project_twfollow', true);
    $row[] = get_post_meta($all_causes->ID, 'project_lklink', true);
    $row[] = get_post_meta($all_causes->ID, 'project_lkmember', true);
    $row[] = get_post_meta($all_causes->ID, 'project_noteams', true);
    $cause_stat = Array();
    $cause_states = wp_get_object_terms($all_causes->ID, 'projects-states');
    foreach($cause_states as $cause_state) {
        $cause_stat[] = $cause_state->name;
    }
    $row[] = implode(",",$cause_stat);
    $row[] = get_post_meta($all_causes->ID, 'project_city', true);
    $row[] = get_post_meta($all_causes->ID, 'project_funding_detail', true);   
    $content[] = $row;
    //This array collect all data from database which is show in CSV file

    }
    /* echo "<pre>";
    print_r($content);
    echo "</pre>"; */

    outputCsv($content,$title);
}
function outputCsv($content,$title){
    $filename = "Export_cause_csv.csv";       
    header("Content-type: text/csv");
    header("Content-Disposition: attachment; filename=$filename");
    header("Pragma: no-cache");
    header("Expires: 0");   
    $output = fopen('php://output', 'w');
    ob_clean();
    fputcsv($output, $title);
    foreach($content as $con) {
        fputcsv($output, $con);
    }
    fclose($output);
    exit();
    ob_flush();
}
function export_cause () {
add_menu_page('Export Cause CSV','Export Cause CSV','manage_options','export_cause', 'export_cause_option');
}
add_action('admin_menu','export_cause');

?>

No comments:

Post a Comment