分享

4 Tips about how to optimize wordpress for seo | Sven's Blog

 这是名字 2014-08-24

Today Sven will introduce some tips about wordpress Seo,SEO means Search Engine Optimization.If you wanna your wordpress site more attractive to search engine,first you would know search engine’s taste.There are lots of plugins which have the same function,but if you want to do it by yourself,follow Sven and do the following job:

SEO for header

First is Seo for header,add following code to header.php

Attention:if there are already title/description/keywords in header.php,first delete the old one.Never forget use your own information instead of the following one.^ ^

it can add tags als the keywords of the single page,it will help a lot.

<?php
if(is_home()) { ?>
<title>Sven's Blog,Sven的博客</title>
<meta content="Welcome to Sven's Blog!欢迎来到Sven的博客" name="Description"/>
<meta content="Sven,Sven Qin,Wordpress,Sven's Blog,sven的博客" name="Keywords"/>
<?php } ?>
<?php
if(is_category()) { ?>
<title><?php echo trim(wp_title('',0)); ?> | <?php bloginfo(name);?><? $paged = get_query_var('paged'); if ( $paged > 1 ) printf('page %s ',$paged); ?></title>
<meta content="<?php echo trim(strip_tags(category_description())); ?>" name="Description"/>
<meta content="<?php echo single_cat_title(); ?>" name="Keywords"/>
<?php } ?>
<?php
if(is_single())  { ?>
<title><?php echo trim(wp_title('',0)); ?> | <?php bloginfo(name);?><? $paged = get_query_var('paged'); if ( $paged > 1 ) printf('page %s ',$paged); ?></title>
<meta name="description" content="<?php echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 200,"......","utf-8"); ?>" />
<?php
$keywords = get_the_tags();$keyword = '';
foreach ($keywords as $value) {
$keyword .= $value->name.',';
}
?>
<meta name="keywords" content="<?php echo $keyword ;?>" />
<?php }?>
<?php
if(is_tag()) { ?>
<title><?php echo trim(wp_title('',0)); ?>_tag page<? $paged = get_query_var('paged'); if ( $paged > 1 ) printf('page %s ',$paged); ?></title>
<meta name="description" content="<?php echo trim(strip_tags(tag_description())); ?><? $paged = get_query_var('paged'); if ( $paged > 1 ) printf('page %s ',$paged); ?>" />
<meta name="keywords" content="<?php echo trim(wp_title('',0)); ?>" />
<?php }?>

Second code for header.php,it can use the first 100 byte as the description of the page.

<?php
if(is_single())  { ?>
<meta name="description" content="<?php echo trim(wp_title('',0)); ?>-<?php echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 100,"......","utf-8"); ?>" />
<?php }?>

add “nofollow” tag to external links automaticly

following code can add rel=”nofollow” to external links,just copy the code to functions.php:

add_filter( 'the_content', 'sven_seo_wl');
function sven_seo_wl( $content ) {
    $regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>";
    if(preg_match_all("/$regexp/siU", $content, $matches, PREG_SET_ORDER)) {
        if( !empty($matches) ) { 
            $srcUrl = get_option('siteurl');
            for ($i=0; $i < count($matches); $i++)
            {     
                $tag = $matches[$i][0];
                $tag2 = $matches[$i][0];
                $url = $matches[$i][0];
                $noFollow = '';
                $pattern = '/target\s*=\s*"\s*_blank\s*"/';
                preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);
                if( count($match) < 1 )
                    $noFollow .= ' target="_blank" ';     
                $pattern = '/rel\s*=\s*"\s*[n|d]ofollow\s*"/';
                preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);
                if( count($match) < 1 )
                    $noFollow .= ' rel="nofollow" '; 
                $pos = strpos($url,$srcUrl);
                if ($pos === false) {
                    $tag = rtrim ($tag,'>');
                    $tag .= $noFollow.'>';
                    $content = str_replace($tag2,$tag,$content);
                }
            }
        }
    }  
    $content = str_replace(']]>', ']]>', $content);
    return $content;
}

Add alt to images

the same,adding the code to functions.php:

add_filter('the_content', 'svenseo');
function svenseo($content) {
       global $post;
       $pattern ="/<a(.*?)href=('|\")(.*?).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>/i";
       $replacement = '<a$1href=$2$3.$4$5 alt="'.$post->post_title.'" title="'.$post->post_title.'"$6>';
       $content = preg_replace($pattern, $replacement, $content);
       return $content;
}

That’s all.

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多