使用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的文本。直接生成输出将导致意外结果。