简体   繁体   中英

How do I include PHP in my html form on a wordpress page?

I made an HTML form inside the Pages section of WordPress, but I need to use the PHP timestamp to submit a hidden field of the current time.

How can I do this? Do I use the "span" tag or something? I think that's for javascript. I just need to be able to submit a timestamp in a hidden file though, so maybe I should use javascript?

Try something like this, if your form is defined in a WordPress template:

<input type="hidden" id="timestamp" value="<?php echo time();?>" />

That will output the timestamp in epoch format. If you want a formatted value, use this for 24 hour formatting:

<input type="hidden" id="timestamp" value="<?php echo date('H:i:s');?>" />

(see http://php.net/manual/en/function.date.php for information regarding formatting options)

I have to agree with briosheje, however: you'd typically want to read the time on the server, after the form has been posted. If not, keep in mind the value that's posted will be stale by a few seconds, possibly minutes (possibly more!)

Wordpress will not parse PHP code. There are plugins that can do it if you desire, but they are less than optimal. Instead, try something like this...

In your functions.php file:

add_shortcode('timestamp', 'my_add_timestamp');

function my_add_timestamp($atts){
    return time();
}

In the page with your form:

<input type="hidden" id="timestamp" value="[timestamp]" />

This will tell WordPress to parse the content of your page/post and replace the [timestamp] with the function call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM