בפוסט הקודם שלי בנושא פילטרים אמרתי שיש לא מעט דברים שניתן לעשות עם פילטרים. בפוסט הזה נראה כמה דוגמאות שימושיות למדיי.
כיווניות תגובות
הקוד הבא יכול לשמש ליישור התגובה לימין או לשמאל כתלות בשפה בה נכתבה
/* Comments directionality filter */
add_filter('comment_text', 'lang_dir');
function lang_dir($content) {
$eng = preg_match_all('/[a-zA-Z].?/', $content, $matches);
$heb = preg_match_all('/[א-תץףךםן].?/', $content, $matches);
if ($eng > $heb) {
return "<div class='lcomment'><p>$content &;lt;/p></div>";
} else {
return "<div class='rcomment'><p>$content</p></div>";
}
}
כמובן שצריך להוסיף גם את קוד ה-css הבא:
.lcomment {
direction: ltr;
text-aliggn: left;
}
.rcomment {
direction: rtl;
text-align: right;
}
הקוד מטפל כרגע בעברית מול אנגלית, אבל ניתן כמובן להתאים אותו לטיפול בשפות נוספות בקלות יחסית.
מחליף שפות
נכון, יש כל מיני תוספים שעושים בעקרון את העבודה, לא בעקרון את רובם אני לא אוהבת וחושבת שהם מסורבלים. לכן כתבתי פילטר שעושה בדיוק את אותה העבודה
/* Language 'plugin' for the theme */
function ShowHideJs() {
?>
<script>
function setElementVisibility(eShow, eHide){
document.getElementById(eShow).style.visibility = "visible";
document.getElementById(eShow).style.display = "block";
document.getElementById(eHide).style.visibility = "hidden";
document.getElementById(eHide).style.display = "none";
}
function ShowHeb(){
setElementVisibility("heb", "eng");
}
function ShowEng(){
setElementVisibility("eng", "heb");
}
</script>
<?
}
function add_buttons($content) {
if ((strstr($content, "[heb]") !== false) && (strstr($content, "[eng]") !== false)) {
$hebbtn = get_bloginfo('template_url')."/img/hBtn";
$engbtn = get_bloginfo('template_url')."/img/eBtn";
$buttons .= "<img src='".$engbtn.".png' onMouseover='this.src=\"".$engbtn."_Hover.png\"' onMouseout='this.src=\"".$engbtn.".png\"' onClick='ShowEng()' />\n";
$buttons .= "<img src='".$hebbtn.".png' onMouseover='this.src=\"".$hebbtn."_Hover.png\"' onMouseout='this.src=\"".$hebbtn.".png\"' onClick='ShowHeb()' />\n";
return "<div id='lang'>".$buttons."".$content;
}
return $content;
}
function heb($content) {
return str_replace ("[/heb]", "</div>",
str_replace("[heb]", "<div id='heb' name='heb' style='visibility:display;'>", $content));
}
function eng($content) {
return str_replace ("[/eng]", "</div>",
str_replace("[eng]", "<div id='eng' style='visibility:hidden; display:none;'>", $content));
}
add_filter('wp_head', 'ShowHideJs');
add_filter('the_content', 'add_buttons');
add_filter('the_content', 'eng');
add_filter('the_content', 'heb');
שימו לב שהקוד למעלה משתמש במספר פילטרים שונים:
- פילטר JS שיושב בהדר של העמוד
- פילטר שמוסיף את כפתורי השפה
- פילטר אנגלית, פילטר עברית
את השנים האחרונים היה נניתן כמובן לשלב לפילטר יחיד, אבל אני החלטתי שזה יותר יפה כך
יש עוד לא מעט פילטרים שימושיים, ניתן לראות חלק בפוסטים הבאים:
ויש עוד לא מעט כאלו
נהניתם ? הרשמו לעדכוני RSS !

");
}
שמי חנית כהן, אני בת 33 ואשת מחשבים משנת 93.