簡體   English   中英

在WordPress頁面中添加隱藏字段

[英]Add hidden field in a wordpress page

在頁面中具有自定義html表單:

    <form action="localhost/mail.php" id="contactForm" method="post">
    <ul>
        <li>
    <input type="hidden" name="submitted" value="USERNAME_HERE" /></li>
        <li>
            <button type="submit">Send request</button>
        </li>
    </ul>

</form>

我可以以某種方式將當前登錄的用戶名添加到value = ...嗎? 那么,當用戶單擊“發送請求”按鈕時,我會在電子郵件中收到其用戶名嗎?

嘗試使用[userinfo field =“ user_login”]和:

    <?php global $current_user;
      get_currentuserinfo();

      echo 'Username: ' . $current_user->user_login . "\n";

?>

但是所有的PHP代碼被削減,所以它是無用的:(

請告知我我做錯了。

謝謝!

通常,我使用頁面模板在頁面中使用PHP。 設置好模板后,您可以執行以下操作:

<?php
/*
 * Template Name: Custom Form
 * Description: Form with pre-filled username. 
 */

    global $current_user;
    get_currentuserinfo();
?>

 <form action="localhost/mail.php" id="contactForm" method="post">
 <ul>
    <li>
      <input type="hidden" name="submitted" value="<?php echo $current_user->user_login; ?>" />
    </li>
    <li>
        <button type="submit">Send request</button>
    </li>
</ul>

</form>

您可能需要將page.php從主題復制到模板的名為page-form.php的文件中,然后修改page-form.php。 尋找the_content(); 並在其中添加自定義PHP。

您可以在此處閱讀有關模板的更多信息: http : //codex.wordpress.org/Page_Templates

也許這會有所幫助:

<?php

$user_nameField = $_POST['user_name'];



?>





    <form action="localhost/mail.php" id="contactForm" method="post">
        <ul>
            <li>
        <input  name="user_name" type="hidden"  id="user_name"  value="<?php echo $ulog; ?>"></li>
            <li>
                <button type="submit">Send request</button>
            </li>
        </ul>  
</form>

使用此插件: http : //wordpress.org/plugins/shortcode-exec-php/

您將需要在此插件中創建一個php代碼塊,然后將其作為短代碼放在您的頁面中。

因此您的php代碼將如下所示:

<?php 
      global $current_user;
      get_currentuserinfo();

      $username = $current_user->user_login;

?>
<form action="localhost/mail.php" id="contactForm" method="post">
    <ul>
        <li>
    <input type="hidden" name="submitted" value="<?php echo $username; ?>" /></li>
        <li>
            <button type="submit">Send request</button>
        </li>
    </ul>

</form>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM