Skip to main content

PHP-funktion

Tilføj denne funktion til jeres functions.php:
function replied_submit($name, $email, $phone, $message, $extra = []) {
  $body = array_merge([
    'name'    => $name,
    'email'   => $email,
    'phone'   => $phone,
    'message' => $message,
  ], $extra);

  $response = wp_remote_post(
    'https://www.replied.dk/api/v1/submit/JERES_API_NØGLE',
    [
      'headers' => ['Content-Type' => 'application/json'],
      'body'    => json_encode($body),
      'timeout' => 10,
    ]
  );

  if (is_wp_error($response)) {
    return false;
  }

  return wp_remote_retrieve_response_code($response) === 200;
}

Brug med Contact Form 7

Tilføj denne hook der sender data til Replied når en CF7-formular submittes:
add_action('wpcf7_mail_sent', function($contact_form) {
  $submission = WPCF7_Submission::get_instance();
  if (!$submission) return;

  $data = $submission->get_posted_data();

  replied_submit(
    $data['your-name'] ?? '',
    $data['your-email'] ?? '',
    $data['your-phone'] ?? '',
    $data['your-message'] ?? '',
    ['kilde' => 'Contact Form 7']
  );
});

Brug med WPForms

add_action('wpforms_process_complete', function($fields, $entry, $form_data) {
  $name = '';
  $email = '';
  $message = '';

  foreach ($fields as $field) {
    if ($field['type'] === 'name')    $name = $field['value'];
    if ($field['type'] === 'email')   $email = $field['value'];
    if ($field['type'] === 'textarea') $message = $field['value'];
  }

  replied_submit($name, $email, '', $message, [
    'kilde'   => 'WPForms',
    'form_id' => $form_data['id'],
  ]);
}, 10, 3);

Shortcode-formular

Hvis I vil lave jeres egen formular som WordPress shortcode:
add_shortcode('replied_form', function() {
  ob_start();
  ?>
  <form method="post" action="">
    <?php wp_nonce_field('replied_submit', 'replied_nonce'); ?>
    <input type="text" name="replied_name" placeholder="Navn" required />
    <input type="email" name="replied_email" placeholder="Email" required />
    <textarea name="replied_message" placeholder="Besked" required></textarea>
    <button type="submit" name="replied_send">Send</button>
  </form>
  <?php

  if (isset($_POST['replied_send']) && wp_verify_nonce($_POST['replied_nonce'], 'replied_submit')) {
    $result = replied_submit(
      sanitize_text_field($_POST['replied_name']),
      sanitize_email($_POST['replied_email']),
      '',
      sanitize_textarea_field($_POST['replied_message'])
    );

    if ($result) {
      echo '<p>Tak for din henvendelse!</p>';
    } else {
      echo '<p>Noget gik galt. Prøv igen.</p>';
    }
  }

  return ob_get_clean();
});
Brug shortcoden i en side: [replied_form]