Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.07 KB | None | 0 0
  1. <?php
  2. /* WP Admin Dashboard > Export Metros DB */
  3. add_action( 'admin_menu', 'metros_export_db' );
  4.  
  5. function metros_export_db(){
  6.     add_menu_page(
  7.         'Export Metros CSV',
  8.         'Metros CSV',
  9.         'manage_options',
  10.         'metros-csv',
  11.         'metros_csv_export',
  12.         'dashicons-external'
  13.     );
  14. }
  15.  
  16. function metros_csv_export() {
  17.     global $title;
  18.     ?>
  19.  
  20.     <div class="wrap">
  21.         <h1><?php echo $title; ?></h1>
  22.         <h2>Easily export all the Metros with ID's from Database.</h2>
  23.         <p class="description">The outcome will be: Metro ID, Metro Name, Metro URL.</p>
  24.  
  25.         <?php
  26.         if(isset($_POST['submit'])) {
  27.  
  28.             $terms = get_terms(
  29.                 array(
  30.                     'taxonomy'   => 'metro',
  31.                     'hide_empty' => false,
  32.                 )
  33.             );
  34.  
  35.             $path = wp_upload_dir();
  36.             $date = date("d-m-Y");
  37.  
  38.             $outstream = fopen($path['path']."/metros" . $date . ".csv", "w");
  39.  
  40.             // Set first fields
  41.             $fields = array('Metro ID', 'Metro Name', 'Metro URL');
  42.  
  43.             fputcsv($outstream, $fields);
  44.  
  45.             if ( ! empty( $terms ) && is_array( $terms ) ) {
  46.  
  47.                 foreach ( $terms as $term ) {
  48.  
  49.                     fputcsv($outstream, array($term->term_id,html_entity_decode($term->name),get_term_link( $term ) ));
  50.  
  51.                 }
  52.  
  53.             }
  54.  
  55.             fclose($outstream);
  56.  
  57.             echo '<br><a href="'.$path['url'].'/metros' . $date . '.csv" style="font-size:16px;color:green;font-weight:bold;">Export is Done! Download here</a>';
  58.  
  59.         }
  60.         ?>
  61.  
  62.         <form action="" method="post">
  63.             <table class="form-table">
  64.                 <tbody>
  65.                 <tr class="row">
  66.                     <td style="padding:15px 0;">
  67.                         <input type="submit" name="submit" id="submit" class="button button-primary" value="Export Metros CSV">
  68.                     </td>
  69.                 </tr>
  70.                 </tbody>
  71.             </table>
  72.         </form>
  73.     </div>
  74.  
  75. <?php }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement