Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* WP Admin Dashboard > Export Metros DB */
- add_action( 'admin_menu', 'metros_export_db' );
- function metros_export_db(){
- add_menu_page(
- 'Export Metros CSV',
- 'Metros CSV',
- 'manage_options',
- 'metros-csv',
- 'metros_csv_export',
- 'dashicons-external'
- );
- }
- function metros_csv_export() {
- global $title;
- ?>
- <div class="wrap">
- <h1><?php echo $title; ?></h1>
- <h2>Easily export all the Metros with ID's from Database.</h2>
- <p class="description">The outcome will be: Metro ID, Metro Name, Metro URL.</p>
- <?php
- if(isset($_POST['submit'])) {
- $terms = get_terms(
- array(
- 'taxonomy' => 'metro',
- 'hide_empty' => false,
- )
- );
- $path = wp_upload_dir(); // or where ever you want the file to go
- $date = date("d-m-Y");
- $outstream = fopen($path['path']."/metros" . $date . ".csv", "w"); // the file name you choose
- // Set first fields
- $fields = array('Metro ID', 'Metro Name', 'Metro URL'); // the user information you want in the csv file
- fputcsv($outstream, $fields); //creates the first line in the csv file
- if ( ! empty( $terms ) && is_array( $terms ) ) {
- foreach ( $terms as $term ) {
- fputcsv($outstream, array($term->term_id,html_entity_decode($term->name),get_term_link( $term ) )); //output the user info line to the csv file
- }
- }
- fclose($outstream);
- echo '<br><a href="'.$path['url'].'/metros' . $date . '.csv" style="font-size:16px;color:green;font-weight:bold;">Export is Done! Download here</a>'; //make a link to the file so the user can download.
- }
- ?>
- <form action="" method="post">
- <table class="form-table">
- <tbody>
- <tr class="row">
- <td style="padding:15px 0;">
- <input type="submit" name="submit" id="submit" class="button button-primary" value="Export Metros CSV">
- </td>
- </tr>
- </tbody>
- </table>
- </form>
- </div>
- <?php }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement