Search

ארכיונים: סניפטים וקטעי קוד | עמוד 17

בטלו את השאלה האם לפתוח או לשמור קבצים ברמת השרת

בדרך כלל כאשר אתם מורידים קובץ מאתר כלשהו, הדפדפן מקפיץ שאלה האם לשמור או לפתוח את הקובץ. אם ברצונכם למנוע מהשרת מלשאול זאת ופשוט לשמור את הקובץ הוסיפו את הקוד הבא לקובץ htaccess:

AddType application/octet-stream .pdf
AddType application/octet-stream .zip
AddType application/octet-stream .mov

מנעו hotlinking לתמונות

נמאס לכם מאנשים אשר משתמשים ברוחב הפס שלכם ושמים קישורים לתמונות שלכם באתר שלהם? תופעה מציקה זו נקראית hotlinking בשפה המקצועית. מנעו hotlinking לתמונות על ידי הוספת הקוד הבא בתחתית הקובץ htaccess ושימו לב שאתם משנים את שם הדומיין לדומיין שלכם.

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example.com/.*$ [NC] 
RewriteRule \.(gif|jpg)$ http://www.example.com/hotlink.gif [R,L]

כיצד לאפשר העלאת קבצי SVG דרך ספריית המדיה

בכדי לאפשר העלאת קבצי SVG דרך ספריית המדיה, הוסיפו את הקוד הבא לקובץ functions.php:

function cc_mime_types($mimes) {
  $mimes['svg'] = 'image/svg+xml';
  return $mimes;
}
add_filter('upload_mimes', 'cc_mime_types');

הסבר מפורט יותר ניתן למצוא בפוסט הבא.

מיקוד CSS בהתאם לדפדפן

נראה כיצד למקד CSS selectors לפי סוג הדפדפן וגירסתו.

/***** Selector Hacks ******/

/* IE6 and below */
* html #uno  { color: red }
 
/* IE7 */
*:first-child+html #dos { color: red } 
 
/* IE7, FF, Saf, Opera  */
html>body #tres { color: red }
 
/* IE8, FF, Saf, Opera (Everything but IE 6,7) */
html>/**/body #cuatro { color: red }
 
/* Opera 9.27 and below, safari 2 */
html:first-child #cinco { color: red }
 
/* Safari 2-3 */
html[xmlns*=""] body:last-child #seis { color: red }
 
/* safari 3+, chrome 1+, opera9+, ff 3.5+ */
body:nth-of-type(1) #siete { color: red }
 
/* safari 3+, chrome 1+, opera9+, ff 3.5+ */
body:first-of-type #ocho {  color: red }
 
/* saf3+, chrome1+ */
@media screen and (-webkit-min-device-pixel-ratio:0) {
 #diez  { color: red  }
}
 
/* iPhone / mobile webkit */
@media screen and (max-device-width: 480px) {
 #veintiseis { color: red  }
}
 
/* Safari 2 - 3.1 */
html[xmlns*=""]:root #trece  { color: red  }
 
/* Safari 2 - 3.1, Opera 9.25 */
*|html[xmlns*=""] #catorce { color: red  }
 
/* Everything but IE6-8 */
:root *> #quince { color: red  }
 
/* IE7 */
*+html #dieciocho {  color: red }
 
/* Firefox only. 1+ */
#veinticuatro,  x:-moz-any-link  { color: red }
 
/* Firefox 3.0+ */
#veinticinco,  x:-moz-any-link, x:default  { color: red  }
 
 
 
/***** Attribute Hacks ******/
 
/* IE6 */
#once { _color: blue }
 
/* IE6, IE7 */
#doce { *color: blue; /* or #color: blue */ }
 
/* Everything but IE6 */
#diecisiete { color/**/: blue }
 
/* IE6, IE7, IE8 */
#diecinueve { color: blue\9; }
 
/* IE7, IE8 */
#veinte { color/*\**/: blue\9; }
 
/* IE6, IE7 -- acts as an !important */
#veintesiete { color: blue !ie; } /* string after ! can be anything */

הגדרת אוטומטית של התמונה הראשונה בפוסט כתמונה הראשית

הסניפט הבא ייקח את התמונה הראשונה שהוא מוצא בפוסט ויגדיר אותה כתמונה ראשית (featured image). במידה ותבחרו תמונה ראשית הוא יראה אותה כמובן.

