Connect with us

Basic SEO – Do It Youselfer (DIY) Using Some Basic PHP Code

solution

Basic SEO – Do It Youselfer (DIY) Using Some Basic PHP Code

Everyone needs a SEO, either a SEO plugin or a DIY SEO.

Plugins are good but you don’t know if they are indeed performing to your expectation. As the plugin gets more and more “advanced”, things get more and more complicated. With so many parameters/variables to worry about, sometimes you need a manual to make sure if you have done right or wrong.

If you are using a plugin, check page source to see if the plugin is indeed doing what you want.

What we get from the above codes?

  1. Title: different titles for different types of pages/posts.
  2. Meta Description: use excerpt as meta description for pages/posts; Front/Home use site description as meta description.
  3. Meta Keywords: use tags as keywords; Front/Home page use the last post’s tags as keywords.
  4. Tell robot not to index only home/front page and single post pages. Author, archive, search, tag pages will be excluded.
  5. Add canonical URL to eliminate content duplication problem.
  6. You should have a faster loading website!

Or you could manually add the following codes to your header.php file.

 

<title>
<?php if ( is_home() ) { ?><?php bloginfo(‘name’); ?><?php } ?>
<?php if ( is_search() ) { ?>Search Results&nbsp;|&nbsp;<?php bloginfo(‘name’); ?><?php } ?>
<?php if ( is_author() ) { ?>Author Archives&nbsp;|&nbsp;<?php bloginfo(‘name’); ?><?php } ?>
<?php if ( is_single() ) { ?><?php wp_title(”); ?>&nbsp;|&nbsp;<?php bloginfo(‘name’); ?><?php } ?>
<?php if ( is_page() ) { ?><?php wp_title(”); ?>&nbsp;|&nbsp;<?php bloginfo(‘name’); ?><?php } ?>
<?php if ( is_category() ) { ?><?php single_cat_title(); ?>&nbsp;|&nbsp;<?php bloginfo(‘name’); ?><?php } ?>
<?php if ( is_month() ) { ?><?php the_time(‘F’); ?>&nbsp;|&nbsp;<?php bloginfo(‘name’); ?><?php } ?>
<?php if (function_exists(‘is_tag’)) { if ( is_tag() ) { ?><?php bloginfo(‘name’); ?>&nbsp;|&nbsp;Tag Archive&nbsp;|&nbsp;<?php single_tag_title(“”, true); } } ?>
</title>
<?php
if (is_home() || is_front_page() ) :
echo ‘<meta name=”description” content=”‘.get_bloginfo(‘description’).’” />’;
else :
$post = $wp_query->post;
$descrip = strip_tags($post->post_content);
$descrip_more = ”;
if (strlen($descrip) > 155) {
$descrip = substr($descrip,0,155);
$descrip_more = ‘ …’;
}
$descrip = str_replace(‘”‘, ”, $descrip);
$descrip = str_replace(“‘”, ”, $descrip);
$descripwords = preg_split(‘/[\n\r\t ]+/’, $descrip, -1, PREG_SPLIT_NO_EMPTY);
array_pop($descripwords);
$descrip = implode(‘ ‘, $descripwords) . $descrip_more;
echo ‘<meta name=”description” content=”‘.$descrip.’” />’;
endif;
?>
<?php global $post;
if( is_single() || is_page() || is_home() ) :
$tags = get_the_tags($post->ID);
if($tags) :
foreach($tags as $tag) :
$sep = (empty($keywords)) ? ” : ‘, ‘;
$keywords .= $sep . $tag->name;
endforeach;
?>
<meta name=”keywords” content=”<?php echo $keywords; ?>” />
<?php
endif;
endif;
?>
<?php if ( !(is_home()) && !(is_single()) ) { ?><meta name=”robots” content=”noindex,follow” /><?php }?>
<?php if ( is_singular() ) { ?> <link rel=”canonical” href=”<?php the_permalink(); ?>” /> <?php } ?>

Related:  Install Glue for Yoast SEO & AMP Plugin on WordPress
Continue Reading
You may also like...

We are a WordPress plugin developer company that focuses on useful WordPress plugin creation and empowering people to earn passive incomes from their blogs. We build unique and groundbreaking plugins that will revolutionize your blog!

Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

More in solution

    Sign up for our newsletter and get a free affiliate e-book!

    About Me:

    Szabi Kisded

    Hi, my name is Szabi and I'm documenting my journey selling plugins on CodeCanyon. I will show you every step of it: learning to code, plugin ideas, WordPress stuff and more. Read more…

    Mega Plugin Bundle:

    CodeCanyon Portfolio:

    Online Courses:

    Latest Promotions:

    Recommended Theme:

    Start Your Own Blog:

    Translate Your Blog:

    AdSense Alternative:

    Best Article Spinner:

    Popular Posts:

    Latest Posts:

    To Top