LoginSignup
4
2

More than 3 years have passed since last update.

[WordPress]管理画面でcsvダウンロード

Last updated at Posted at 2019-02-20

結構要望が多い機能だと思うが割と情報が無い。
投稿一覧(edit.php)かプラグインでやる。
それぞれに良し悪しあるんで状況によって判断。

投稿一覧でやる

バルクアクション(一括操作のプルダウン)に追加する。
ボタン単体でやらせたいけどいい感じのフック見つからずひとまず断念。

追記(2020/03/30)
いい感じのフック見つかる→ manage_posts_extra_tablenav

manage_posts_extra_tablenav
    $format = '
<div class="alignleft actions">
    <button type="submit" class="button" name="action">ダウンロード</button>
</div>';
functions.php

add_filter('bulk_actions-edit-member', function ($bulk_actions) {
    $bulk_actions['download'] = 'ダウンロード';
    return $bulk_actions;
});

add_action('load-edit.php', function () {
    $wp_list_table = _get_list_table('WP_Posts_List_Table');
    $action = $wp_list_table->current_action();
    if ( !$action || $action != 'download' ) return;
    check_admin_referer('bulk-posts');
    $row;
    if ( empty($row) ) return;
    $filepath = sprintf('%s.csv', date('YmdHis'));
    if ( touch($filepath) ) {
        $file = new SplFileObject($filepath, "w");
        $file->fputcsv([
                _s('タイトル'),
                _s('作成日'),
        ]);
        foreach ($row as $r) {
            $file->fputcsv([
                    _s($r['post_title']),
                    $r['created'],
            ]);
        }
        @header("Pragma: public");
        @header("Expires: 0");
        @header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        @header("Cache-Control: private", false);
        @header("Content-Type: application/force-download");
        @header("Content-Disposition: attachment; filename=\"".basename($filepath)."\";" );
        @header("Content-Transfer-Encoding: binary");
        @readfile($filepath);
        unlink($filepath);
        die;
    }

});

function _s($val) {
    return mb_convert_encoding($val, 'SJIS-win', 'UTF-8');
}

プラグイン作ってやる

プラグインはあんまり作らなくて諸々知らないので
きっとちゃんとしたやり方があると思われ。

/plugins/{plugin-slug}/{plugin-slug}.php

/*
 * 省略
 */

add_action('init', function () {
    if ( is_admin() && ($_REQUEST['page']??'')=='{menu_slug}' && ($_REQUEST['cmd']??'')=='download' ) {
        $row;
        if ( empty($row) ) return;
        $filepath = sprintf('%s.csv', date('YmdHis'));
        if ( touch($filepath) ) {
            $file = new SplFileObject($filepath, "w");
            $file->fputcsv([
                    _s('タイトル'),
                    _s('作成日'),
            ]);
            foreach ($row as $r) {
                $file->fputcsv([
                        _s($r['post_title']),
                        $r['created'],
                ]);
            }
            @header("Pragma: public");
            @header("Expires: 0");
            @header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
            @header("Cache-Control: private", false);
            @header("Content-Type: application/force-download");
            @header("Content-Disposition: attachment; filename=\"".basename($filepath)."\";" );
            @header("Content-Transfer-Encoding: binary");
            @readfile($filepath);
            unlink($filepath);
            die;
        }
    }
});
4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2