Để xóa các ký tự đặc biệt trong một đoạn văn bản code nhưng vẫn giữ lại định dạng HTML như xuống hàng, in đậm, màu sắc,… bạn có thể sử dụng một shortcode trong WordPress.
Thêm đoạn code sau vào file functions.php của theme Flatsome (hoặc child theme để tránh mất khi cập nhật):
// xoa ky tu dac biet
function remove_special_chars_page_shortcode() {
ob_start();
?>
<form id="remove-special-chars-form">
<label for="chars-to-remove"><strong>Nhập các ký tự cần xóa (mỗi ký tự một dòng):</strong></label><br>
<textarea id="chars-to-remove" name="chars-to-remove" rows="5" placeholder="Ví dụ:\n@\n#\n$" style="width: 100%; margin-bottom: 10px;"></textarea><br>
<label for="text-to-process"><strong>Nhập văn bản cần xử lý:</strong></label><br>
<textarea id="text-to-process" name="text-to-process" rows="6" placeholder="Nhập văn bản vào đây..." style="width: 100%; margin-bottom: 10px;"></textarea><br>
<button type="button" id="process-text-btn" style="background-color: #0073aa; color: #fff; padding: 10px 15px; border: none; cursor: pointer;">
Xóa ký tự đặc biệt
</button>
</form>
<div id="processed-result" style="margin-top: 20px; padding: 10px; border: 1px solid #ccc; background-color: #f9f9f9;">
<strong>Kết quả xử lý:</strong>
<div id="result-content" style="white-space: pre-wrap; margin-top: 10px;"></div>
<button type="button" id="copy-text-btn" style="background-color: #28a745; color: #fff; padding: 10px 15px; border: none; cursor: pointer; margin-top: 10px;">
Sao chép văn bản mới
</button>
</div>
<script>
document.getElementById('process-text-btn').addEventListener('click', function() {
const charsToRemove = document.getElementById('chars-to-remove').value.trim();
const textToProcess = document.getElementById('text-to-process').value;
if (!textToProcess) {
alert("Vui lòng nhập văn bản cần xử lý.");
return;
}
if (!charsToRemove) {
alert("Vui lòng nhập các ký tự cần xóa.");
return;
}
// Tách các ký tự cần xóa thành mảng (mỗi dòng một ký tự)
const charsArray = charsToRemove.split('\n').map(char => char.trim()).filter(char => char);
// Tạo regex từ các ký tự cần xóa
const regex = new RegExp(`[${charsArray.map(c => c.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).join('')}]`, 'g');
// Xóa các ký tự trong văn bản
const processedText = textToProcess.replace(regex, '');
// Hiển thị kết quả
document.getElementById('result-content').innerHTML = processedText;
});
// Nút sao chép văn bản
document.getElementById('copy-text-btn').addEventListener('click', function() {
const resultContent = document.getElementById('result-content').innerText;
// Sao chép vào clipboard
navigator.clipboard.writeText(resultContent).then(() => {
alert("Đã sao chép văn bản mới vào clipboard.");
}).catch(err => {
alert("Không thể sao chép văn bản.");
});
});
</script>
<?php
return ob_get_clean();
}
add_shortcode('remove_special_chars_page', 'remove_special_chars_page_shortcode');
//--------------
Dán shortcode [=_=remove_special_chars_page] vào một trang hoặc bài viết trên WordPress
Hướng Dẫn Sử Dụng Giao Diện
- Bước 1: Nhập các ký tự cần xóa vào ô đầu tiên (ví dụ:
@#$%^&*()). - Bước 2: Nhập đoạn văn bản cần xử lý vào ô thứ hai.
- Bước 3: Nhấn nút “Xóa ký tự đặc biệt”.
- Kết quả: Văn bản đã được xử lý sẽ hiển thị trong phần “Kết quả xử lý”.
Chào ! Bạn thấy nội dung này thế nào?





