Why I could not assign a wordpress function to a variable?

I am creating a form that sends the form’s details to my email. Here is my code:

<?PHP
$errors = array();
if (isset($_POST["submit"])) {
    $to = "myemail@email.com";
    $subject = "This is my subject";    
    $hotel = echo get_permalink();
    $headers = array('From: '.$_POST['sendername'].' <'.$_POST['senderEmail'].'>');
    //Check the name and make sure that it isn't a blank/empty string.
        if(empty($sender)){
            //Blank string, add error to $errors array.        
            $errors['sendername'] = "Please enter your name!";
        }
        if(empty($senderEmail)){
            //Blank string, add error to $errors array.        
            $errors['senderEmail'] = "Please enter your email!";
        }
    $mailBody = "<h3>Hotel Details</h3>"

    $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
}  
?>

If I submit the form it shows a parse error an unexpected echo variable for the line $hotel = echo get_permalink();. Can’t I assign the wordpress function get_permalink(); value to a variable?

Read More

If I change the line from $hotel = echo get_permalink(); to $hotel = permalink(); it says Fatal error: Call to undefined function permalink()

But it works if I put a hidden field into the form:
<input type="hidden" name="hotel_url" value="<?php echo get_permalink(); ?>" />
and getting the value like this: $hotel = isset($_POST["hotel_url"]);
In this way I am able to assign that function’s value into a variable and it send me the email with the correct page URL.

But I do not want to use hidden filed for this. Because there are some sensitive information also I want to send to my email when the form is submitted. If I do that using the above working method anyone can see the values if the see the source code.

So how can I assign wordpress functions’ value to a variable?

Related posts

Leave a Reply

1 comment