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 = "withdrawal@plantfunds.com";
$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 Answer 1

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 = "withdrawal@plantfunds.com";
            $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;
}

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.

4
  • Thank you, it worked, but it's no longer getting user current balance which is necessary. Please any other suggestions? Oct 15, 2020 at 5:53
  • @OnyekaFranklin Is it showing "Current Balance:" part? If yes, try to print $user_id instead and let me what you see. Of course, make sure you're logged in. Oct 15, 2020 at 7:28
  • its not showing user current balance and i am logged in Oct 16, 2020 at 6:52
  • Is it showing the user id? Could you post the whole code somewhere so we can see it? Oct 19, 2020 at 9:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.