Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Plugin Name: My Resees Plugin
- * Description: A plugin to import and display CSV data.
- * Version: 1.0
- * Author: Your Name
- */
- // Custom Post Type for Top Members
- function create_top_members_post_type() {
- register_post_type('top_members', array(
- 'labels' => array(
- 'name' => __('Top Members'),
- 'singular_name' => __('Top Member')
- ),
- 'public' => true,
- 'has_archive' => true,
- 'supports' => array('title', 'editor', 'thumbnail') // Added 'thumbnail'
- ));
- }
- add_action('init', 'create_top_members_post_type');
- // Admin Menu
- function directory_plugin_menu() {
- add_menu_page('Directory Plugin', 'Directory Plugin', 'manage_options', 'directory_plugin', 'import_data_page', 'dashicons-list-view');
- }
- add_action('admin_menu', 'directory_plugin_menu');
- // Import Data Page
- function import_data_page() {
- // CSV Upload Form
- ?>
- <form method="post" enctype="multipart/form-data">
- <label for="csv_file">Upload CSV File:</label>
- <input type="file" name="csv_file" accept=".csv" required>
- <input type="submit" name="submit" value="Import">
- </form>
- <?php
- // Handle CSV file upload and save data as posts
- if (isset($_POST['submit']) && $_FILES['csv_file']['type'] == 'text/csv') {
- $csv_file = $_FILES['csv_file'];
- $handle = fopen($csv_file['tmp_name'], 'r');
- while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
- $post_id = wp_insert_post(array(
- 'post_title' => wp_strip_all_tags($data[0]),
- 'post_type' => 'top_members',
- 'post_status' => 'publish'
- ));
- add_post_meta($post_id, 'mobile_number', $data[1]);
- add_post_meta($post_id, 'email', $data[2]);
- add_post_meta($post_id, 'category', $data[3]);
- // Handle profile photo
- if (!empty($data[4])) {
- $image_url = $data[4];
- $upload_dir = wp_upload_dir();
- $image_data = file_get_contents($image_url);
- $filename = basename($image_url);
- if (wp_mkdir_p($upload_dir['path'])) {
- $file = $upload_dir['path'] . '/' . $filename;
- } else {
- $file = $upload_dir['basedir'] . '/' . $filename;
- }
- file_put_contents($file, $image_data);
- $wp_filetype = wp_check_filetype($filename, null);
- $attachment = array(
- 'post_mime_type' => $wp_filetype['type'],
- 'post_title' => sanitize_file_name($filename),
- 'post_content' => '',
- 'post_status' => 'inherit'
- );
- $attach_id = wp_insert_attachment($attachment, $file, $post_id);
- require_once(ABSPATH . 'wp-admin/includes/image.php');
- $attach_data = wp_generate_attachment_metadata($attach_id, $file);
- wp_update_attachment_metadata($attach_id, $attach_data);
- set_post_thumbnail($post_id, $attach_id);
- }
- }
- echo '<p>Data imported successfully.</p>';
- fclose($handle);
- }
- }
- // Enqueue DataTables scripts and styles
- function enqueue_datatables_scripts() {
- wp_enqueue_style('datatables-css', 'https://cdn.datatables.net/1.10.25/css/jquery.dataTables.css');
- wp_enqueue_script('jquery');
- wp_enqueue_script('datatables-js', 'https://cdn.datatables.net/1.10.25/js/jquery.dataTables.js', array('jquery'), '', true);
- }
- add_action('wp_enqueue_scripts', 'enqueue_datatables_scripts');
- // Shortcode to display top members
- function display_top_members() {
- $query = new WP_Query(array('post_type' => 'top_members'));
- $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>';
- while ($query->have_posts()) {
- $query->the_post();
- $profile_photo = get_the_post_thumbnail(get_the_ID(), 'thumbnail');
- $name = get_the_title();
- $mobile_number = get_post_meta(get_the_ID(), 'mobile_number', true);
- $email = get_post_meta(get_the_ID(), 'email', true);
- $category = get_post_meta(get_the_ID(), 'category', true);
- $output .= "<tr><td class='profile-photo'>$profile_photo</td><td>$name</td><td>$mobile_number</td><td>$email</td><td>$category</td></tr>";
- }
- $output .= '</tbody></table></div>';
- $output .= "<script>jQuery(document).ready(function() { jQuery('#top-members-table').DataTable(); });</script>";
- wp_reset_postdata();
- return $output;
- }
- add_shortcode('top_members', 'display_top_members');
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement