DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

Sure, there’s lots of great WordPress plugins to help you with SEO. But in case you don’t want to use a plugin, here’s a super efficient code make your blog SEO friendly.

Let’s start by pasting the code below into your functions.php file:

function basic_wp_seo() {
	global $page, $paged, $post;
	$default_keywords = 'wordpress, plugins, themes, design, dev, development, security, htaccess, apache, php, sql, html, css, jquery, javascript, tutorials'; // customize
	$output = '';

	// description
	$seo_desc = get_post_meta($post->ID, 'mm_seo_desc', true);
	$description = get_bloginfo('description', 'display');
	$pagedata = get_post($post->ID);
	if (is_singular()) {
		if (!empty($seo_desc)) {
			$content = $seo_desc;
		} else if (!empty($pagedata)) {
			$content = apply_filters('the_excerpt_rss', $pagedata->post_content);
			$content = substr(trim(strip_tags($content)), 0, 155);
			$content = preg_replace('#\n#', ' ', $content);
			$content = preg_replace('#\s{2,}#', ' ', $content);
			$content = trim($content);
		} 
	} else {
		$content = $description;	
	}
	$output .= '<meta name="description" content="' . esc_attr($content) . '">' . "\n";

	// keywords
	$keys = get_post_meta($post->ID, 'mm_seo_keywords', true);
	$cats = get_the_category();
	$tags = get_the_tags();
	if (empty($keys)) {
		if (!empty($cats)) foreach($cats as $cat) $keys .= $cat->name . ', ';
		if (!empty($tags)) foreach($tags as $tag) $keys .= $tag->name . ', ';
		$keys .= $default_keywords;
	}
	$output .= "\t\t" . '<meta name="keywords" content="' . esc_attr($keys) . '">' . "\n";

	// robots
	if (is_category() || is_tag()) {
		$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
		if ($paged > 1) {
			$output .=  "\t\t" . '<meta name="robots" content="noindex,follow">' . "\n";
		} else {
			$output .=  "\t\t" . '<meta name="robots" content="index,follow">' . "\n";
		}
	} else if (is_home() || is_singular()) {
		$output .=  "\t\t" . '<meta name="robots" content="index,follow">' . "\n";
	} else {
		$output .= "\t\t" . '<meta name="robots" content="noindex,follow">' . "\n";
	}

	// title
	$title_custom = get_post_meta($post->ID, 'mm_seo_title', true);
	$url = ltrim(esc_url($_SERVER['REQUEST_URI']), '/');
	$name = get_bloginfo('name', 'display');
	$title = trim(wp_title('', false));
	$cat = single_cat_title('', false);
	$tag = single_tag_title('', false);
	$search = get_search_query();

	if (!empty($title_custom)) $title = $title_custom;
	if ($paged >= 2 || $page >= 2) $page_number = ' | ' . sprintf('Page %s', max($paged, $page));
	else $page_number = '';

	if (is_home() || is_front_page()) $seo_title = $name . ' | ' . $description;
	elseif (is_singular())            $seo_title = $title . ' | ' . $name;
	elseif (is_tag())                 $seo_title = 'Tag Archive: ' . $tag . ' | ' . $name;
	elseif (is_category())            $seo_title = 'Category Archive: ' . $cat . ' | ' . $name;
	elseif (is_archive())             $seo_title = 'Archive: ' . $title . ' | ' . $name;
	elseif (is_search())              $seo_title = 'Search: ' . $search . ' | ' . $name;
	elseif (is_404())                 $seo_title = '404 - Not Found: ' . $url . ' | ' . $name;
	else                              $seo_title = $name . ' | ' . $description;

	$output .= "\t\t" . '<title>' . esc_attr($seo_title . $page_number) . '</title>' . "\n";

	return $output;
}

Once done, replace the $default_keywords (line 3) with your own and add the following code into your header.php file:

<?php echo basic_wp_seo(); ?>

Then, don’t forget to check the source of your pages and fine tune the code if needed.

Big thanks to Jeff Starr for this amazing code!

Tags

14 Comments

  1. thanks. i use seo by yoast plugin.
    can use this code? or i should deactive plugin yoast?

  2. The problem with this approach is that what it does is cram the same phrases and words onto every one of your pages. Keep the list of terms short and well-focused and make them unique/long-tail-ish, and MAYBE that’s OK. But put a long list of words on every page and whether the terms are hard to rank for or not eventually GOOG will notice what you’ve done and punish you.

    This is really nothing more than indiscriminate keyword stuffing. Great proof-of-concept, but … a really bad idea, says this SEO guy

  3. Keywords? It’s not 1990 anymore! Also, a SEO plugin gives you much more control than this snippet.
    This method might be fine for some people, but I strongly prefer to use Yoast for this.

  4. Thanks for this. I’ll have to look at it later to see if I can figure out all that it’s doing. I currently install the FV Simpler SEO plugin, having found Yoast too complex, without good defaults, and export/import (it may be different now).

    It seems one of the good features the plugins give you is the ability to edit the meta description that Google et al. will display to searchers. Of course the above will not take care of this, so I want to consider whether it would be good to have both the code and FV Simpler SEO installed.

  5. Nice of you to offer this hack but not sure why when there are some great, and free, SEO plugins available – like SEO Plugin by Yoast.

  6. Really great solution thank you for posting. WP can be complicated using other SEO methods but having direct control is always my preferred choice.

  7. Your much better off doing this with a plugin, you could even just put this code in a plugin.

  8. Thanks for sharing the code, I´ll try it and compare the results with SEO plugins.

  9. Hello,
    I’m sorry but when I add and use this snippet, I just get the Title of the post/page (in the page).
    Not the Meta Description, Title or Keywords… in code source.

  10. Simple and effective solution for basic usage. Yoast is far better but more complex too so your function is ok for those who do just want basic SEO setup.

  11. Thanks a lot. This one really helped.

Leave a Comment

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