fork download
  1. // Index contact form
  2. add_action('wp_ajax_my_action', 'data_fetch');
  3. add_action('wp_ajax_nopriv_my_action', 'data_fetch');
  4. function data_fetch() {
  5.  
  6. $form_errors = array();
  7.  
  8. if( isset($_POST['submit_form']) ) {
  9.  
  10. $name = $_POST['fullname'];
  11. $email = $_POST['email'];
  12. $message = $_POST['message'];
  13. $file = $_POST['file_field'];
  14.  
  15. $subject = 'Z formularza na stronie';
  16.  
  17. $content = "<html>
  18. <head>
  19. <style>
  20. h3 {
  21. text-transform: uppercase;
  22. padding: 0 0 15px 0;
  23. margin: 0;
  24. font-size: 16px;
  25. }
  26. h4 {
  27. font-size: 12px;
  28. line-height: 16px;
  29. padding: 0;
  30. margin: 10px 0 0 0;
  31. font-weight: 800;
  32. }
  33. p {
  34. font-size: 12px;
  35. line-height: 16px;
  36. padding: 0;
  37. margin: 0;
  38. }
  39. </style>
  40. </head>
  41. <body>
  42. <h4>WIADOMOŚĆ Z FORMULARZA NA STRONIE</h4>
  43. <h4>NADAWCA: </h4>
  44. <p>$name, $email</p>
  45. <h4>TREŚĆ: </h4>
  46. <p class='message'>$message</p></br>
  47. </body>
  48. </html>";
  49.  
  50. foreach ($fields as $field) {
  51. if (isset($_POST[$field])) $posted[$field] = stripslashes(trim($_POST[$field])); else $posted[$field] = '';
  52. }
  53.  
  54. $errors = array_filter($form_errors);
  55.  
  56. if (empty($errors)) {
  57. if ( ! function_exists( 'wp_handle_upload' ) ) {
  58. require_once( ABSPATH . 'wp-admin/includes/file.php' );
  59. }
  60.  
  61. $uploadedfile = $_FILES['attachmentFile'];
  62. $upload_overrides = array( 'test_form' => false );
  63. $movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
  64.  
  65. if ( $movefile && ! isset( $movefile['error'] ) ) {
  66. $movefile['url'];
  67. }
  68.  
  69. $to = get_option('admin_email');
  70. $attachments = array($movefile['file']);
  71.  
  72. $headers = array('Content-Type: text/html; charset=UTF-8',
  73. 'From: '.$name.' <'.$email.'>',
  74. 'Reply-To: '.$name.' <'.$email.'>');
  75.  
  76. if(wp_mail($to , $subject , $content , $headers , $attachments)){
  77. echo '<div id="message_ok">Wiadomość wysłana :)</div> ' ;
  78. }
  79. else {
  80. echo '<div id="message_error">Niestety, wiadomość nie została wysłana, spróbuj jeszcze raz.</div> ' ;
  81. }
  82.  
  83. }
  84. }
  85. wp_die();
  86. }
  87.  
  88. wp_register_script ( 'contact_form', get_template_directory_uri() . '/js/contact_form.js' );
  89. wp_enqueue_script ( 'contact_form' );
Success #stdin #stdout 0s 83072KB
stdin
Standard input is empty
stdout
// Index contact form
    add_action('wp_ajax_my_action', 'data_fetch');
    add_action('wp_ajax_nopriv_my_action', 'data_fetch');
    function data_fetch() {
        
    $form_errors = array();

    if( isset($_POST['submit_form']) )  {

    $name = $_POST['fullname'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $file = $_POST['file_field'];

    $subject = 'Z formularza na stronie';

    $content = "<html>
                    <head>
                        <style>
                            h3 {
                                text-transform: uppercase;
                                padding: 0 0 15px 0;
                                margin: 0;
                                font-size: 16px;
                            }
                            h4 {
                                font-size: 12px;
                                line-height: 16px;
                                padding: 0;
                                margin: 10px 0 0 0;
                                font-weight: 800;
                            }
                            p {
                                font-size: 12px;
                                line-height: 16px;
                                padding: 0;
                                margin: 0;
                            }
                        </style>
                    </head>
                        <body>
                                <h4>WIADOMOŚĆ Z FORMULARZA NA STRONIE</h4>
                                <h4>NADAWCA: </h4>
                                <p>$name, $email</p>
                                <h4>TREŚĆ: </h4>
                                <p class='message'>$message</p></br>
                        </body>
                </html>";
    
    foreach ($fields as $field) {
        if (isset($_POST[$field])) $posted[$field] = stripslashes(trim($_POST[$field])); else $posted[$field] = '';
    }
            
    $errors = array_filter($form_errors);

    if (empty($errors)) {   
        if ( ! function_exists( 'wp_handle_upload' ) ) {
            require_once( ABSPATH . 'wp-admin/includes/file.php' );
        }

        $uploadedfile = $_FILES['attachmentFile'];
        $upload_overrides = array( 'test_form' => false );
        $movefile = wp_handle_upload( $uploadedfile, $upload_overrides );

        if ( $movefile && ! isset( $movefile['error'] ) ) {         
            $movefile['url'];
        }

        $to = get_option('admin_email');
        $attachments = array($movefile['file']);

        $headers = array('Content-Type: text/html; charset=UTF-8',
                    'From: '.$name.' <'.$email.'>',
                    'Reply-To: '.$name.' <'.$email.'>');

        if(wp_mail($to , $subject , $content , $headers , $attachments)){
            echo '<div id="message_ok">Wiadomość wysłana :)</div> ' ;
        }
        else {
            echo '<div id="message_error">Niestety, wiadomość nie została wysłana, spróbuj jeszcze raz.</div> ' ; 
        }
        
        }
    }
    wp_die();
}

    wp_register_script ( 'contact_form', get_template_directory_uri() . '/js/contact_form.js' );
    wp_enqueue_script ( 'contact_form' );