3

I am building a set of forms shortcodes in Wordpress. I have given up trying to get the form processing script into a shortcode and am just going to make it available to all pages.

There is however an echo that puts out the response of the form which I want to put into it's own shortcode.

<?php echo $response; ?>

This needs to go at the top of the form so that validation messages appear in the right place.

Total form processing code:

<?php

  //response generation function

  $response = "";

  //function to generate response
  function my_contact_form_generate_response($type, $message){

    global $response;

    if($type == "success") $response = "<div class='callout success'><p>{$message}</p></div>";
    else $response = "<div class='callout error'><p>{$message}</p></div>";
  }

  // response messages
    $not_human       = "Please fill out the human verification field to prove you are not a spam robot. A number 2 will do the job nicely.";
    $missing_content = "It looks like we are missing something. Please chek the form below.";
    $email_invalid   = "That email Address doesn't look quite right.";
    $message_unsent  = "Your message wasn't sent. Please have another go. If it still doesn't work then please call us using the number supplied.";
    $message_sent    = "Thank you for your enquiry. We will be in contact shortly.";

  // user posted variables
    $business_name = $_POST['message_business_name'];
    $first_name = $_POST['message_first_name'];
    $last_name = $_POST['message_last_name'];
    $email = $_POST['message_email'];
    $phone = $_POST['message_phone'];
    $human = $_POST['message_human'];
    $location = $_POST['location'];
    $message_opt_in = $_POST['message_opt_in'];

    $optin = "";
    if ($message_opt_in == "on"){
        $optin = "The user has opted in to marketing emails";
    }
    else {
        $optin = "!!!!!!!!!!!!!!!!!!!!!!\n\n The user has NOT opted in to marketing emails! \n\n!!!!!!!!!!!!!!!!!!!!!!\n\n";
    }

    $body = "$optin \n\n\n\n This message was sent from xxx.com $location\n\nBusiness name: $business_name \nName: $first_name \nName: $last_name \nEmail: $email \nPhone: $phone";

  //php mailer variables
    $to ="[email protected]";
    $subject = "Someone sent a message from ".get_bloginfo('name');
    $headers = 'From: '. $email . "\r\n" .
      'Reply-To: ' . $email . "\r\n";
  if(!$human == 0){
    if($human != 2) my_contact_form_generate_response("error", $not_human); //not human!
    else {

      //validate email
      if(!filter_var($email, FILTER_VALIDATE_EMAIL))
        my_contact_form_generate_response("error", $email_invalid);
      else //email is valid
      {
        //validate presence of name and message
        if(empty($business_name) || empty($first_name) || empty($email)){
          my_contact_form_generate_response("error", $missing_content);
        }
        else //ready to go!
        {
          $sent = wp_mail($to, $subject, strip_tags($body), $headers);
          if($sent) my_contact_form_generate_response("success", $message_sent); //message sent!
          else my_contact_form_generate_response("error", $message_unsent); //message wasn't sent
        }
      }
    }
  }
  else if ($_POST['submitted']) my_contact_form_generate_response("error", $missing_content);

?>
                <?php echo $response; ?>

I have tried amongst other things that would probably make a PHP dev laugh.

function form_response(){
    echo $response;
}

function form_response(){
    echo '<?php echo "$response"; ?>';
}

Disclaimer I'm not a PHP developer as you can probably tell.

3 Answers 3

2

In the my_contact_form_generate_response function, you can do return $response; at the very bottom before the closing } for the function.

Then, you can do echo my_contact_form_generate_response($type, $message); and it will echo the return value, which will be $response in this case.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, but I'm still not sure how to make the echo work within a function I can pull as a shortcode.
0

Try to change

else if ($_POST['submitted']) my_contact_form_generate_response("error", $missing_content);

To

else if ($_POST['submitted']){
    $response = 'Some things';
    $response .= my_contact_form_generate_response("error", $missing_content);
    return $response;
}

3 Comments

I tried this without doing the shortcode bit but it didn't output anything also would this still not work within a function pulled by a shortcode?
So to translate... ... else if ($_POST['submitted']){ $response = 'Some things'; echo $response; my_contact_form_generate_response("error", $missing_content); } ?> Then the shortcode function function form_response(){ return $response; } I missing something as the page is now blank below the form part. Also I don't get why you would set $response to a string of random text?
@TheReveller pastebin.com/g50TR7nc You can do with it. Sorry about late reply. xD
0

I had to call $response from global

function form_response(){
    echo $GLOBALS["response"];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.