您现在的位置是:首页 > 教程 > WordPress教程WordPress教程

wordpress怎么添加自定义按钮并导出csv

陈仓翼2023-07-03 22:24:30WordPress教程已有人查阅

导读wordpress 后台添加自定义按钮导出csv在wp-admin/edit.php中找到如下代码:

wordpress 后台添加自定义按钮导出csv在wp-admin/edit.php中找到如下代码:

<?php
if ( current_user_can( $post_type_object->cap->create_posts ) ) {
    echo ' <a href="' . esc_url( admin_url( $post_new_file ) ) . '" class="page-title-action">' . esc_html( $post_type_object->labels->add_new ) . '</a>';
}

在上面代码的下一行加入如下代码:

if ($post_type == 'aaa') {
    echo ' <a href="'.esc_url( admin_url('admin-ajax.php?action=export_permanent_csv')).'" class="page-title-action">CSVをエクスポート</a>';
}

$post_type 是在这个文件的头部获取的文章的类型。
在wp-content/themes/hcr/functions/admin.php

function export_permanent_csv()
{
    $args = array(
            'post_type' => 'aaa',
            'numberposts' => -1,
            'meta_key' => 'mark_id',
            'orderby' => 'meta_value_num',
            'order' => 'ASC',
            );
    $posts = get_posts($args);
    if (empty($posts)) {
        return;
    }
    $noNumber = 1;
    foreach ($posts as $post) {
        $metaData = get_post_meta($post->ID);
        $data = [
            $metaData['mark_id'][0],
            $noNumber,
            $post->post_title,
            $metaData['prmnnt_address'][0],
            $metaData['prmnnt_tel'][0],
            $metaData['prmnnt_fax'][0],
            $metaData['prmnnt_site'][0],
            $metaData['prmnnt_time'][0],
            $metaData['prmnnt_closing'][0],
            $metaData['prmnnt_service'][0],
            $metaData['prmnnt_class'][0],
            $post->post_type,
        ];
        $csv_output .= '"'.implode('","', $data).'"'."n";
        $noNumber++;
    }
    $csv_output .= "n";
    $filename = $file."_".date("Ymd", time());
    header("Content-type: application/vnd.ms-excel");
    header("Content-disposition: csv" . date("Y-m-d") . ".csv");
    header("Content-disposition: filename=".$filename.".csv");
    print $csv_output;
    exit;

}
add_action('wp_ajax_export_permanent_csv', 'export_permanent_csv');

本文标签:

很赞哦! ()

留言与评论 (共有 条评论)
验证码:

相关标签