我想制作一个按钮,点击后客户可以直接通过 WhatsApp 向我发送消息。我正在使用 WordPress。出于反垃圾邮件的原因,我想将链接存储在我的
function.php
中,而不是直接存储在我的 HTML 中。
我现在使用
wp-redirect
,但这会让他们离开我的网站。我不想要这样。那么我该怎么做呢?我对编码还很陌生。
函数.php
// Check if the button has been clicked
if ( isset( $_POST['whatsApp_btn'] ) ) {
// Call your function here
my_WhatsApp();
}
// Define your function
function my_WhatsApp() {
// Redirect to a URL
wp_redirect( 'https://api.whatsapp.com/send?phone=MyPhoneNumber' );
exit;
}
index.php
<form method="post">
<button type="submit" name="whatsApp_btn" target="_blank" class="whatsapp" alt="Link to WhatsApp Chat" :focus>
<i class="fa-brands fa-whatsapp"></i>
<i> whatsApp Ons</i></button>
</form>
您可以创建一个简码并在
index.php
中调用它。
首先,您在
function.php
中创建短代码:
<?php
function custom_whatsapp_buttom() {
ob_start();
?>
<a class="whatsapp" alt="Link to WhatsApp Chat" target="__blank"
href="https://api.whatsapp.com/send?phone=MyPhoneNumber&text=Text example">
<i class="fa-brands fa-whatsapp"></i>
<i>whatsApp Ons</i>
</a>
<?php
ob_end_flush();
}
add_shortcode('whatsapp-btn-shortcode', 'custom_whatsapp_buttom');
ob_start()
和 ob_end_flush()
来传递 html 标记。<a>
标签而不是 <buttom>
。您可以将短代码添加到您的
index.php
文件中,如下所示:
<?php echo do_shortcode('[whatsapp-btn-shortcode]'); ?>
如果需要,您可以更改短代码的名称
whatsapp-btn-shortcode
。希望对你有帮助。