Advertisement
A_God

Plog

Aug 25th, 2023
802
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.80 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Plugin Name: My Resees Plugin
  4.  * Description: A plugin to import and display CSV data.
  5.  * Version: 1.0
  6.  * Author: Your Name
  7.  */
  8.  
  9. // Custom Post Type for Top Members
  10. function create_top_members_post_type() {
  11.     register_post_type('top_members', array(
  12.         'labels' => array(
  13.             'name' => __('Top Members'),
  14.             'singular_name' => __('Top Member')
  15.         ),
  16.         'public' => true,
  17.         'has_archive' => true,
  18.         'supports' => array('title', 'editor', 'thumbnail') // Added 'thumbnail'
  19.     ));
  20. }
  21. add_action('init', 'create_top_members_post_type');
  22.  
  23.  
  24. // Admin Menu
  25. function directory_plugin_menu() {
  26.     add_menu_page('Directory Plugin', 'Directory Plugin', 'manage_options', 'directory_plugin', 'import_data_page', 'dashicons-list-view');
  27. }
  28. add_action('admin_menu', 'directory_plugin_menu');
  29.  
  30. // Import Data Page
  31. function import_data_page() {
  32.     // CSV Upload Form
  33.     ?>
  34.     <form method="post" enctype="multipart/form-data">
  35.         <label for="csv_file">Upload CSV File:</label>
  36.         <input type="file" name="csv_file" accept=".csv" required>
  37.         <input type="submit" name="submit" value="Import">
  38.     </form>
  39.     <?php
  40.  
  41.     // Handle CSV file upload and save data as posts
  42.     if (isset($_POST['submit']) && $_FILES['csv_file']['type'] == 'text/csv') {
  43.         $csv_file = $_FILES['csv_file'];
  44.         $handle = fopen($csv_file['tmp_name'], 'r');
  45.        
  46.         while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
  47.             $post_id = wp_insert_post(array(
  48.                 'post_title'    => wp_strip_all_tags($data[0]),
  49.                 'post_type'     => 'top_members',
  50.                 'post_status'   => 'publish'
  51.             ));
  52.             add_post_meta($post_id, 'mobile_number', $data[1]);
  53.             add_post_meta($post_id, 'email', $data[2]);
  54.             add_post_meta($post_id, 'category', $data[3]);
  55.  
  56.             // Handle profile photo
  57.             if (!empty($data[4])) {
  58.                 $image_url  = $data[4];
  59.                 $upload_dir = wp_upload_dir();
  60.                 $image_data = file_get_contents($image_url);
  61.  
  62.                 $filename   = basename($image_url);
  63.                 if (wp_mkdir_p($upload_dir['path'])) {
  64.                     $file = $upload_dir['path'] . '/' . $filename;
  65.                 } else {
  66.                     $file = $upload_dir['basedir'] . '/' . $filename;
  67.                 }
  68.  
  69.                 file_put_contents($file, $image_data);
  70.  
  71.                 $wp_filetype = wp_check_filetype($filename, null);
  72.                 $attachment  = array(
  73.                     'post_mime_type' => $wp_filetype['type'],
  74.                     'post_title'     => sanitize_file_name($filename),
  75.                     'post_content'   => '',
  76.                     'post_status'    => 'inherit'
  77.                 );
  78.  
  79.                 $attach_id = wp_insert_attachment($attachment, $file, $post_id);
  80.                 require_once(ABSPATH . 'wp-admin/includes/image.php');
  81.                 $attach_data = wp_generate_attachment_metadata($attach_id, $file);
  82.                 wp_update_attachment_metadata($attach_id, $attach_data);
  83.                 set_post_thumbnail($post_id, $attach_id);
  84.             }
  85.         }
  86.        
  87.         echo '<p>Data imported successfully.</p>';
  88.        
  89.         fclose($handle);
  90.     }
  91. }
  92.  
  93. // Enqueue DataTables scripts and styles
  94. function enqueue_datatables_scripts() {
  95.     wp_enqueue_style('datatables-css', 'https://cdn.datatables.net/1.10.25/css/jquery.dataTables.css');
  96.     wp_enqueue_script('jquery');
  97.     wp_enqueue_script('datatables-js', 'https://cdn.datatables.net/1.10.25/js/jquery.dataTables.js', array('jquery'), '', true);
  98. }
  99. add_action('wp_enqueue_scripts', 'enqueue_datatables_scripts');
  100.  
  101. // Shortcode to display top members
  102. function display_top_members() {
  103. $query = new WP_Query(array('post_type' => 'top_members'));
  104. $output = '<div class="member-table-container"><table id="top-members-table" class="display"><thead><tr><th>Profile Photo</th><th>Name</th><th>Mobile Number</th><th>Email</th><th>Category</th></tr></thead><tbody>';
  105. while ($query->have_posts()) {
  106.     $query->the_post();
  107.     $profile_photo = get_the_post_thumbnail(get_the_ID(), 'thumbnail');
  108.     $name = get_the_title();
  109.     $mobile_number = get_post_meta(get_the_ID(), 'mobile_number', true);
  110.     $email = get_post_meta(get_the_ID(), 'email', true);
  111.     $category = get_post_meta(get_the_ID(), 'category', true);
  112.  
  113.     $output .= "<tr><td class='profile-photo'>$profile_photo</td><td>$name</td><td>$mobile_number</td><td>$email</td><td>$category</td></tr>";
  114. }
  115. $output .= '</tbody></table></div>';
  116. $output .= "<script>jQuery(document).ready(function() { jQuery('#top-members-table').DataTable(); });</script>";
  117. wp_reset_postdata();
  118.  
  119.     return $output;
  120. }
  121. add_shortcode('top_members', 'display_top_members');
  122. ?>
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement