WordPress Add HTML Meta Tag in Header – WordPress Tutorial

By | April 8, 2021

In wordpress, we may edit wordpress theme files to add some html meta tags. However, if we want to add html meta tags by programming, how to do? In this tutorial, we will introdue this topic.

HTML Meta Tag

Look at example code below:

<head>
<meta charset="<?php bloginfo( 'charset' ); ?>">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="profile" href="http://gmpg.org/xfn/11">

<?php wp_head(); ?>
</head>

Run this code, you may get these html tags.

WordPress wp_head() function example

However, if you plan to add htm tags by code, such as html meta tag. How to do?

How to add html meta tag in wordpress html header?

If we will add a html meta tag:

<meta name="thumbnail" content="" />

Look at example code below:

function wpsp_add_custom_meta()
{
	if(is_single()){
		global $post;
		if (has_post_thumbnail( $post->ID ) ){
			$img_url = get_the_post_thumbnail_url($post->ID, 'full');
			echo '<meta name="thumbnail" content="'.$img_url.'" />';
		}
	}
	
}
add_action('wp_head','wpsp_add_custom_meta',5);

In this example, we will add a thumbnail meta tag in wordpress single page. You can add this code in your theme functions.php.

Run this code, you will find this meta tag is added successfully.

Leave a Reply

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