File whatsapp-chat.php
PHP
<?php
// Deklarasi plugin
class WhatsappChat {
// Konstruktor
public function __construct() {
// Register hook
add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts'));
add_action('wp_footer', array($this, 'add_chat_button'));
add_shortcode('whatsapp-chat', array($this, 'whatsapp_chat_shortcode'));
}
// Enqueue scripts
public function enqueue_scripts() {
// Enqueue script
wp_enqueue_script('whatsapp-chat', plugin_dir_url(__FILE__) . '/assets/js/whatsapp-chat.js', array('jquery'), '1.0.0', true);
}
// Add chat button
public function add_chat_button() {
// Get whatsapp number
$whatsapp_number = get_option('whatsapp_number');
// Check if whatsapp number is set
if ($whatsapp_number) {
// Render chat button
echo '<a class="whatsapp-chat-button" href="https://wa.me/' . $whatsapp_number . '">' . get_option('whatsapp_text', 'Chat via WhatsApp') . '</a>';
}
}
// Shortcode
public function whatsapp_chat_shortcode($atts) {
// Get whatsapp number
$whatsapp_number = get_option('whatsapp_number');
// Check if whatsapp number is set
if ($whatsapp_number) {
// Get custom text
$text = $atts['text'];
// Render chat button
return '<a class="whatsapp-chat-button" href="https://wa.me/' . $whatsapp_number . '">' . $text . '</a>';
}
return '';
}
}
// Instantiate plugin
new WhatsappChat();
File WhatsApp-chat.js
JavaScript
(function($) {
// Function to open WhatsApp chat
function openWhatsAppChat() {
// Get WhatsApp number
var whatsappNumber = $('#whatsapp-number').val();
// Open WhatsApp chat
window.location.href = 'https://wa.me/' + whatsappNumber;
}
// Initialize
$(document).ready(function() {
// Add click event to chat button
$('#whatsapp-chat-button').click(openWhatsAppChat);
});
})(jQuery);
Kode ini akan membuat plugin WhatsApp yang memiliki fitur-fitur berikut:
Menampilkan tombol WhatsApp di footer website.
Mendukung shortcode untuk menampilkan tombol WhatsApp di konten halaman atau pos.
Mendukung custom text untuk menentukan teks yang akan ditampilkan pada tombol WhatsApp.
Untuk mengaktifkan plugin, buka halaman Plugins di dashboard WordPress. Klik tombol Aktifkan di samping plugin Whatsapp Chat.
Untuk mengatur nomor WhatsApp, buka halaman Pengaturan > Whatsapp Chat. Masukkan nomor WhatsApp Anda di bidang Nomor WhatsApp.
Untuk mengatur teks tombol WhatsApp, buka halaman Pengaturan > Whatsapp Chat. Masukkan teks yang Anda inginkan di bidang Teks Tombol.
ingat! sesuaikan dengan jalan kodingmu…
Semoga membantu!