How To Add Meta Tags In WordPress Without A Plugin

How to add meta tags in WordPress without a plugin

Meta tags are brilliant for helping Search Engines understand what is happening on the website and help social media pick the correct data when your website is shared across different social media. Your appearance is super important for your performance on social media.

You must do a little coding to set your meta tags without a plugin. In your WordPress menu, head over to Appearance -> Theme file editor. On the right side, click on functions.php, then add this code:
add_action(‘wp_head’, ‘meta_tags’);
function meta_tags(){
?>
PASTE META TAGS HERE
<?php
}

Don’t get scared by the code if you haven’t coded before or have limited knowledge. It’s very straightforward, and if you want to see a full example, including the meta tags, then keep on reading. I will dig into both OG meta tags, which are used by social media and the general SEO tags, which Google and other search engines use as guidelines.

What are meta tags?

What you see I have marked on the image below is a meta description. It’s a short description describing your website to Google, and it’s pulled from your source code. The meta description is rarely used by Google any longer as Google takes pieces of your content relevant to the search phrase and displays that instead.

image

Another meta tag is meta keywords. People often add their focus keyword in this meta tag, but this meta tag is deprecated and no longer used by Google. In the past, it was used by Google to help Google decide where this piece of content should wank, basically relevant keywords.

There is also the meta title, and this, however, is handled entirely automatically from WordPress. You don’t need to do anything in this case. Your meta title is the title of your blog post, so ensure to add your primary keywords in the title, and it’s also used as the first header on your blog post.

[cboxarea id=”cbox-DZ7YzH84qT48YAf1″]

Usually, you would use an SEO plugin like Rank Math or Yoast to take care of your Search Engine Optimization, especially your keywords. Still, it is possible to achieve this in an automated way without installing a plugin.

Add meta tags automatically via the WordPress theme file editor (functions.php)

WordPress is a Content Management system, so you can almost do anything in the admin part. Here is a step-by-step guide to automatically adding meta tags for all your content types, whether it’s pages or blog posts.

Head over to Appearance -> Theme file editor, and make sure you click on functions.php on the right side. Then scroll to the bottom of the file and insert the following code snippet.

function add_meta_description_everywhere() {
global $post;

if ( is_singular() ) {
  $post_content = strip_tags( $post->post_content );
  $post_content = strip_shortcodes( $post->post_content );
  $post_content = str_replace( array("\n", "\r", "\t"), ' ', $post_content );
  $post_content = mb_substr( $post_content, 0, 160, 'utf8' );
  echo'<meta name="description" content="'.$post_content.'" />'."\n";
}

if ( is_home() ) {
  echo'<meta name="description" content="'.get_bloginfo( "description" ) .'" />'."\n";
}

if ( is_category() ) {
  $cat_description = strip_tags(category_description());
  echo'<meta name="description" content="'.$cat_description.'" />'."\n";
}
}
add_action( 'wp_head', 'add_meta_description_everywhere');

This function will, on all your single posts, take the content of the posts’ first 160 characters and use it as a meta description. A meta description is recommended to be between 155-160. Otherwise, it’ll start to be cut off by Google and other search engines.

If you instead want to watch a video of how it’s done, then I’ve made a small guide here, where I show you, in less than 20 seconds, how to do it.

If you want to be able to manage the meta tags for each post and page individually, then you’ll have to install an SEO plugin such as Rank Math or Yoast.

This method I’ll recommend almost anyone if they don’t want to use a plugin, but there is a second method where we can use an FTP client like FileZilla.

Add meta tags to WordPress via the theme header.php

Now you know how to add meta tags using functions.php, and it happens in an automated way.

If you want to add a meta tag which is the same on all pages, then you can do it in the header.php in Appearance -> Theme Editor.

Make sure your theme is chosen in the dropdown in the top right, it usually is per default, but if you’re using a child theme, you might need to change to the main theme.

But then you click on your header.php file, and in between the HTML tags <head></head> is where you want to place your meta tag.

Add meta tags to WordPress via the theme header

A meta tag could for instance be you wanted to set the viewport and the allowed zoom on your website such as:

<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=5">

Add meta tags to WordPress via an FTP client

When you’re signed in via FileZilla, you need to navigate through the folders to wp-content -> themes -> [Your active theme] -> functions.php.

Right-click on the functions.php file and press View/edit. It will now open up the file in an editor, and I’m personally using VSCode. Now the steps are just the same as in the previous method. Scroll to the bottom of the file and insert the code snippet listed above. Then press save, and make sure you upload the file to FileZilla again.

If you want to see a guide on how you use FileZilla or, in general, another FTP client, then you can read and watch it in my article about how you back up your website manually.

Add Open Graph meta tags via the WordPress theme file editor

Social media use an Open Graph tag to figure out what image and text to use when you or others share your website or specific content types on social media.

If you make sure your open graph tags are taken care of. Your website, when shared, will look something like this:

image 1

You can always check your website on opengraph.xyz to ensure your Open Graph tags are in order.

To achieve adding open graph meta tags without a plugin, you need to go to appearance -> Theme file editor and click on functions.php, and then insert the following code snippet. This is an automated code snippet, so it will automatically take the title, description, URL and image from the page your visitor is trying to share and showcase it.

function add_og_tags_everywhere() {
  global $post;
  $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
  echo '<meta property="og:site_name" content="' . get_bloginfo( "name" ) . '" />' . "\n";
  echo '<meta property="og:url" content="' . get_permalink() . '" />' . "\n";
  echo '<meta property="og:image" content="' . $thumbnail[0] . '" />' . "\n";
  if ( is_singular() ) {
    $post_content = strip_tags( $post->post_content );
    $post_content = strip_shortcodes( $post->post_content );
    $post_content = str_replace( array("\n", "\r", "\t"), ' ', $post_content );
    $post_content = mb_substr( $post_content, 0, 160, 'utf8' );
    echo '<meta property="og:title" content="' . get_the_title() . '" />' . "\n";
    echo '<meta property="og:description" content="' . $post_content . '" />' . "\n";
  }
  if ( is_home() ) {
    echo '<meta property="og:title" content="' . get_bloginfo( "name" ) . '" />' . "\n";
    echo '<meta property="og:description" content="' . get_bloginfo( "description" ) . '" />' . "\n";
  }
  if ( is_category() ) {
    $cat_description = strip_tags(category_description());
    echo '<meta property="og:title" content="' . single_cat_title() . '" />' . "\n";
    echo '<meta property="og:description" content="' . $cat_description . '" />' . "\n";
  }
}
add_action( 'wp_head', 'add_og_tags_everywhere');

Similar Posts

Leave a Reply

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