Advertisement
Guest User

wordpress form with image upload and store to database

a guest
Nov 1st, 2019
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.00 KB | None | 0 0
  1. <?php
  2. /*
  3.  
  4.  
  5. Plugin Name: My Family
  6. Plugin URI:
  7. Description: Plugin for my Family
  8. Version: 0.1.0
  9. Author: David Saerang
  10. Author URI:
  11. */
  12.  
  13. // disable direct access
  14. if ( ! defined( 'ABSPATH' ) ) {
  15.     exit;
  16. }
  17.  
  18. function family_options_install() {
  19.     global $wpdb;
  20.     $table_name = $wpdb->prefix . 'family_survey';
  21.     $wpdb_collate = $wpdb->collate;
  22.     $sql =
  23.     "CREATE TABLE {$table_name} (
  24.        `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  25.        `nama` varchar(50) DEFAULT NULL,
  26.        `email` varchar(150) DEFAULT NULL,
  27.        `gambar` varchar(50) DEFAULT NULL,
  28.        PRIMARY KEY (`id`)
  29.    )
  30.    COLLATE {$wpdb_collate}";
  31.     require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
  32.     dbDelta( $sql );    
  33. }
  34.  
  35.     // run the install scripts upon plugin activation
  36.     register_activation_hook(__FILE__, 'family_options_install');
  37.    
  38.     //menu items
  39. //  add_action('admin_menu','family_modifymenu');
  40.     function family_modifymenu() {
  41.        
  42.         //this is the main item for the menu
  43.         add_menu_page('Family', //page title
  44.         'Family', //menu title
  45.         'manage_options', //capabilities
  46.         'family_list', //menu slug
  47.         'family_list' //function
  48.         );
  49.        
  50.         //this is a submenu
  51.         add_submenu_page('family_list', //parent slug
  52.         'Rekapan', //page title
  53.         'Rekap Family', //menu title
  54.         'manage_options', //capability
  55.         'lihat_rekap_family', //menu slug
  56.         'lihat_rekap_family'); //function
  57.        
  58.         //this submenu is HIDDEN, however, we need to add it anyways
  59.         add_submenu_page(null, //parent slug
  60.         'Update Family', //page title
  61.         'Update', //menu title
  62.         'manage_options', //capability
  63.         'update_rekap_family', //menu slug
  64.         'update_rekap_family'); //function
  65.     }
  66.  
  67.  
  68. //  define('ROOTDIR', plugin_dir_path(__FILE__));
  69.  
  70.     function fam_subscriber_to_uploads() {
  71.         $subscriber = get_role('subscriber');
  72.  
  73.         if ( ! $subscriber->has_cap('upload_files') ) {
  74.             $subscriber->add_cap('upload_files');
  75.         }
  76.     }
  77.    
  78.     add_action('admin_init', 'fam_subscriber_to_uploads');
  79.  
  80.         function fam_input_form(){
  81.            
  82.                 $nama = $_POST["user_name"];
  83.                 $email = $_POST["user_email"];
  84.                 $gambar = $_POST["async-upload"];
  85.             //insert
  86.                 if (isset($_POST['insert'])) {
  87.                 global $wpdb;              
  88.                 $table_name = $wpdb->prefix . 'family_survey';
  89.  
  90.                     $wpdb->insert(
  91.                         $table_name,
  92.                         array(
  93.                             'nama' => $nama,
  94.                             'email' => $email,
  95.                             'gambar' => $gambar,
  96.                         ), 
  97.                         array('%s','%s','%s')      
  98.                     );
  99.     }
  100.             ob_start();
  101.             ?>
  102.                 <?php if ( is_user_logged_in() ): ?>
  103.                     <p class="form-notice"></p>
  104.                     <form action="" method="post" class="image-form">
  105.                         <?php wp_nonce_field('image-submission'); ?>
  106.                         <p><input type="text" name="user_name" placeholder="Your Name" required></p>
  107.                         <p><input type="email" name="user_email" placeholder="Your Email Address" required></p>
  108.                         <p class="image-notice"></p>
  109.                         <p><input type="file" name="async-upload" class="image-file" accept="image/*" required></p>
  110.                         <input type="hidden" name="image_id">
  111.                         <input type="hidden" name="action" value="image_submission">
  112.                         <div class="image-preview"></div>
  113.                         <hr>
  114.                         <p><input type="submit" name="insert" value="Submit"></p>
  115.                     </form>
  116.                 <?php else: ?>
  117.                     <p>Please <a href="<?php echo esc_url( wp_login_url( get_permalink() ) ); ?>">login</a> first to submit your image.</p>
  118.                 <?php endif; ?>
  119.             <?php
  120.             $output = ob_get_clean();
  121.             return $output;
  122.         }
  123.         add_shortcode('family_form', 'fam_input_form');
  124.  
  125.  
  126.     function fam_load_scripts() {
  127.         wp_enqueue_script('image-form-js', plugin_dir_url( __FILE__ ) . 'js/myfam-public.js', array('jquery'), '0.1.0', true);
  128.  
  129.         $data = array(
  130.                     'upload_url' => admin_url('async-upload.php'),
  131.                     'ajax_url'   => admin_url('admin-ajax.php'),
  132.                     'nonce'      => wp_create_nonce('media-form')
  133.                 );
  134.  
  135.         wp_localize_script( 'image-form-js', 'su_config', $data );
  136.     }
  137.     add_action('wp_enqueue_scripts', 'fam_load_scripts');
  138.  
  139.         function fam_image_submission_cb() {
  140.             check_ajax_referer('image-submission');
  141.  
  142.             $user_name  = filter_var( $_POST['user_name'],FILTER_SANITIZE_STRING );
  143.             $user_email = filter_var( $_POST['user_email'], FILTER_VALIDATE_EMAIL );
  144.             $image_id   = filter_var( $_POST['image_id'], FILTER_VALIDATE_INT );
  145.            
  146.  
  147.             if ( ! ( $user_name && $user_email && $image_id ) ) {
  148.                 wp_send_json_error( array('msg' => 'Validation failed. Please try again later.') );
  149.             }
  150.  
  151.             $to      = get_option('davidsaerang@gmail.com');
  152.             $subject = 'New image submission!';
  153.             $message = sprintf(
  154.                             'New image submission from %s (%s). Link: %s',
  155.                             $user_name,
  156.                             $user_email,
  157.                             wp_get_attachment_url( $image_id )
  158.                         );
  159.  
  160.             $result = wp_mail( $to, $subject, $message );
  161.  
  162.             if ( $result ) {
  163.                 wp_send_json_error( array('msg' => 'Email failed to send. Please try again later.') );
  164.                
  165.             } else {
  166.                
  167.                 wp_send_json_success( array('msg' => 'Your submission successfully sent.') );
  168.                
  169.                
  170.                
  171.             }
  172.                
  173.  
  174.     }
  175.  
  176.  
  177. add_action('wp_ajax_image_submission', 'fam_image_submission_cb');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement