最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Show form per shortcode

programmeradmin3浏览0评论

This is my code. I want to show it per shortcode.

$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="/">
                <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>';
   }

This is my code. I want to show it per shortcode.

$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/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>';
   }
Share Improve this question edited Oct 14, 2020 at 6:56 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Oct 14, 2020 at 6:40 Onyeka FranklinOnyeka Franklin 1
Add a comment  | 

1 Answer 1

Reset to default 1

Use add_shortcode in your plugin or theme or, if you don't want to add a plugin or modify the theme, use Code Snippets plugin. Your code will look like this:

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/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;
}

Note that I converted the echo's to strings and returned them. As the reference says:

Note that the function called by the shortcode should never produce an output of any kind. Shortcode functions should return the text that is to be used to replace the shortcode. Producing the output directly will lead to unexpected results.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论