I run a multiple author platform where I have added conditions that force authors to add tags to their post. To improve SEO, I would like to add Meta Tags to the posts. Ideally, with a light solution (have high traffic) through functions or custom plugin.
There are two meta tags that I want to add, first one is description:
<meta name="description" content="Description should be no more than 150 characters" />
For the description, I want to grab the excerpt of the post and strip it to 150 character max (including spaces). If the post doesn't have an excerpt, then grab the first 150 characters of the body text (excluding shortcodes if there are any).
For the keywords:
<meta name="keywords" content="Keyword, Keyword 2, Keyword 3" />
Each keyword or keyword phrase need to be separated by a comma and then a space. I want to grab the category and tags of the post and add it here. For instance, if my post's category is Movie
, and the tags are Oldboy
, Revenge
and South Korean
- then the meta keywords would be: content="Movie, Oldboy, Revenge, South Korean"
Also, obviously, the code need to be executed only on Post pages. And I am guessing that the <head>
HTML tag comes far before the actual loop, so I'll need something else than the global $post
.
If you choose to answer, please be detailed and add comments in your code so that I can understand and learn from you.