Wednesday, 15 June 2011

php - How to get a wordpress post custom filed value and assign it to a variable? -



php - How to get a wordpress post custom filed value and assign it to a variable? -

i creating form sends form's details email.

my custom field is:

my function.php

function gethotel_contact_number() { global $wp_query; $postid = $wp_query->post->id; $gethotel_contact_number = get_post_meta($postid, 'hotel_contact_number', true); $gethotel_contact_number; }

and form is:

<?php $errors = array(); if (isset($_post["submit"])) { $to = "myemail@email.com"; $subject = "this subject"; $hotel = get_permalink(); $hotel_contact_nmbr = gethotel_contact_number(); $headers = array('from: '.$_post['sendername'].' <'.$_post['senderemail'].'>'); //check name , create sure isn't blank/empty string. if(empty($sender)){ //blank string, add together error $errors array. $errors['sendername'] = "please come in name!"; } if(empty($senderemail)){ //blank string, add together error $errors array. $errors['senderemail'] = "please come in email!"; } $mailbody = "<h3>hotel details</h3><br/> $hotel<br/> $hotel_contact_nmbr<br/>" $mail_sent = wp_mail( $to, $subject, $mailbody, $headers ); } if ($mail_sent) { ?> <h1 style="color: #007f00;">request sent.</h1> <?php } else { ?> <form id="" name="" action="<?php echo get_permalink(); ?>" method="post"> <div class="label-input-wrapper"> <div class="form-label">name</div> <div class="form-input"> <input type="text" name="sendername" value="<?php if(!empty($errors)) { echo $sender;} ?>" /> <div class="error-msg"> <?php if(isset($errors['sendername'])) { echo '<span style="color: red">'.$errors['sendername'].'</span>'; } ?> </div> </div> </div> <div class="label-input-wrapper"> <div class="form-label">e-mail</div> <div class="form-input"> <input type="email" name="senderemail" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$" required value="<?php if(!empty($errors)) { echo $senderemail;} ?>" /> <div class="error-msg"> <?php if(isset($errors['senderemail'])) { echo '<span style="color: red">'.$errors['senderemail'].'</span>'; } ?> </div> </div> </div> <input type="submit" value="submit" name="submit"> </form> <?php } ?>

but when form submitted getting blank value hotel contact number. doing wrong? how can proper custom filed value when form submitted?

anyhow it's working fine if utilize hidden fields mentioned here: have @ bottom area of question mention hidden fields.

change function next code:

function gethotel_contact_number() { global $wp_query; $postid = $wp_query->post->id; $gethotel_contact_number = get_post_meta($postid, 'hotel_contact_number', true); homecoming $gethotel_contact_number; }

php wordpress forms email

No comments:

Post a Comment