frederik's blog

Music (5) Projects (2) Snippets (8) Technology (5) Thoughts (9)

Speak×

Basic WordPress SEO without using plugins

!

Don’t you just hate it when you need to install a plugin to achieve one simple thing using WordPress?

Basic WordPress SEO without plugins can be achieved with very few lines of code. We’ll use custom fields to make this happen.

WordPress provides the perfect function for our purposes: get_post_meta( int $post_id, string $key = '', bool $single = false ). Read more about it here.

The following snippet is all you need to add to the header.php in the <head> section. I put it right above the wp_head() PHP call.

<?php if (is_single() || is_page()) { ?>
    <meta name="description" content="<?php echo get_post_meta(get_the_ID(), 'frjk_meta_description', true); ?>">
<?php } else if (is_category()) { ?>
    <meta name="description" content="<?php echo wp_strip_all_tags(category_description()); ?>">
<?php } ?>

But for the custom field to work you have to add it first.

If you’ve never used custom fields with WordPress before you may have to enable them first. You can do so by clicking on Screen Options while editing a post (or a page) and checking the box I circled in red.

Then you add a custom field as shown in the screenshot below. The custom fields are below the editor. The name has to match the key from the get_post_meta() function. In my case that’s frjk_meta_description. You can choose whatever name you like.

If the custom field is empty the output will be blank.

So, before publishing a post, you should make sure to add the newly created custom field. Add it to your checklist.

If the meta description is empty, Google and other search engines manage to create a snippet by themselves. That’s fine in many cases but it would be nice if this was a true set-and-forget solution.

Even better WordPress SEO without plugins

You may have noticed that the snippet above contains a second condition. When a category is shown, we also want to have a meta description. In this case we just add the category description.

We can ignore the daily, monthly and yearly archives and from my point of view we can also ignore all the other archive types.

We need a meta description for the frontpage. We also need a fallback if we forget to add the custom field for posts or pages.

So we check whether the custom field is empty and if it is we add a couple of words from the post (or page) excerpt.

Again, WordPress provides all the functions we need, so the size of our revised snippet is still very manageable.

<?php if (is_single() || is_page()) { ?>
    <?php $post_meta_cache = get_post_meta(get_the_ID(), 'frjk_meta_description', true); ?>
    <?php if($post_meta_cache && !empty($post_meta_cache)) { ?>
	<meta name="description" content="<?php echo $post_meta_cache; ?>">
    <?php } else { ?>
	<?php setup_postdata($post); ?>
	<meta name="description" content="<?php echo wp_trim_words(wp_strip_all_tags( get_the_excerpt(), true ), 20); ?>">
    <?php } ?>
<?php } else if (is_category()) { ?>
    <meta name="description" content="<?php echo wp_strip_all_tags(category_description()); ?>">
<?php } else if (is_home()) { ?>
    <meta name="description" content="This is where the meta description for the frontpage is added. Manually.">
<?php } ?>

SEO plugins can do much more

But they do so at a price. Not only figuratively but with an actual price tag attached.

Of course it would be nice to have the ability to change the meta description for the frontpage from the backend but I prefer to keep things simple. I hardly ever edit the meta description once I’ve created one.

The SEO plugins I’ve used in the past give me much more control than my snippet but I like how lightweight the solution above is compared to professional SEO suites. I don’t need the bloat and most people I’ve met don’t need it either.