• I am working in functions.php file. I would like to have a button. If user click on it, a CSV file will be download. How can I develop this feature ?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Step-1: Add this code with init action in functions.php file.
    (this code is to show export button)
    
    function admin_post_list_add_export_button( $which ) {
        global $typenow;
      
        if ( 'post' === $typenow && 'top' === $which ) {
            ?>
            <input type="submit" name="jnext_export_posts" class="button button-primary" value="<?php _e('Export Posts'); ?>" />
            <?php
        }
    }
    add_action( 'admin_init', 'admin_post_list_add_export_button' );
    
    Screenshot: https://prnt.sc/re4QGfilsCZ4
    
    Step-2: Add this code with init action in functions.php file.
    (this code is for the download CSV file)
    
    function jnext_export_posts() {
        if(isset($_GET['jnext_export_posts'])) {
            $args = array(
                'post_type' => 'post',
                'post_status' => 'publish',
            );
     
            if ( isset($_GET['post']) ) {
                $args['post__in'] = $_GET['post'];
            } else {
                $args['posts_per_page'] = -1;
            }
      
            global $post;
            $arr_post = get_posts($args);
            if ($arr_post) {
      
                header('Content-type: text/csv');
                header('Content-Disposition: attachment; filename="wp-posts.csv"');
                header('Pragma: no-cache');
                header('Expires: 0');
      
                $file = fopen('php://output', 'w');
      
                fputcsv($file, array('Post ID', 'Post Title', 'URL', 'Categories', 'Tags'));
      
                foreach ($arr_post as $post) {
                    setup_postdata($post);
                       
                    $categories = get_the_category();
                    $cats = array();
                    if (!empty($categories)) {
                        foreach ( $categories as $category ) {
                            $cats[] = $category->name;
                        }
                    }
      
                    $post_tags = get_the_tags();
                    $tags = array();
                    if (!empty($post_tags)) {
                        foreach ($post_tags as $tag) {
                            $tags[] = $tag->name;
                        }
                    }
      
                    fputcsv($file, array(get_the_ID(), get_the_title(), get_the_permalink(), implode(",", $cats), implode(",", $tags)));
                }
      
                exit();
            }
        }
    }
     
    add_action( 'admin_init', 'jnext_export_posts' );
    
    Thank You.
    • This reply was modified 2 years, 1 month ago by Vipul Ghori.

    Hi @mabufoysal
    Sorry for the trouble. You may apply this simple solution. Just add an extra attribute called “download” and put a name whatever you want as the file name. It should work like a charm. 😃

    <a href="path_to_file" download="proposed_file_name">Download</a>

    Give it a try and let me know how that goes! 😄

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Download CSV’ is closed to new replies.