Show form per shortcode

时间:2020-10-14 作者:Onyeka Franklin

这是我的密码。我想按短代码显示它。

$user_id = get_current_user_id();
$balance = mycred_get_users_balance( $user_id );
echo "Current Balance: ".$balance."<br><br>";
if ($_POST && $_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST[\'amount\'])){
    $towithdraw = $_POST[\'amount\'];
        $towithdraws = "-".$towithdraw;
        if ($towithdraw > $balance){
            echo "Insufficient Funds!";
    }else{
        mycred_subtract( \'Withdrawal\', $user_id, $towithdraws, \'Activity earnings withdrawal\' );

        $uname = $_POST[\'uname\'];
        $fname = $_POST[\'fname\'];
        $lname = $_POST[\'lname\'];
        $email = $_POST[\'email\'];
        $amount = $_POST[\'amount\'];
        $acct = $_POST[\'acct\'];
        $link = $_POST[\'link\'];
$name = $fname.lname;


$message = $_POST[\'message\'];
$formcontent=" From: $name \\n Username: $uname \\n Email Address: $email \\n Amount: $amount \\n  Facebook link: $link \\n Account details: $acct \\n";
$recipient = "[email protected]";
$subject = "Withdrawal Form";
$mailheader = "From: $email \\r\\n";
wp_mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Your withdrawal request has been submitted successfully and you have been debited instantly, please kindly wait for moderation";
            
  
    }
}else{

echo \'<form method="POST" action="https://plantfunds.com/withdrawal/">
                <label for="uname">Username:</label><br>
                <input type="text" id="uname" name="uname" required=" "><br>

            <label for="fname">First name:</label><br>
                <input type="text" id="fname" name="fname" required=" "><br>

            <label for="lname">Last name:</label><br>
                <input type="text" id="lname" name="lname" required=" "><br>

            <label for="email">Email address:</label><br>
                <input type="email" id="email" name="email" required=" "><br>

            <label for="acct">Bank Details:</label><br>
                <textarea id="email" name="acct" required=" "> </textarea>  <br>


                <label for="amount">Amount to withdraw:</label><br>
                <input type="number" id="amount" name="amount" value="amount" required=" "><br><br>

            <label for="link">Facebook link:</label><br>

            <input type="url" id="link" name="link" required=" "><br><br>
                <input type="submit" value="Submit Request">
            </form>\';
   }

1 个回复
SO网友:Nour Edin Al-Habal

使用add_shortcode 在插件或主题中,或者如果不想添加插件或修改主题,请使用Code Snippets 插件。您的代码如下所示:

add_shortcode( "your-awesome-unique-shortcode", "wpse376442_your_awesome_shortcode_callback");

function wpse376442_your_awesome_shortcode_callback($atts, $content, $shortcode_tag) {
    $user_id = get_current_user_id();
    $balance = mycred_get_users_balance( $user_id );
    $echo = "Current Balance: ".$balance."<br><br>";
    if ($_POST && $_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST[\'amount\'])){
        $towithdraw = $_POST[\'amount\'];
            $towithdraws = "-".$towithdraw;
            if ($towithdraw > $balance){
                $echo = "Insufficient Funds!";
        }else{
            mycred_subtract( \'Withdrawal\', $user_id, $towithdraws, \'Activity earnings withdrawal\' );
    
            $uname = $_POST[\'uname\'];
            $fname = $_POST[\'fname\'];
            $lname = $_POST[\'lname\'];
            $email = $_POST[\'email\'];
            $amount = $_POST[\'amount\'];
            $acct = $_POST[\'acct\'];
            $link = $_POST[\'link\'];
            
            $name = $fname.lname;
            
            
            $message = $_POST[\'message\'];
            $formcontent=" From: $name \\n Username: $uname \\n Email Address: $email \\n Amount: $amount \\n  Facebook link: $link \\n Account details: $acct \\n";
            $recipient = "[email protected]";
            $subject = "Withdrawal Form";
            $mailheader = "From: $email \\r\\n";
            
            wp_mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
            
            $echo = "Your withdrawal request has been submitted successfully and you have been debited instantly, please kindly wait for moderation";
                
      
        }
    }else {
    
        $echo = \'<form method="POST" action="https://plantfunds.com/withdrawal/">
                    <label for="uname">Username:</label><br>
                    <input type="text" id="uname" name="uname" required=" "><br>
    
                <label for="fname">First name:</label><br>
                    <input type="text" id="fname" name="fname" required=" "><br>
    
                <label for="lname">Last name:</label><br>
                    <input type="text" id="lname" name="lname" required=" "><br>
    
                <label for="email">Email address:</label><br>
                    <input type="email" id="email" name="email" required=" "><br>
    
                <label for="acct">Bank Details:</label><br>
                    <textarea id="email" name="acct" required=" "> </textarea>  <br>
    
    
                    <label for="amount">Amount to withdraw:</label><br>
                    <input type="number" id="amount" name="amount" value="amount" required=" "><br><br>
    
                <label for="link">Facebook link:</label><br>
    
                <input type="url" id="link" name="link" required=" "><br><br>
                    <input type="submit" value="Submit Request">
                </form>\';
    }
    return $echo;
}
注意,我将echo转换为字符串并返回它们。作为reference 表示:

请注意,由shortcode调用的函数不应产生任何类型的输出。Shortcode函数应返回用于替换Shortcode的文本。直接生成输出将导致意外结果。

相关推荐

Shortcode called twice

我正在使用一个快捷代码处理我的产品信息,它可以自动生成一个包含信息的表。但这个短代码似乎被调用了两次。我不是一个优秀的后端开发人员,但我正在努力学习一些基础知识,以便能够制作一些基本的PHP函数。我非常感谢你的帮助。提前谢谢。我的代码如下所示:function displayTable() { echo \'<table>\'; echo \'<tbody>\'; $fields = get_field_objects();&