Home > CMS Tutorial > WordPress > body text

Detailed explanation of how to add custom buttons and export csv in wordpress

藏色散人
Release: 2021-09-11 17:18:45
forward
3136 people have browsed it

The following tutorial column of WordPress will introduce to you how to add custom buttons and export csv in the WordPress background. I hope it will be helpful to friends in need!

Detailed explanation of how to add custom buttons and export csv in wordpress

Wordpress background adds a custom button to export csv

Find the following code in wp-admin/edit.php :

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

Add the following code to the next line of the above code:

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

$post_type is the type of article obtained from the header of this file.
In 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');
Copy after login

The above is the detailed content of Detailed explanation of how to add custom buttons and export csv in wordpress. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:wordpress
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template