function autoset_featured() {
          global $post;
          $already_has_thumb = has_post_thumbnail($post->ID);
              if (!$already_has_thumb)  {
              $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
                          if ($attached_image) {
                                foreach ($attached_image as $attachment_id => $attachment) {
                                set_post_thumbnail($post->ID, $attachment_id);
                                }
                           }
                        }
      }
add_action('the_post', 'autoset_featured');
add_action('save_post', 'autoset_featured');
add_action('draft_to_publish', 'autoset_featured');
add_action('new_to_publish', 'autoset_featured');
add_action('pending_to_publish', 'autoset_featured');
add_action('future_to_publish', 'autoset_featured');

ביטול הודעת השגיאה במסך הכניסה של וורדפרס

כאשר משתמשים מכניסים שם משתמש או סיסמא שאינם נכונים, מופיעה הודעת שגיאה שאומרת להם איזה מאלו שגויים. בהנחה ומישהו מנסה לפרוץ לאתר שלכם על ידי ניחוש אחד מאלו, הודאת שגיאה זו יכולה לעזור להבין במה שגה. חסמו זאת על ידי הוספת שורה זו לקובץ functions.php:

add_filter('login_errors',create_function('$a', "return null;"));

קביעת ברירת מחדל למצב עריכת פוסט וורדפרס

ישנם כאלו שמעדיפים לעבוד עם העורך הויזואלי (visual editor) כשהם כותבים פוסט וכאלו שמעדיפים את עורך הטקסט (text editor). שנו את ברירת המחדל בעריכת פוסט על הוספת שורות לקובץ functions.php.

# Visual Editor as default
add_filter( 'wp_default_editor', create_function('', 'return "tinymce";') );

# HTML Editor as default
add_filter( 'wp_default_editor', create_function('', 'return "html";') );

איך לאפשר תגובות בסוגי תוכן מותאמים (custom post types)

כברירת מחדל וורדפרס חוסמת את אפשרות התגובות בסוגי תוכן מותאמים (custom post types). על מנת לאפשר תגובות בפוסט מותאם אישית הוסיפו את האפשרות של comments תחת supports היכן שאתם מגדירים אותו (שורה 32).

function snippet_custom_init() {
      $labels = array(
          'name' => 'סניפט',
          'singular_name' => 'סניפט',
          'add_new' => 'הוסף סניפט חדש',
          'add_new_item' => 'הוסף סניפט חדש',
          'edit_item' => 'ערוך סניפט',
          'new_item' => 'סניפט חדש',
          'all_items' => 'כל הסניפטים',
          'view_item' => 'הצג סניפט',
          'search_items' => 'חפש סניפט',
          'not_found' =>  'לא נמצא סניפט',
          'not_found_in_trash' => 'לא נמצא סניפט בפח',
          'parent_item_colon' => '',
          'menu_name' => 'סניפטס',
          );

      $args = array(
          'labels' => $labels,
          'exclude_from_search' => false,
          'public' => true,
          'publicly_queryable' => true,
          'show_ui' => true,
          'show_in_menu' => true,
          'query_var' => true,
          'rewrite' => array( 'slug' => 'סניפט' ),
          'capability_type' => 'post',
          'has_archive' => true,
          'hierarchical' => false,
          'taxonomies' => array('category'),
          'menu_position' => null,
          'supports' => array( 'title', 'author', 'thumbnail', 'excerpt', 'comments', 'editor' )
          );

        register_post_type( 'snippet', $args );
    }
add_action( 'init', 'snippet_custom_init', 0);

כיצד להפוך את סדר התגובות בוורדפרס?

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

אסביר פה כיצד להפוך את סדר התגובות בוורדפרס.

אופציה ראשונה

גשו להגדרות > דיון. תחת הגדרות תגובות נוספות מצאו את ״ההגדרות הישנות יותר תוצגנה בתחילת העמוד״ ושנו את ההגדרה בהתאם.

להפוך את סדר התגובות בוורדפרס

אופציה שנייה

הוסיפו את הקוד הבא לקובץ functions.php של התבנית שלכם:

function wpb_reverse_comments($comments) {
		return array_reverse($comments);
	}	
add_filter ('comments_array', 'wpb_reverse_comments');

אנו משתמשים פה בפילטר comments_array בכדי להפוך את סדר התגובות.

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