dousong3760 2017-01-25 19:47
浏览 73

Wordpress - 为前端用户使用“Flash消息”的最佳方式

Some background: I developed a wordpress plugin with a form in it. I need a way to notify the user if they mess up filling out the form. My initial inclination was php session variables. I added the various pieces of code to my plugin to make this work including the session_start() at the top of my header which broke things. So i started doing research on the best way to show messages to the user.

I can use cookies as an alternative to php sessions but my question is:

What is the best way to set up front-end messages inside of Wordpress? I am new to wordpress mind you. I heard of Wordpress global variables? Does Wordpress provide something like global variables I can set which can be accessed anywhere? That would work. Is there some plugin i should look into (I would hate to bloat this). Are cookies the best route?

Here is a snippet of code that may illustrate what I am trying to accomplish:

Front End Form (shortcode)

  <form method='POST'class="kb_donation_form" action="<?= // my action ?>">

            <div class="form-group">
                <label for="kb_first_name">First Name</label>
                <input type="text" class="form-control" id="kb_first_name" name="kb_first_name" placeholder="First Name">
            </div>

            <div class="form-group">
                <label for="kb_last_name">Last Name</label>
                <input type="text" class="form-control" id="kb_last_name" name="kb_last_name" placeholder="First Name">
            </div>
            <div class="form-group">
                <label for="kb_email">Email</label>
                <input type="email" class="form-control" id="kb_email" name="kb_email" placeholder="Email Address">
            </div>

            <div class="form-group">
                <label for="kb_grad_year">Graduation Year</label>
                <select id="kb_grad_year" name="kb_grad_year" class="form-control">
                    <option value="friend">I am just a friend.</option>
                    <option value="1950">1950</option>
                </select>
            </div>

            <div class="form-group">
                <label for="kb_donation_amount">Donation Amount</label>
                <input type="text" class="form-control" id="kb_donation_amount" name="kb_donation_amount" placeholder="100.00">
            </div>

            <div class="form-group">
                <input type="submit" name="submit" value="Make Donation" class="btn btn-default">
            </div>
        </form>

The post handler code:

if(isset($_POST['submit'])) {
    if(
        !empty($_POST['kb_first_name']) &&
        !empty($_POST['kb_last_name']) &&
        !empty($_POST['kb_email']) &&
        !empty($_POST['kb_grad_year']) &&
        !empty($_POST['kb_donation_amount'])
       ) {
        // All Data Is Set

        // Make sure email is valid format
        $email = filter_var($_POST['kb_email'], FILTER_VALIDATE_EMAIL);

        if(!empty($email)) { // ect.. ect..}
else {
// screw up
// Notify the user
// $_SESSION['error'] = 'You Screwed Up' === NEED ALTERNATIVE
}
  • 写回答

2条回答 默认 最新

  • douyan9417 2018-01-25 10:08
    关注

    First of all, don't provide any of your PHP script link like you did by using action="/kb-donations/includes/handler.php"

    It can raise security issues. It's not a WordPress way of doing things.

    To process your form use "admin_post"(admin_post_YOUR_ACTION) and "admin_post_nopriv" (admin_post_nopriv_YOUR_ACTION) actions. Here is a good explanation:

    https://www.sitepoint.com/handling-post-requests-the-wordpress-way/

    and then you need to set flash messages to give error messages or success messages to the users.

    If its a backend form, then use "admin_notices" action. Here is a good explanation:

    https://premium.wpmudev.org/blog/adding-admin-notices/

    For front-end forms :

    i. You can use your form model to show the flash messages (success or error messages by storing in "error" private variable of the backed up model)

    or ii. you can use "template_notices" action.

    or iii. you can use "session_start" (starting a session, storing data into the session and clearing it after display) solution.

    Using global variable is always a bad idea.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题