Most SEO plugins have way too many bells and whistles for my simple needs, so I wrote a little snippet that’s meant as a drop-in, DIY replacement for the big WordPress SEO plugins. If you want a lot of features and options, then try Yoast’s awesome SEO plugin or the great All in One SEO; otherwise, if you just want something simple that works, check out Basic WP SEO — a simple slab of code that you add to your functions.php file and done.
What does this code do?
Simple. It optimizes your WordPress pages for search engines by including the following meta data in the
- Meta description – uses custom meta if exists, else uses the first 155 words of content
- Meta keywords – uses custom meta if exists, else generates from tags and categories
- Meta Robots – includes meta robots index/follow or noindex/follow depending on page
- SEO title – uses custom meta if exists, else generates an optimized unique title
These are some useful, beneficial things to include in your web pages, which is why SEO plugins are so popular. The problem is that most SEO plugins are just way too overwrought and heavy-handed, especially for DIY folk who implement stuff like .htaccess, robots.txt, sitemaps, and Google Analytics via their own optimized methods.
Trying to roll it all up into a single plugin just wasn’t working for me, but I needed something solid for my own sites’s SEO and now am using the following Basic WP SEO snippet, which I’ll eventually turn into a plugin of its own:
So that’s it, just enter your default keywords and then call the function by including this tag in the
<head> section of your pages (e.g.,
<?php echo basic_wp_seo(); ?>
Note that this code is a replacement/alternative for existing methods, so it doesn’t overwrite or hook into anything — call it wherever and customize as needed. This also means that you’ll want to remove any existing
<meta> tags that may already exist in your theme’s header.php file.
Once the Basic WP SEO script and header tag are uploaded to the server, visit each of the different types of pages (e.g., single, index, archive, search, 404, et al) and view the source code to verify proper functionality. In other words, I’ve tested this script thoroughly on my own sites (e.g., you can see this code in action at Perishable Press and here at DigWP.com), but your mileage may vary so test well and fine-tune as needed.
With a simple script, the features are fewer, but again that’s the whole point. Here are some “micro-features” of Basic WP SEO:
- One script, plug-n-play functionality with minimal configuration
- Optimized title tags based on Perfect WordPress Title Tags
- Makes it easy to migrate and use data from most any SEO plugin
- Generates SEO-optimized meta robots tags for all page views
- Outputs clean markup with no funky HTML comments, et al
Again, the main purpose of this script is to provide a simple alternative to the “all-purpose” WP SEO plugins currently available. With Yoast and All in One for example, functionality goes far beyond optimized meta/robots and title tags; you also get sitemaps, navigation links, Google Analytics, and the whole nine yards. So the main feature of the Basic WP SEO script is that it doesn’t try to do it all, instead it focuses on the basics and lets you go from there.
Migrating from AiOSEO, Yoast SEO, or similar
Support for existing/custom meta data is baked right in, so you can continue adding custom keywords, titles, and descriptions for individual posts and pages via custom fields. Then, to utilize any existing meta data from other SEO plugins, you basically have two options: change the script or rename the meta fields in the database. For example, at Perishable Press, I was using AiOSEO before trying WP SEO, then tried Yoast SEO, so my WP database contained lots of weird looking custom meta fields like this:
_yoast_wpseo_metakeywords _yoast_wpseo_title _yoast_wpseo_metadesc
Apparently Yoast SEO prepends its prefix to any existing SEO fields when activated, so who knows what names you’ll actually see in your database, but the meta data should look similar. In any case, once you’ve got the names of the meta fields, there are two ways of migrating from the SEO plugin to the Basic WP SEO script:
- Edit the
mm_seo_titlein the Basic WP SEO script to match the existing field values.
- OR if you’re sure that you won’t be returning to the SEO plugin, simply rename the meta fields in the database (explained below)
Of these choices, I went ahead and just renamed the db fields using the following SQL commands:
UPDATE `wp_postmeta` SET `meta_key` = 'mm_seo_keywords' WHERE `meta_key` = '_yoast_wpseo_metakeywords' UPDATE `wp_postmeta` SET `meta_key` = 'mm_seo_title' WHERE `meta_key` = '_yoast_wpseo_title' UPDATE `wp_postmeta` SET `meta_key` = 'mm_seo_desc' WHERE `meta_key` = '_yoast_wpseo_metadesc'
Some notes about these queries:
- BACKUP your database before making any changes
- The underscore means that the custom field is hidden; omitting it means the custom field will be available for use and customization via Post/Page editor.
- You’ll want to replace the
_yoast_wpseo_metakeywordsand other names with the actual names used in your database.
wp_postmetais the default table name, so if you’re using custom database prefix, you’ll need to add it (for example, you’ll need to change
whatever_wp_postmetaif your custom db prefix is “whatever_”).
Once the meta fields have been renamed, they will be used by the Basic WP SEO script if available; or if not, then you can either add them manually (via custom fields), or else the script will automatically generate unique descriptions, keywords, titles, and robots tags for your posts and pages.
I hope this technique helps others who find themselves wanting a simpler SEO solution. I’m currently using this exact script at four of my sites and it’s been doing great, so I thought I would share with you fine folks in the WP community.
On-topic ideas and suggestions welcome! :)