Dưới đây là một plugin WordPress hoàn chỉnh dùng shortcode (ví dụ:
- Tạo file PHP (ví dụ: blogspot-mail-send-shortcode.php) trong thư mục wp-content/plugins của WordPress và dán toàn bộ mã sau vào:
phpSao chépChỉnh sửa<?php
/*
Plugin Name: Blogspot Mail Send Shortcode
Plugin URI: http://example.com/
Description: Gửi mail cho khách hàng chứa danh sách bài đăng mới nhất từ hocdethi.tranganhnam.xyz (ảnh đại diện và trích dẫn tối đa 222 từ) thông qua shortcode
.
Version: 1.0
Author: Your Name
Author URI: http://example.com/
License: GPL2
*/
// Enqueue script cho AJAX
function bmss_enqueue_scripts() {
wp_enqueue_script( 'bmss-ajax', plugins_url( 'bmss-ajax.js', __FILE__ ), array('jquery'), '1.0', true );
wp_localize_script( 'bmss-ajax', 'bmss_ajax_obj', array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'bmss_ajax_nonce' )
) );
}
add_action( 'wp_enqueue_scripts', 'bmss_enqueue_scripts' );
// Shortcode: hiển thị form gửi mail
function bmss_mail_send_shortcode() {
ob_start();
?>
<div id="bmss-form-wrapper">
<form id="bmss-mail-form" method="post">
<label for="bmss_email"><?php _e('Nhập Email của bạn:', 'bmss'); ?></label>
<input type="email" id="bmss_email" name="bmss_email" required style="width:300px;" />
<input type="submit" value="<?php _e('Gửi mail', 'bmss'); ?>" />
</form>
<div id="bmss-response" style="margin-top:10px;"></div>
</div>
<?php
return ob_get_clean();
}
add_shortcode( 'blogspot_mail_send', 'bmss_mail_send_shortcode' );
// Xử lý AJAX gửi mail
function bmss_handle_send_mail() {
check_ajax_referer( 'bmss_ajax_nonce', 'nonce' );
$email = isset($_POST['bmss_email']) ? sanitize_email( $_POST['bmss_email'] ) : '';
if ( empty($email) || !is_email( $email ) ) {
wp_send_json_error( array('message' => 'Email không hợp lệ.') );
}
// URL feed mới (dạng JSON thuần)
$feed_url = 'https://hocdethi.tranganhnam.xyz/feeds/posts/default?alt=json&max-results=5';
$response = wp_remote_get( $feed_url );
if ( is_wp_error( $response ) ) {
wp_send_json_error( array('message' => 'Không thể lấy dữ liệu từ feed.') );
}
$body = wp_remote_retrieve_body( $response );
$data = json_decode( $body );
if ( !$data || !isset($data->feed->entry) ) {
wp_send_json_error( array('message' => 'Dữ liệu feed không hợp lệ.') );
}
$entries = $data->feed->entry;
$email_body = "Danh sách bài đăng mới nhất:\n\n";
// Duyệt qua các bài đăng (tối đa 5)
$numPosts = min( count($entries), 5 );
for ( $i = 0; $i < $numPosts; $i++ ) {
$entry = $entries[$i];
// Lấy tiêu đề bài viết
$post_title = isset($entry->title->{'$t'}) ? $entry->title->{'$t'} : 'No Title';
// Lấy link bài viết
$post_url = '';
if ( isset($entry->link) && is_array($entry->link) ) {
foreach( $entry->link as $lnk ) {
if ( isset($lnk->rel) && $lnk->rel == "alternate" ) {
$post_url = $lnk->href;
break;
}
}
}
// Lấy nội dung (hoặc summary)
$content = "";
if ( isset($entry->content->{'$t'}) ) {
$content = $entry->content->{'$t'};
} elseif ( isset($entry->summary->{'$t'}) ) {
$content = $entry->summary->{'$t'};
}
// Loại bỏ thẻ HTML
$plain_text = strip_tags( $content );
// Cắt thành 222 từ nếu vượt quá
$words = preg_split('/\s+/', $plain_text);
if ( count($words) > 222 ) {
$plain_text = implode(' ', array_slice($words, 0, 222)) . '...';
}
$email_body .= "Tiêu đề: " . $post_title . "\n";
$email_body .= "Link: " . $post_url . "\n";
$email_body .= "Trích dẫn: " . $plain_text . "\n\n";
}
$subject = 'Các bài đăng mới nhất từ Blogspot';
$headers = array('Content-Type: text/plain; charset=UTF-8');
$mail_sent = wp_mail( $email, $subject, $email_body, $headers );
if ( $mail_sent ) {
wp_send_json_success( array('message' => 'Email đã được gửi thành công tới ' . $email) );
} else {
wp_send_json_error( array('message' => 'Gửi email không thành công, vui lòng thử lại.') );
}
}
add_action( 'wp_ajax_bmss_send_mail', 'bmss_handle_send_mail' );
add_action( 'wp_ajax_nopriv_bmss_send_mail', 'bmss_handle_send_mail' );
?>
- Tạo file JavaScript (ví dụ: bmss-ajax.js) trong cùng thư mục plugin với nội dung sau:
jsSao chépChỉnh sửajQuery(document).ready(function($) {
$('#bmss-mail-form').on('submit', function(e){
e.preventDefault();
var email = $('#bmss_email').val();
$('#bmss-response').html('Đang gửi, vui lòng chờ...');
$.ajax({
url: bmss_ajax_obj.ajax_url,
type: 'POST',
dataType: 'json',
data: {
action: 'bmss_send_mail',
bmss_email: email,
nonce: bmss_ajax_obj.nonce
},
success: function(response) {
if(response.success) {
$('#bmss-response').html('<span style="color:green;">' + response.data.message + '</span>');
} else {
$('#bmss-response').html('<span style="color:red;">' + response.data.message + '</span>');
}
},
error: function() {
$('#bmss-response').html('<span style="color:red;">Có lỗi xảy ra. Vui lòng thử lại.</span>');
}
});
});
});
Hướng dẫn sử dụng:
- Cài đặt Plugin:
- Upload cả file blogspot-mail-send-shortcode.php và bmss-ajax.js vào thư mục plugin (nếu cần, bạn có thể tạo một thư mục con với tên plugin và đặt các file đó vào).
- Vào Dashboard > Plugins và kích hoạt “Blogspot Mail Send Shortcode”.
- Chèn Shortcode:
- Chèn shortcode
- Chèn shortcode
- Quy trình hoạt động:
- Khách truy cập nhập email của họ và bấm “Gửi mail”.
- Plugin sẽ qua AJAX gửi yêu cầu đến server, server sẽ lấy feed từ URL (https://hocdethi.tranganhnam.xyz/feeds/posts/default?alt=json&max-results=5), xử lý dữ liệu và gửi mail tới địa chỉ đã nhập với danh sách bài đăng mới nhất (bao gồm tiêu đề, link và trích dẫn tối đa 222 từ).
Hy vọng plugin này sẽ hữu ích cho bạn trong việc gửi mail nội dung bài đăng từ Blogspot!
Leave a comment