فارسی کردن اعداد در سایت وردپرسی و المنتوری
در این آموزش شما می توانید با استفاده از این تکه کد ساده تمامی اعداد انگلیسی سایتتون رو فارسی بکنید
کد فارسی کردن اعداد
زبان: JAVASCRIPT
<script>
document.addEventListener('DOMContentLoaded', function() {
function convertToPersian(text) {
const persianNumbers = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
return text.replace(/\d/g, function(match) {
return persianNumbers[parseInt(match)];
});
}
function convertAllTextNodes(node) {
if (node.nodeType === Node.TEXT_NODE) {
let parent = node.parentNode;
let isSafe = true;
while (parent && parent !== document.body) {
const tagName = parent.tagName;
if (['SCRIPT', 'STYLE', 'TEXTAREA', 'INPUT', 'CODE', 'PRE', 'SELECT', 'OPTION'].includes(tagName)) {
isSafe = false;
break;
}
if (parent.classList && (
parent.classList.contains('wp-editor') ||
parent.classList.contains('block-editor') ||
parent.id === 'wp-content-editor-container'
)) {
isSafe = false;
break;
}
parent = parent.parentNode;
}
if (isSafe && node.textContent.trim()) {
node.textContent = convertToPersian(node.textContent);
}
} else {
node.childNodes.forEach(convertAllTextNodes);
}
}
convertAllTextNodes(document.body);
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
mutation.addedNodes.forEach(function(node) {
if (node.nodeType === Node.ELEMENT_NODE) {
convertAllTextNodes(node);
}
});
});
});
observer.observe(document.body, {
childList: true,
subtree: true,
characterData: false
});
});
</script>