Wordpress - 为前端用户使用“Flash Messages"的最佳方式 [英] Wordpress - Best way to use 'Flash Messages' for front-end users

查看:19
本文介绍了Wordpress - 为前端用户使用“Flash Messages"的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一些背景:我开发了一个带有表单的 wordpress 插件.如果用户填写表单时出错,我需要一种方法来通知用户.我最初的倾向是 php 会话变量.我将各种代码添加到我的插件中以完成这项工作,包括在我的标题顶部的 session_start() 破坏了一些东西.所以我开始研究向用户显示消息的最佳方式.

我可以使用 cookie 作为 php 会话的替代,但我的问题是:

在 Wordpress 中设置前端消息的最佳方法是什么? 请注意,我是 wordpress 新手. 我听说过 Wordpress 全局变量?Wordpress 是否提供诸如全局变量之类的东西,我可以设置它可以在任何地方访问?那行得通.有没有我应该研究的插件(我不想让它膨胀).Cookie 是最佳途径吗?

这里是一段代码,可以说明我想要完成的事情:

前端表单(简码)

 <form method='POST'class="kb_donation_form" action="<?=//my action ?>"><div class="form-group"><label for="kb_first_name">名字</label><input type="text" class="form-control" id="kb_first_name" name="kb_first_name" placeholder="名字">

<div class="form-group"><label for="kb_last_name">姓</label><input type="text" class="form-control" id="kb_last_name" name="kb_last_name" placeholder="名字">

<div class="form-group"><label for="kb_email">电子邮件</label><input type="email" class="form-control" id="kb_email" name="kb_email" placeholder="电子邮件地址">

<div class="form-group"><label for="kb_grad_year">毕业年份</label><select id="kb_grad_year" name="kb_grad_year" class="form-control"><option value="friend">我只是一个朋友.</option><option value="1950">1950</option></选择>

<div class="form-group"><label for="kb_donation_amount">捐赠金额</label><input type="text" class="form-control" id="kb_donation_amount" name="kb_donation_amount" placeholder="100.00">

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

</表单>

后处理程序代码:

if(isset($_POST['submit'])) {如果(!empty($_POST['kb_first_name']) &&!empty($_POST['kb_last_name']) &&!empty($_POST['kb_email']) &&!empty($_POST['kb_grad_year']) &&!empty($_POST['kb_donation_amount'])){//设置所有数据//确保电子邮件是有效格式$email = filter_var($_POST['kb_email'], FILTER_VALIDATE_EMAIL);if(!empty($email)) {//ect.. ect..}别的 {//搞砸//通知用户//$_SESSION['error'] = '你搞砸了' === 需要替代方案}

解决方案

首先,不要像使用action="/kb-donations/includes/handler.php"

它可能会引发安全问题.这不是 WordPress 的做事方式.

要处理您的表单,请使用admin_post"(admin_post_YOUR_ACTION) 和admin_post_nopriv"(admin_post_nopriv_YOUR_ACTION) 操作.这是一个很好的解释:

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

然后您需要设置闪现信息,以向用户提供错误信息或成功信息.

如果是后端表单,则使用admin_notices"操作.这是一个很好的解释:

https://premium.wpmudev.org/blog/adding-admin-通知/

对于前端表单:

我.您可以使用您的表单模型来显示闪现消息(通过存储在备份模型的错误"私有变量中的成功或错误消息)

或 ii.您可以使用template_notices"操作.

或 iii.您可以使用session_start"(启动会话,将数据存储到会话中并在显示后清除)解决方案.

使用全局变量总是一个坏主意.

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
}

解决方案

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.

这篇关于Wordpress - 为前端用户使用“Flash Messages"的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