פילטרים בוורדפרס – קוד שימושי

בפוסט הקודם שלי בנושא פילטרים אמרתי שיש לא מעט דברים שניתן לעשות עם פילטרים. בפוסט הזה נראה כמה דוגמאות שימושיות למדיי.

כיווניות תגובות

הקוד הבא יכול לשמש ליישור התגובה לימין או לשמאל כתלות בשפה בה נכתבה

1
2
3
4
5
6
7
8
9
10
11
12
/* 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 הבא:

1
2
3
4
5
6
7
8
.lcomment {
    direction: ltr;
    text-aliggn: left;
}
.rcomment {
    direction: rtl;
    text-align: right;
}

הקוד מטפל כרגע בעברית מול אנגלית, אבל ניתן כמובן להתאים אותו לטיפול בשפות נוספות בקלות יחסית.

מחליף שפות

נכון, יש כל מיני תוספים שעושים בעקרון את העבודה, לא בעקרון את רובם אני לא אוהבת וחושבת שהם מסורבלים. לכן כתבתי פילטר שעושה בדיוק את אותה העבודה

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/* 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');

שימו לב שהקוד למעלה משתמש במספר פילטרים שונים:

  1. פילטר JS שיושב בהדר של העמוד
  2. פילטר שמוסיף את כפתורי השפה
  3. פילטר אנגלית, פילטר עברית

את השנים האחרונים היה נניתן כמובן לשלב לפילטר יחיד, אבל אני החלטתי שזה יותר יפה כך :-)

יש עוד לא מעט פילטרים שימושיים, ניתן לראות חלק בפוסטים הבאים:

ויש עוד לא מעט כאלו :-)

025 פילטרים בוורדפרס   קוד שימושי נהניתם ? הרשמו לעדכוני RSS !

8 תגובות על הפוסט “פילטרים בוורדפרס – קוד שימושי

  1. שאלה לי אלייך,

    אני מעוניין למצוא פתרון אלגנטי לצמצום מספר המילים של excerpt, לא דרך תוסף אלא דרך פילטרים.

    הפונקציה שמחזירה את ה-excerpt של הפוסט משתמש בפונקציה wp_trim_excerpt(). עיון בקוד של הפונקציה גילה את שורת הקוד הבאה:

    $excerpt_length = apply_filters('excerpt_length', 55);

    איך את מציע לשנות זאת בעזרת פילטר?

  2. פינגבאק: תגובות בוורדפרס – לימין שור! « E.T. Official Blog

כתיבת תגובה

האימייל שלך לא יוצג באתר. (*) שדות חובה מסומנים

*


:mrgreen::|:twisted::arrow:8O:):?8):evil::D:idea::oops::P:roll:;):cry::o:lol::x:(:!::?:

תגי HTML מותרים: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>