Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function api_endpoints() {
- register_rest_route('sendform', '/kontakt/', array(
- 'methods' => 'POST',
- 'callback' => 'send_contact_form'
- ));
- }
- add_action( 'rest_api_init', 'api_endpoints')
- function send_contact_form( $request ){
- $full_name = sanitize_text_field( trim($request['full_name']) );
- $email = sanitize_email( trim($request['email']) );
- $body = wp_ksest_post( trim($request['body']) );
- $errors = [];
- if ( empty( $full_name)) {
- $errors[] = "Podaj Imię i Nazwisko"
- }
- if ( empty( $email) || ! filter_var( variable: $email, filter: FILTER_VALIDATE_EMAIL)) {
- $errors[] = "Podaj adres E-mail"
- }
- if ( empty( $body)) {
- $errors[] = "Napisz Wiadomość"
- }
- if ( count( $errors )) {
- return new WP_Error( code: 'contact_form_errors', message: $errors, [ 'status' => 422 ])
- }
- $message = "Imię i nazwisko: {$full_name}. <br> Od: {$email}. <br> Wiadomosc: $body";
- $headers = ['Content-Type: text/html; charset=UTF-8']
- wp_mail($email, 'contact_form', $message, $headers)
- return 'Sucess;'
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement