Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. function api_endpoints() {
  2. register_rest_route('sendform', '/kontakt/', array(
  3. 'methods' => 'POST',
  4. 'callback' => 'send_contact_form'
  5. ));
  6. }
  7.  
  8. add_action( 'rest_api_init', 'api_endpoints')
  9.  
  10. function send_contact_form( $request ){
  11. $full_name = sanitize_text_field( trim($request['full_name']) );
  12. $email = sanitize_email( trim($request['email']) );
  13. $body = wp_ksest_post( trim($request['body']) );
  14. $errors = [];
  15. if ( empty( $full_name)) {
  16. $errors[] = "Podaj Imię i Nazwisko"
  17. }
  18. if ( empty( $email) || ! filter_var( variable: $email, filter: FILTER_VALIDATE_EMAIL)) {
  19. $errors[] = "Podaj adres E-mail"
  20. }
  21. if ( empty( $body)) {
  22. $errors[] = "Napisz Wiadomość"
  23. }
  24. if ( count( $errors )) {
  25. return new WP_Error( code: 'contact_form_errors', message: $errors, [ 'status' => 422 ])
  26. }
  27.  
  28. $message = "Imię i nazwisko: {$full_name}. <br> Od: {$email}. <br> Wiadomosc: $body";
  29. $headers = ['Content-Type: text/html; charset=UTF-8']
  30. wp_mail($email, 'contact_form', $message, $headers)
  31. return 'Sucess;'
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement