Search

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

הסרת גורפת של מחירי מבצע בחנויות ווקומרס

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

/**
 * Savvy Disable all sales on WooCommerce Stores.
 *
 */
function savvy_wc_get_sale_price( $sale_price, $product ) {
 	return $product->get_regular_price(); // Un-comment this to disable all sale prices
}
add_filter( 'woocommerce_product_get_sale_price', 'savvy_wc_get_sale_price', 50, 2 );
add_filter( 'woocommerce_product_get_price', 'savvy_wc_get_sale_price', 50, 2 );

שינוי מיקום הסרגל העליון בוורדפרס לתחתית

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

לא פעם קרה לי כי הסרגל העליון (admin bar) של וורדפרס הפריע לי בעיניים מכיוון והסתיר תפריטים שהיו מוגדרים כ – position:fixed ב css. מספר שורות ב functions.php והאדמין יירד לתחתית העמוד:

<?php
/***** BEGIN HERE *****/        
function fb_move_admin_bar() { ?>
	<style type="text/css">
		body {
			margin-top: -28px;
			padding-bottom: 28px;
		}
		body.admin-bar #wphead {
			padding-top: 0;
		}
		body.admin-bar #footer {
			padding-bottom: 28px;
		}
		#wpadminbar {
			top: auto !important;
			bottom: 0;
		}
		#wpadminbar .quicklinks .menupop ul {
			bottom: 28px;
		}
	</style>
<?php }
// on backend area
add_action( 'admin_head', 'fb_move_admin_bar' );
// on frontend area
add_action( 'wp_head', 'fb_move_admin_bar' );

שיפור ביצועים של הפונקציות wp_query ו query_posts

שימו לב להערות בקוד! לדוגמה, במידה ונחוץ עימוד ממוספר(Pagination) אין להשתמש ב no_found_rows=true.

$args = array(
	// Normal query arguments goes here //
	'no_found_rows' => true, // counts posts, remove if pagination required
	'update_post_term_cache' => false, // grabs terms, remove if terms required (category, tag...)
	'update_post_meta_cache' => false, // grabs post meta, remove if post meta required
);
$loop = new WP_Query( $args );

למידע נוסף בקרו במאמר הבא.

כיצד להסתיר את מפת האתר (sitemap) ממנועי החיפוש?

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

<IfModule mod_rewrite.c>
 <Files sitemap.xml>
    Header set X-Robots-Tag "noindex"
 </Files>
 </IfModule>

שימו לב להחליף את sitemap.xml בשם הקובץ של ה Sitemap שלכם. עוד מידע על מהו Sitemap (מפת אתר) תמצאו בפוסט כיצד ליצור Sitemap (מפת אתר) בוורדפרס ואיך לשלוח קובץ זה באמצעות גוגל סרץ׳ קונסול.

כיצד למנוע ממנועי חיפוש מלאנדקס תיקיות

אתם מעוניינים כי מנועי חיפוש יאנדקסו את דפי הבלוג אך לא את קבצי ה php של התקנת וורדפרס. ערכו את הקובץrobots.txt והוסיפו את השורות הבאות על מנת לחסום מנועי חיפוש מלאנדקס קבצים אלו:

User-agent: *
Disallow: /wp-admin/
Disallow: /wp-includes/
Disallow: /wp-content/plugins/
Disallow: /wp-content/themes/
Disallow: /feed/
Disallow: */feed/

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

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


// function to get the top level category object
// Usage - $top_cat = get_top_category();
// echo $top_cat->slug;

function get_top_category() {

    $cats = get_the_category(); // category object
    $top_cat_obj = array();

    foreach($cats as $cat) {
        if ($cat->parent == 0) {
            $top_cat_obj[] = $cat;
        }
    }
    $top_cat_obj = $top_cat_obj[0];

    return $top_cat_obj;
}

הוסיפת שדות נוספים לפרופיל המשתמש בלוח הבקרה

הניחו את הקוד הבא בקובץ functions.php בכדי להוסיף שדות נוספים בעמוד פרופיל המשתמש בוורדפרס.

// CUSTOM USER PROFILE FIELDS
   function my_custom_userfields( $contactmethods ) {

        // ADD CONTACT CUSTOM FIELDS
        $contactmethods['contact_phone_office']     = 'Office Phone';
        $contactmethods['contact_phone_mobile']     = 'Mobile Phone';
        $contactmethods['contact_office_fax']       = 'Office Fax';
    
        // ADD ADDRESS CUSTOM FIELDS
        $contactmethods['address_line_1']       = 'Address Line 1';
        $contactmethods['address_line_2']       = 'Address Line 2 (optional)';
        $contactmethods['address_city']         = 'City';
        $contactmethods['address_state']        = 'State';
        $contactmethods['address_zipcode']      = 'Zipcode';
   }
   add_filter('user_contactmethods','my_custom_userfields',10,1);>

הצגת סוגי תוכן מותאמים אישית בתוצאות החיפוש

הקוד הבא יגרום לסוגי התוכן המותאמים (custom post types) באתר הוורדפרס שלכם להיות מוצגים בחיפוש:


// MAKE CUSTOM POST TYPES SEARCHABLE
function searchAll( $query ) {
    if ( $query->is_search ) { $query->set( 'post_type', array( 'site', 'plugin', 'theme', 'person' )); }
     return $query;
}

add_filter( 'the_search_query', 'searchAll' );

שנו בשורה מספר 4 לסוגי התוכן הרצויים…

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

קטע הקוד הבא יוודא כי אף משתמש המחובר ללוח הבקרה של וורדפרס לא יקבל הודעות על עדכוני גירסה לוורדפרס:


    // REMOVE THE WORDPRESS UPDATE NOTIFICATION FOR ALL USERS EXCEPT SYSADMIN
    
    global $user_login;
    get_currentuserinfo();
    
    if ($user_login !== "admin") { // change admin to the username that gets the updates
        add_action( 'init', create_function( '$a', "remove_action( 'init', 'wp_version_check' );" ), 2 );
        add_filter( 'pre_option_update_core', create_function( '$a', "return null;" ) );
    }

גירסא נוספת של קוד זה תציג את הודעת העדכון לכל מנהלי האתר ולא רק למשתמש השם admin:


    // REMOVE THE WORDPRESS UPDATE NOTIFICATION FOR ALL USERS EXCEPT SYSADMIN
    
    global $user_login;
    get_currentuserinfo();
    
    if (!current_user_can('update_plugins')) { // checks to see if current user can update plugins 
          add_action( 'init', create_function( '$a', "remove_action( 'init', 'wp_version_check' );" ), 2 );
          add_filter( 'pre_option_update_core', create_function( '$a', "return null;" ) );
    }

שינוי הלוגו וקישור התמונה בעמוד ההתחברות של וורדפרס

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

add_filter( 'login_headerurl', 'namespace_login_headerurl' );
/**
 * Replaces the login header logo URL
 *
 * @param $url
 */
function namespace_login_headerurl( $url ) {
    $url = home_url( '/' );
    return $url;
}
add_filter( 'login_headerurl', 'namespace_login_headerurl' );
    

/**
 * Replaces the login header logo title
 *
 * @param $title
 */
function namespace_login_headertitle( $title ) {
    $title = get_bloginfo( 'name' );
    return $title;
}
add_filter( 'login_headertitle', 'namespace_login_headertitle' );
    
    

/**
 * Replaces the login header logo
 */
function namespace_login_style() {
    echo '<style>.login h1 a { background-image: url( ' . get_template_directory_uri() . '/images/logo.png ) !important; }</style>';
}
add_action( 'login_head', 'namespace_login_style' );